Class: RelaxedJob::Worker

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

Constant Summary collapse

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



52
53
54
# File 'lib/relaxed_job/worker.rb', line 52

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

#startObject



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

def start
  say "*** Starting job worker #{queue.worker_name}"

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

  loop do
    result = nil

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

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

    break if $exit

    if count.zero?
      sleep(SLEEP)
    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