module EzNemo::StorageAdapter

Defines DataStore class for MySQL

Constants

DEFAULT_QUEUE_SIZE

Number of records it queues up before writing

Public Class Methods

new() click to toggle source
# File lib/eznemo/mysql.rb, line 19
def initialize
  @results = []
  @queue_size = EzNemo.config[:datastore][:queue_size]
  @queue_size ||= DEFAULT_QUEUE_SIZE
  @queue_interval = EzNemo.config[:datastore][:queue_interval]
  @opts = EzNemo.config[:datastore][:options]
  @opts[:flags] = Mysql2::Client::MULTI_STATEMENTS
end

Public Instance Methods

check_ids_with_tags(tags = nil) click to toggle source

@param tags [Array<String, …>] list of tag text @return [Array] check_id mathing all tags; nil when no tags supplied

# File lib/eznemo/mysql.rb, line 51
def check_ids_with_tags(tags = nil)
  return nil if tags.nil? || tags.empty?
  candi_ids = []
  tags.each { |t| candi_ids << Tag.where(text: t).map(:check_id) }
  final_ids = candi_ids[0]
  candi_ids.each { |ids| final_ids = final_ids & ids }
  final_ids
end
checks() click to toggle source

Returns all active checks @return [Array<EzNemo::Check, …>]

# File lib/eznemo/mysql.rb, line 36
def checks
  checks_with_tags(EzNemo.config[:checks][:tags])
end
checks_with_tags(tags = nil) click to toggle source

Returns all active checks matching all tags @param tags [Array<String, …>] list of tag text @return [Array<EzNemo::Checks, …>]

# File lib/eznemo/mysql.rb, line 43
def checks_with_tags(tags = nil)
  cids = check_ids_with_tags(tags)
  return Check.where(state: true).all if cids.nil?
  Check.where(state: true, id: cids).all
end
emdatabase() click to toggle source

Creates and returns new instance of {Mysql2::EM::Client} @return [Mysql2::EM::Client]

# File lib/eznemo/mysql.rb, line 30
def emdatabase
  Mysql2::EM::Client.new(@opts)
end
flush() click to toggle source

Flush queue to storage

# File lib/eznemo/mysql.rb, line 112
def flush
  logger = EzNemo.logger
  # Won't run after trap; run in another thread
  thr = Thread.new do
    logger.debug 'Spawned flushing thread.'
    if write_results(true)
      logger.info "Flushed."
    else
      logger.info "Nothing to flush."
    end
  end
  thr.join
end
start_loop() click to toggle source

Register EventMachine blocks

# File lib/eznemo/mysql.rb, line 61
def start_loop
  return unless @queue_interval
  logger = EzNemo.logger
  logger.info 'Registering MySQL EM block...'
  EM.add_periodic_timer(@queue_interval) do
    EzNemo.logger.debug 'Queue interval time arrived.'
    write_results
  end
end
store_result(result) click to toggle source

Stores a result; into queue first @param result [Hash] (see {EzNemo::Monitor#report})

# File lib/eznemo/mysql.rb, line 73
def store_result(result)
  @results << result
  return if @queue_interval
  if @results.count >= @queue_size
    EzNemo.logger.debug 'Queue is full.'
    write_results
  end
end
write_results(sync = false) click to toggle source

Write the results to storage from queue @param sync [Boolean] use EM (async) if false @return [Object] Mysql2 client instance

# File lib/eznemo/mysql.rb, line 85
def write_results(sync = false)
  logger = EzNemo.logger
  return nil if @results.empty?
  if sync
    logger.debug 'Writing to DB...'
    Result.db.transaction do
      @results.each { |r| r.save}
    end
    return true
  else
    db = emdatabase
    stmt = ''
    @results.each { |r| stmt << Result.dataset.insert_sql(r) + ';' }
    defer = db.query(stmt)
    defer.callback do
      logger.debug 'Wrote to DB async.'
    end
    defer.errback do |r|
      logger.error r.message
      db.close if db.ping
    end
  end
  @results.clear
  db
end