Class: Cuetip::Models::Job
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Cuetip::Models::Job
- Defined in:
- lib/cuetip/models/job.rb
Constant Summary collapse
- STATUSES =
%w[Pending Running Complete Aborted Expired].freeze
Instance Method Summary collapse
-
#execute(&block) ⇒ Boolean
Execute the job.
-
#expired? ⇒ Boolean
Has this job expired?.
-
#expires_at ⇒ Object
The time that this job expired.
-
#log(text) ⇒ Object
Log some text about this job.
-
#queued? ⇒ Boolean
Is this job in the queue.
-
#remove_from_queue ⇒ Object
Remove this job from the queue.
-
#requeue_on_failure? ⇒ Boolean
Should this job be requeued on a failure right now?.
Instance Method Details
#execute(&block) ⇒ Boolean
Execute the job
71 72 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/cuetip/models/job.rb', line 71 def execute(&block) log "Beginning execution of job #{id} with #{class_name}" # Initialize a new instance of the job we wish to execute job_klass = class_name.constantize.new(self) # If the job has expired, we should not be executing this so we'll just # remove it from the queue and mark it as expired. if expired? log 'Job has expired' self.status = 'Expired' remove_from_queue Cuetip.config.emit(:expired, self, job_klass) return false end Cuetip.config.emit(:before_perform, self, job_klass) # If we have a block, call this so we can manipulate our actual job class # before execution if needed (mostly for testing) block.call(job_klass) if block_given? # Mark the job as runnign update!(status: 'Running', started_at: Time.now, executions: executions + 1) begin # Perform the job within a timeout Timeout.timeout(maximum_execution_time || 1.year) do job_klass.perform end # Mark the job as complete and remove it from the queue self.status = 'Complete' log 'Job completed successfully' remove_from_queue Cuetip.config.emit(:completed, self, job_klass) true rescue Exception, Timeout::TimeoutError => e log "Job failed with #{e.class} (#{e.})" # If there's an error, mark the job as failed and copy exception # data into the job self.status = 'Failed' self.exception_class = e.class.name self. = e. self.exception_backtrace = e.backtrace.join("\n") # Handle requeing the job if needed. if requeue_on_failure? # Requeue this job for execution again after the retry interval. new_job = queued_job.requeue(run_after: Time.now + retry_interval.to_i) log "Requeing job to run after #{new_job.run_after.to_s(:long)}" self.status = 'Pending' else # We're done with this job. We can't do any more retries. remove_from_queue end Cuetip.config.emit(:exception, e, self, job_klass) false end ensure unless destroyed? self.finished_at = Time.now save! end Cuetip.config.emit(:finished, self, job_klass) log 'Finished processing' end |
#expired? ⇒ Boolean
Has this job expired?
34 35 36 |
# File 'lib/cuetip/models/job.rb', line 34 def expired? ttl? ? expires_at <= Time.now : false end |
#expires_at ⇒ Object
The time that this job expired
39 40 41 |
# File 'lib/cuetip/models/job.rb', line 39 def expires_at ttl? ? created_at + ttl : nil end |
#log(text) ⇒ Object
Log some text about this job
64 65 66 |
# File 'lib/cuetip/models/job.rb', line 64 def log(text) Cuetip.logger.info "[#{id}] #{text}" end |
#queued? ⇒ Boolean
Is this job in the queue
29 30 31 |
# File 'lib/cuetip/models/job.rb', line 29 def queued? queued_job.present? end |
#remove_from_queue ⇒ Object
Remove this job from the queue
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cuetip/models/job.rb', line 49 def remove_from_queue queued_job&.destroy self.queued_job = nil log 'Removed from queue' if delete_after_execution? destroy log 'Removed job from database' end end |
#requeue_on_failure? ⇒ Boolean
Should this job be requeued on a failure right now?
44 45 46 |
# File 'lib/cuetip/models/job.rb', line 44 def requeue_on_failure? retry_count && retry_interval ? executions <= retry_count : false end |