Class: Navvy::Job
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Navvy::Job
- Includes:
- DataMapper::Resource, MongoMapper::Document, Mongoid::Document, Mongoid::Timestamps
- Defined in:
- lib/navvy/job.rb,
lib/navvy/job/sequel.rb,
lib/navvy/job/mongoid.rb,
lib/navvy/job/data_mapper.rb,
lib/navvy/job/mongo_mapper.rb,
lib/navvy/job/active_record.rb
Class Attribute Summary collapse
-
.keep ⇒ Fixnum, ...
If and how long the jobs should be kept.
-
.limit ⇒ Integer
Limit of jobs to be fetched at once.
-
.max_attempts ⇒ Fixnum
How often should a job be retried? Will use the value stored in Navvy.configuration (defaults to 24), or – for backwards compatibility – Navvy::Job.max_attempts.
Class Method Summary collapse
-
.cleanup ⇒ true, false
Clean up jobs that we don’t need to keep anymore.
-
.delete_all ⇒ true, false
Deletes all jobs.
-
.enqueue(object, method_name, *args) ⇒ Job, false
Add a job to the job queue.
-
.keep? ⇒ true, false
Should the job be kept? Will calculate if the keeptime has passed if it’s probably a boolean.
-
.next(limit = self.limit) ⇒ array?
Find the next available jobs in the queue.
Instance Method Summary collapse
-
#args ⇒ array
Get the job arguments as an array.
-
#completed(return_value = nil) ⇒ true, false
Mark the job as completed.
-
#completed_at? ⇒ true, false
(also: #completed?)
Check if completed_at is set.
-
#duration ⇒ Time, Integer
Check how long it took for a job to complete or fail.
-
#failed(message = nil) ⇒ true, false
Mark the job as failed.
-
#failed_at? ⇒ true, false
(also: #failed?)
Check if failed_at is set.
-
#ran? ⇒ true, false
Check if the job has been run.
-
#retry ⇒ Navvy::Job
Retry the current job.
-
#run ⇒ String
Run the job.
-
#started ⇒ true, false
Mark the job as started.
-
#status ⇒ :pending, ...
Get the job status.
-
#times_failed ⇒ Integer
Check how many times the job has failed.
Class Attribute Details
.keep ⇒ Fixnum, ...
If and how long the jobs should be kept. Will use the value stored in Navvy.configuration (defaults to false), or – for backwards compatibility – Navvy::Job.keep.
25 26 27 |
# File 'lib/navvy/job.rb', line 25 def self.keep @keep || Navvy.configuration.keep_jobs end |
.limit ⇒ Integer
Limit of jobs to be fetched at once. Will use the value stored in Navvy.configuration (defaults to 100), or – for backwards compatibility – Navvy::Job.limit.
14 15 16 |
# File 'lib/navvy/job.rb', line 14 def self.limit @limit || Navvy.configuration.job_limit end |
.max_attempts ⇒ Fixnum
How often should a job be retried? Will use the value stored in Navvy.configuration (defaults to 24), or – for backwards compatibility – Navvy::Job.max_attempts.
36 37 38 |
# File 'lib/navvy/job.rb', line 36 def self.max_attempts @max_attempts || Navvy.configuration.max_attempts end |
Class Method Details
.cleanup ⇒ true, false
Clean up jobs that we don’t need to keep anymore. If Navvy::Job.keep is false it’ll delete every completed job, if it’s a timestamp it’ll only delete completed jobs that have passed their keeptime.
61 62 63 64 65 66 67 68 |
# File 'lib/navvy/job/sequel.rb', line 61 def self.cleanup if keep.is_a? Fixnum time = Time.now - keep filter{completed_at <= time}.delete else filter(~{:completed_at => nil}).delete unless keep? end end |
.delete_all ⇒ true, false
Deletes all jobs.
75 76 77 |
# File 'lib/navvy/job/sequel.rb', line 75 def self.delete_all Navvy::Job.destroy end |
.enqueue(object, method_name, *args) ⇒ Job, false
Add a job to the job queue.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/navvy/job/sequel.rb', line 17 def self.enqueue(object, method_name, *args) = {} if args.last.is_a?(Hash) = args.last.delete(:job_options) || {} args.pop if args.last.empty? end Job[insert( :object => object.to_s, :method_name => method_name.to_s, :arguments => args.to_yaml, :priority => [:priority] || 0, :parent_id => [:parent_id], :run_at => [:run_at] || Time.now, :created_at => Time.now )] end |
.keep? ⇒ true, false
Should the job be kept? Will calculate if the keeptime has passed if it’s probably a boolean.
47 48 49 50 |
# File 'lib/navvy/job.rb', line 47 def self.keep? return (Time.now + self.keep) >= Time.now if self.keep.is_a? Fixnum self.keep end |
.next(limit = self.limit) ⇒ array?
Find the next available jobs in the queue. This will not include failed jobs (where :failed_at is not nil) and jobs that should run in the future (where :run_at is greater than the current time).
Navvy::Job.limit
jobs were found.
46 47 48 49 50 51 52 |
# File 'lib/navvy/job/sequel.rb', line 46 def self.next(limit = self.limit) filter(:failed_at => nil). filter(:completed_at => nil). filter{run_at <= Time.now}. order(:priority.desc, :created_at). first(limit) end |
Instance Method Details
#args ⇒ array
Get the job arguments as an array.
138 139 140 |
# File 'lib/navvy/job.rb', line 138 def args arguments.is_a?(Array) ? arguments : YAML.load(arguments) end |
#completed(return_value = nil) ⇒ true, false
Mark the job as completed. Will set completed_at to the current time and optionally add the return value if provided.
update_attributes call
98 99 100 101 102 103 |
# File 'lib/navvy/job/sequel.rb', line 98 def completed(return_value = nil) update( :completed_at => Time.now, :return => return_value ) end |
#completed_at? ⇒ true, false Also known as: completed?
Check if completed_at is set.
120 121 122 |
# File 'lib/navvy/job.rb', line 120 def completed_at? !completed_at.nil? end |
#duration ⇒ Time, Integer
Check how long it took for a job to complete or fail.
111 112 113 |
# File 'lib/navvy/job.rb', line 111 def duration ran? ? (completed_at || failed_at) - started_at : 0 end |
#failed(message = nil) ⇒ true, false
Mark the job as failed. Will set failed_at to the current time and optionally add the exception message if provided. Also, it will retry the job unless max_attempts has been reached.
update_attributes call
115 116 117 118 119 120 121 |
# File 'lib/navvy/job/sequel.rb', line 115 def failed( = nil) self.retry unless times_failed >= self.class.max_attempts update( :failed_at => Time.now, :exception => ) end |
#failed_at? ⇒ true, false Also known as: failed?
Check if failed_at is set.
129 130 131 |
# File 'lib/navvy/job.rb', line 129 def failed_at? !failed_at.nil? end |
#ran? ⇒ true, false
Check if the job has been run.
102 103 104 |
# File 'lib/navvy/job.rb', line 102 def ran? completed? || failed? end |
#retry ⇒ Navvy::Job
Retry the current job. Will add self to the queue again, giving the clone a parend_id equal to self.id. Also, the priority of the new job will be the same as its parent’s and it’ll set the :run_at date to N ** 4, where N is the times_failed count.
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/navvy/job.rb', line 83 def retry self.class.enqueue( object, method_name, *(args << { :job_options => { :parent_id => parent_id || id, :run_at => Time.now + times_failed ** 4, :priority => priority } }) ) end |
#run ⇒ String
Run the job. Will delete the Navvy::Job record and return its return value if it runs successfully unless Navvy::Job.keep is set. If a job fails, it’ll call Navvy::Job#failed and pass the exception message. Failed jobs will not get deleted.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/navvy/job.rb', line 64 def run begin started result = constantize(object).send(method_name, *args).inspect Navvy::Job.keep? ? completed(result) : destroy result rescue Exception => exception failed(exception.) end end |
#started ⇒ true, false
Mark the job as started. Will set started_at to the current time.
update_attributes call
85 86 87 |
# File 'lib/navvy/job/sequel.rb', line 85 def started update(:started_at => Time.now) end |
#status ⇒ :pending, ...
Get the job status
147 148 149 150 151 |
# File 'lib/navvy/job.rb', line 147 def status return :completed if completed? return :failed if failed? :pending end |
#times_failed ⇒ Integer
Check how many times the job has failed. Will try to find jobs with a parent_id that’s the same as self.id and count them
129 130 131 132 133 134 |
# File 'lib/navvy/job/sequel.rb', line 129 def times_failed i = parent_id || id self.class.filter({:id => i} | {:parent_id => i}). filter(~{:failed_at => nil}). count end |