Class: Periodically::Job
- Inherits:
-
Object
- Object
- Periodically::Job
- Defined in:
- lib/periodically/job.rb
Instance Method Summary collapse
- #execute_instance(instance) ⇒ Object
-
#initialize(klass, method, opts) ⇒ Job
constructor
A new instance of Job.
- #poll_next_instance ⇒ Object
Constructor Details
#initialize(klass, method, opts) ⇒ Job
Returns a new instance of Job.
7 8 9 10 11 12 13 |
# File 'lib/periodically/job.rb', line 7 def initialize(klass, method, opts) @klass = klass @method = method @class_key = klass.name @job_key = "#{@class_key}/#{method.to_s}" @opts = opts end |
Instance Method Details
#execute_instance(instance) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/periodically/job.rb', line 24 def execute_instance(instance) return_value = begin instance.send(@method) rescue => e stored_error = "#{e.message}\n#{e.backtrace.join("\n")}" Periodically.logger.error("Job instance[#{instance}] execution raised an exception\n#{stored_error}") new_error_count = increase_instance_error_count(instance) lock_instance(instance, DEFAULT_ERROR_DELAY.call(new_error_count)) store_instance_error(instance, stored_error) return end if return_value.is_a?(Periodically::Defer::DeferInstance) lock_instance(instance, return_value.duration) elsif return_value.is_a?(Periodically::Defer::DeferJob) Job.lock_key("locks:#{@job_key}", return_value.duration) elsif return_value.is_a?(Periodically::Defer::DeferClass) Job.lock_key("locks:#{@class_key}", return_value.duration) end clear_instance_error_count(instance) end |
#poll_next_instance ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/periodically/job.rb', line 15 def poll_next_instance return if job_or_class_locked? where = @opts[:on].call() where.to_a.find do |obj| !instance_locked?(obj) end end |