File: evented.rb

package info (click to toggle)
ruby-raemon 0.3.0%2Bgit.2012.05.18.b78eaae57c-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 220 kB
  • ctags: 104
  • sloc: ruby: 901; makefile: 4
file content (51 lines) | stat: -rw-r--r-- 1,187 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
####################################################################
# Simple queue daemon example
# Using asynchronous Beanstalk Ruby driver (em-jack)
####################################################################
$:.unshift ::File.dirname(__FILE__) + '/../lib'

require 'rubygems'
require 'raemon'
require 'em-jack'

class EventedJobWorker
  include Raemon::Worker
  
  def start
    super
  end
  
  def stop
    super
    EM.stop_event_loop if EM.reactor_running?
  end
  
  def run
    EM.run do
      @queue = EMJack::Connection.new
      
      @queue.each_job(5) do |job|        
        logger.info "(#{Process.ppid}:#{Process.pid}) got job: #{job.inspect}"
        # process(job)
        @queue.delete(job)
        
        heartbeat!
      end
      
      @queue.on_error do |error|
        case error
        when :timed_out
          # We use the reserve timeout for the heartbeat
          heartbeat!
          
          # Note: this will only run once.. we need to call the @queue.each_job
          # again .. maybe put it in a block
        else
          logger.error error.to_s
        end
      end
    end
  end
end

Raemon::Master.start 2, EventedJobWorker