Class: Delayed::Worker
- Inherits:
-
Object
- Object
- Delayed::Worker
- Defined in:
- lib/hirefire/workers/delayed_job/worker.rb
Instance Method Summary collapse
Instance Method Details
#start ⇒ Object
This method gets invoked on heroku by the rake task “jobs:work”
This is basically the same method as the Delayed Job version, except for the following:
-
All ouput will now go through the HireFire::Logger.
-
Invoke the ::Delayed::Job.environment.hire method at every loop to see whether we need to hire more workers so that we can delegate this task to the workers, rather than the web servers to improve web-throughput by avoiding any unnecessary API calls to Heroku. If there are any workers running, then the front end will never invoke API calls since the worker(s) can handle this itself.
-
When HireFire cannot find any jobs to process it sends the “fire” signal to all workers, ending all the processes simultaneously. The reason we wait for all the processes to finish before sending the signal is because it’ll otherwise interrupt workers and leave jobs unfinished.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/hirefire/workers/delayed_job/worker.rb', line 29 def start HireFire::Logger. "Starting job worker!" trap('TERM') { HireFire::Logger. 'Exiting...'; $exit = true } trap('INT') { HireFire::Logger. 'Exiting...'; $exit = true } queued = Delayed::Job.new loop do ::Delayed::Job.environment.hire result = nil realtime = Benchmark.realtime do result = work_off end count = result.sum break if $exit if count.zero? sleep(1) else HireFire::Logger. "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result.last] end ## # HireFire Hook # After the last job in the queue finishes processing, Delayed::Job.new.jobs (queued.jobs) # will return 0. This means that there aren't any more jobs to process for any of the workers. # If this is the case it'll command the current environment to fire all the hired workers # and then immediately break out of this infinite loop. if queued.jobs == 0 break if Delayed::Job.environment.fire end break if $exit end ensure Delayed::Job.clear_locks!(name) end |