Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored jobs:work, added jobs:clear.
- added Rake descriptions - factored jobs:work into Delayed::Worker for reuse in scripts - updated the README to suggest using Delayed::Worker.new.start
- Loading branch information
Showing
3 changed files
with
55 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Delayed | ||
class Worker | ||
SLEEP = 5 | ||
|
||
def initialize(options={}) | ||
@quiet = options[:quiet] | ||
end | ||
|
||
def start | ||
puts "*** Starting job worker #{Delayed::Job.worker_name}" unless @quiet | ||
|
||
trap('TERM') { puts 'Exiting...' unless @quiet; $exit = true } | ||
trap('INT') { puts 'Exiting...' unless @quiet; $exit = true } | ||
|
||
loop do | ||
result = nil | ||
|
||
realtime = Benchmark.realtime do | ||
result = Delayed::Job.work_off | ||
end | ||
|
||
count = result.sum | ||
|
||
break if $exit | ||
|
||
if count.zero? | ||
sleep(SLEEP) | ||
puts 'Waiting for more jobs...' unless @quiet | ||
else | ||
status = "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result.last] | ||
RAILS_DEFAULT_LOGGER.info status | ||
puts status unless @quiet | ||
end | ||
|
||
break if $exit | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,11 @@ | ||
namespace :jobs do | ||
desc "Clear the delayed_job queue." | ||
task :clear => :environment do | ||
Delayed::Job.delete_all | ||
end | ||
|
||
desc "Start a delayed_job worker." | ||
task :work => :environment do | ||
|
||
puts "*** Starting job worker #{Delayed::Job.worker_name}" | ||
|
||
SLEEP = 5 | ||
|
||
trap('TERM') { puts 'Exiting...'; $exit = true } | ||
trap('INT') { puts 'Exiting...'; $exit = true } | ||
|
||
loop do | ||
result = nil | ||
|
||
realtime = Benchmark.realtime do | ||
result = Delayed::Job.work_off | ||
end | ||
|
||
count = result.sum | ||
|
||
break if $exit | ||
|
||
if count.zero? | ||
sleep(SLEEP) | ||
puts 'Waiting for more jobs...' | ||
else | ||
status = "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result.last] | ||
RAILS_DEFAULT_LOGGER.info status | ||
puts status | ||
end | ||
|
||
break if $exit | ||
end | ||
Delayed::Worker.new.start | ||
end | ||
end | ||
end |