Class: DelayedJob
- Inherits:
-
Object
- Object
- DelayedJob
- Includes:
- Mongoid::Document, Mongoid::Timestamps
- Defined in:
- app/models/delayed_job.rb
Overview
Handle delayed jobs
TODO: Add fields and functions for :do_not_run_before/_after and Priority
Class Method Summary (collapse)
-
+ (Object) delete_failed_jobs!
Delete failed jobs.
-
+ (Object) enqueue(class_name, not_before, *args)
Store a request in the delayed_jobs-collection.
-
+ (Object) failed
failed.
-
+ (Object) ready
ready jobs - where not_before is before now and not failed.
-
+ (Object) run
run the queue until the processed will be killed.
-
+ (Object) run_next_job
Fetch the oldest job in the queue and run the perform-method of the queued object.
-
+ (Object) waiting
waiting.
Class Method Details
+ (Object) delete_failed_jobs!
Delete failed jobs
102 103 104 105 |
# File 'app/models/delayed_job.rb', line 102 def self.delete_failed_jobs! puts "DELETING #{failed.count} JOB(S) FROM QUEUE" failed.delete_all end |
+ (Object) enqueue(class_name, not_before, *args)
Store a request in the delayed_jobs-collection
39 40 41 42 43 44 |
# File 'app/models/delayed_job.rb', line 39 def self.enqueue(class_name,not_before,*args) job = self.create( :class_name => class_name, :not_before => not_before, :args => args ) end |
+ (Object) failed
failed
29 30 31 |
# File 'app/models/delayed_job.rb', line 29 def failed where(:failed_with.ne => nil) end |
+ (Object) ready
ready jobs - where not_before is before now and not failed
19 20 21 |
# File 'app/models/delayed_job.rb', line 19 def ready where(:not_before.lt => Time.now.utc, :failed_with => nil) end |
+ (Object) run
run the queue until the processed will be killed. Sleep CONSTANTS['sleep_in_delayed_jobs_worker'] seconds between executing the next job. The delay-value can be configured in the application.yml file.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'app/models/delayed_job.rb', line 73 def self.run puts "RUNNING DELAYED JOBS LOOP - PRESS ^C OR SEND SIG_KILL TO TERMINATE" while true if self.ready.count > 0 # There are jobs to execute begin self.run_next_job rescue => e puts "-- ERROR #{e} WHILE EXECUTING JOB #{self.first.inspect}" end else # No jobs ready yet. unless Rails.env == 'production' print "#{Time.now().to_s} - No jobs ready to execute. " print "#{self.waiting.count} waiting jobs. " end if self.failed.count > 0 puts "#{self.failed.count} jobs marked failed" else puts "" unless Rails.env == 'production' end end puts "Sleeping #{CONSTANTS['sleep_in_delayed_jobs_worker']}" unless Rails.env == 'production' sleep( (CONSTANTS['sleep_in_delayed_jobs_worker'] || "60" ).to_i ) end end |
+ (Object) run_next_job
Fetch the oldest job in the queue and run the perform-method of the queued object. When done remove the job from the queue.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/models/delayed_job.rb', line 48 def self.run_next_job job = self.ready.first if job puts "RUNNING #{job.inspect}" unless Rails.env.eql?('production') worker = eval("::#{job.class_name}.new(#{job.args})") if worker.respond_to? "perform" begin worker.perform rescue => e job.failed_with = e.to_s puts "-- JOB FAILED - MARKED AS FAILED WITH #{e.to_s}" job.save return end else puts "CLASS #{job.class_name} DOES NOT RESPOND TO 'perform'. JOB CANCELED" end job.delete end end |
+ (Object) waiting
waiting
24 25 26 |
# File 'app/models/delayed_job.rb', line 24 def waiting where(:not_before.gte => Time.now.utc) end |