class FeedNotifier::Runner

Attributes

config[R]

Public Class Methods

new(run_config) click to toggle source
# File lib/feed_notifier/runner.rb, line 5
def initialize(run_config)
  @run_config = run_config
  @config = Config.load(run_config[:config_path])
end

Public Instance Methods

check_and_notify(feed_list) click to toggle source
# File lib/feed_notifier/runner.rb, line 53
def check_and_notify(feed_list)
  FeedNotifier.logger.info "polling feed list..#{feed_list.urls}"

  entries = feed_list.updated_entries()
  FeedNotifier.logger.info "updated entries #{entries}"
  if entries.size > 0
    entries.each do |entry|
      Notifier.notify(entry)
    end
  end
end
check_pid() click to toggle source
# File lib/feed_notifier/runner.rb, line 10
def check_pid
  if File.exist?(pid_path)
    pid = File.read(pid_path)
    raise "already running process PID:#{pid}"
  end
end
cleanup() click to toggle source
# File lib/feed_notifier/runner.rb, line 17
def cleanup
  if File.exist?(pid_path)
    File.delete(pid_path)
  end
end
pid_path() click to toggle source
# File lib/feed_notifier/runner.rb, line 23
def pid_path
  File.join(@run_config[:pid_dir],'feed_notifier.pid')
end
run() click to toggle source
# File lib/feed_notifier/runner.rb, line 40
def run
  setup do
    thread = ::Thread.new do
      feed_list = FeedList.new(config)
      while true do
        check_and_notify(feed_list)
        sleep config.interval
      end
    end
    thread.join
  end
end
setup() { || ... } click to toggle source
# File lib/feed_notifier/runner.rb, line 27
def setup
  if @run_config[:daemon]
    check_pid
    Process.daemon if @run_config[:daemon]
  end

  begin
    yield
  rescue Interrupt => e
    cleanup
  end
end