Class: RelaxedJob::Worker

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

Constant Summary collapse

DEFAULT_SLEEP =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(couchrest_url, options = {}) ⇒ Worker

Returns a new instance of Worker.



12
13
14
15
# File 'lib/relaxed_job/worker.rb', line 12

def initialize(couchrest_url, options={})
  @queue   = RelaxedJob::Queue.new(couchrest_url)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/relaxed_job/worker.rb', line 10

def options
  @options
end

#queueObject (readonly)

Returns the value of attribute queue.



9
10
11
# File 'lib/relaxed_job/worker.rb', line 9

def queue
  @queue
end

Instance Method Details

#nameObject



17
18
19
# File 'lib/relaxed_job/worker.rb', line 17

def name
  "testname"
end

#say(text) ⇒ Object



54
55
56
# File 'lib/relaxed_job/worker.rb', line 54

def say(text)
  puts text unless options[:quiet]
end

#start(options = {}) ⇒ Object



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
52
# File 'lib/relaxed_job/worker.rb', line 21

def start(options={})
  say "*** Starting job worker #{queue.worker_name}"

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

  sleep_for = options[:sleep] || DEFAULT_SLEEP

  loop do
    result = nil

    realtime = Benchmark.realtime do
      result = queue.work
      sleep sleep_for
    end

    count = result.values.inject(0) { |a,v| a+v }

    break if $exit

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

    break if $exit
  end

ensure
  queue.clear_locks!
end