class Ryespy::App

Attributes

config[R]
running[R]

Public Class Methods

config_defaults() click to toggle source
# File lib/ryespy/app.rb, line 12
def self.config_defaults
  {
    :log_level          => :INFO,
    :polling_interval   => 60,
    :redis_ns_ryespy    => 'ryespy',
    :redis_ns_notifiers => 'resque',
    :imap => {
      :port    => 993,
      :ssl     => true,
      :filters => ['INBOX'], # mailboxes
    },
    :ftp => {
      :port    => 21,
      :passive => false,
      :filters => ['/'], # dirs
    },
    :amzn_s3 => {
      :filters => [''], # prefixes
    },
    :goog_cs => {
      :filters => [''], # prefixes
    },
    :goog_drv => {
      :filters => [''],
    },
    :rax_cf => {
      :endpoint => :us,
      :region   => :dfw,
      :filters  => [''], # prefixes
    },
  }
end
new(eternal = false, opts = {}) click to toggle source
# File lib/ryespy/app.rb, line 48
def initialize(eternal = false, opts = {})
  @eternal = eternal
  
  @logger = opts[:logger] || Logger.new(nil)
  
  @config = OpenStruct.new(self.class.config_defaults)
  
  @running = false
  @threads = {}
end

Public Instance Methods

configure() { |config| ... } click to toggle source
# File lib/ryespy/app.rb, line 59
def configure
  yield @config
  
  @logger.level = Logger.const_get(@config.log_level)
  
  Redis.current = Redis::Namespace.new(@config.redis_ns_ryespy,
    :redis => Redis.connect(:url => @config.redis_url)
  )
  
  @logger.debug { "Configured #{@config.to_s}" }
end
notifiers() click to toggle source
# File lib/ryespy/app.rb, line 71
def notifiers
  unless @notifiers
    @notifiers = []
    @config.notifiers[:sidekiq].each do |notifier_url|
      @notifiers << Notifier::Sidekiq.new(
        :url       => notifier_url,
        :namespace => @config.redis_ns_notifiers,
        :logger    => @logger
      )
    end
  end
  
  @notifiers
end
start() click to toggle source
# File lib/ryespy/app.rb, line 86
def start
  begin
    @running = true
    
    setup
    
    @threads[:refresh] ||= Thread.new do
      refresh_loop # refresh frequently
    end
    
    @threads.values.each(&:join)
  ensure
    cleanup
  end
end
stop() click to toggle source
# File lib/ryespy/app.rb, line 102
def stop
  @running = false
  
  @threads.values.each { |t| t.run if t.status == 'sleep' }
end

Private Instance Methods

check_all() click to toggle source
# File lib/ryespy/app.rb, line 139
def check_all
  listener_class_map = {
    :imap    => :IMAP,
    :ftp     => :FTP,
    :amzn_s3 => :AmznS3,
    :goog_cs => :GoogCS,
    :goog_drv => :GoogDrv,
    :rax_cf  => :RaxCF,
  }
  
  listener_config = @config[@config.listener].merge({
    :notifiers => notifiers,
    :logger    => @logger,
  })
  
  listener_class = Listener.const_get(listener_class_map[@config.listener])
  
  listener_class.new(listener_config) do |listener|
    listener_config[:filters].each { |f| listener.check(f) }
  end
end
cleanup() click to toggle source
# File lib/ryespy/app.rb, line 114
def cleanup
end
refresh_loop() click to toggle source
# File lib/ryespy/app.rb, line 117
def refresh_loop
  while @running do
    begin
      check_all
    rescue StandardError => e
      @logger.error { e.to_s }

      raise if @config.log_level == :DEBUG
    end
    
    if !@eternal
      stop
      
      break
    end
    
    @logger.debug { "Snoring for #{@config.polling_interval} s" }
    
    sleep @config.polling_interval # sleep awhile (snore)
  end
end
setup() click to toggle source
# File lib/ryespy/app.rb, line 110
def setup
  require_relative "listener/#{@config.listener}"
end