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 |
# File 'lib/periodically/job.rb', line 24 def execute_instance(instance) return_value = begin instance.send(@method) rescue => e Periodically::ErrorHandler.handle_error(e, instance) new_error_count = Periodically::ErrorCounts.increment(instance_key(instance)) Periodically::Locks.lock(instance_key(instance), DEFAULT_ERROR_DELAY.call(new_error_count)) Periodically::ErrorMessages.store(instance_key(instance), e) return end if return_value.is_a?(Periodically::Defer::DeferInstance) Periodically::Locks.lock(instance_key(instance), return_value.duration) elsif return_value.is_a?(Periodically::Defer::DeferJob) Periodically::Locks.lock(@job_key, return_value.duration) elsif return_value.is_a?(Periodically::Defer::DeferClass) Periodically::Locks.lock(@class_key, return_value.duration) end Periodically::ErrorCounts.reset(instance_key(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 Periodically::Locks.locked?(@job_key, @class_key) where = @opts[:on].call() where.to_a.find do |obj| !Periodically::Locks.locked?(instance_key(obj)) end end |