Class: Jobit::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/jobit/worker.rb

Constant Summary collapse

SLEEP =
5

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Worker

Returns a new instance of Worker.



9
10
11
12
13
# File 'lib/jobit/worker.rb', line 9

def initialize(options={})
  @quiet = options[:quiet]
  #Delayed::Job.min_priority = options[:min_priority] if options.has_key?(:min_priority)
  #Delayed::Job.max_priority = options[:max_priority] if options.has_key?(:max_priority)
end

Instance Method Details

#loggerObject



5
6
7
# File 'lib/jobit/worker.rb', line 5

def logger
  @logger ||= Jobit::Job.logger
end

#say(text) ⇒ Object



43
44
45
46
# File 'lib/jobit/worker.rb', line 43

def say(text)
  puts text unless @quiet
  logger.info text if logger
end

#startObject



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
# File 'lib/jobit/worker.rb', line 15

def start
  #say "*** Starting job worker #{Jobit::Job.worker_name}"
  say "*** Starting job worker #{Jobit::Job.worker_name}"

  trap('TERM') { say 'Exiting...'; $exit = true }
  trap('INT') { say 'Exiting...'; $exit = true }

  loop do
    result = nil

    realtime = Benchmark.realtime do
      result = Jobit::Job.work_off
    end

    count = result.sum

    break if $exit

    if count.zero?
      sleep(SLEEP)
    else
      say "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result[1]]
    end

    break if $exit
  end
end