Class: FeedNotifier::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/feed_notifier/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(run_config) ⇒ Runner

Returns a new instance of Runner.



5
6
7
8
# File 'lib/feed_notifier/runner.rb', line 5

def initialize(run_config)
  @run_config = run_config
  @config = Config.load(run_config[:config_path])
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/feed_notifier/runner.rb', line 3

def config
  @config
end

Instance Method Details

#check_and_notify(feed_list) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# 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_pidObject



10
11
12
13
14
15
# 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

#cleanupObject



17
18
19
20
21
# File 'lib/feed_notifier/runner.rb', line 17

def cleanup
  if File.exist?(pid_path)
    File.delete(pid_path)
  end
end

#pid_pathObject



23
24
25
# File 'lib/feed_notifier/runner.rb', line 23

def pid_path
  File.join(@run_config[:pid_dir],'feed_notifier.pid')
end

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
# 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

#setupObject



27
28
29
30
31
32
33
34
35
36
37
38
# 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