class Procrastinator::Task
Wraps a task handler and task metadata
@author Robin Miller
Constants
- TIME_FIELDS
Fields that store time information
Public Class Methods
# File lib/procrastinator/task.rb, line 22 def initialize(metadata, handler) @metadata = metadata @handler = handler end
Public Instance Methods
Records a failure in metadata and attempts to run the handler’s fail
hook if present.
@param error [StandardError] - the error that caused the failure
# File lib/procrastinator/task.rb, line 50 def fail(error) hook = @metadata.failure(error) try_hook(hook, error) hook end
Executes the Task
Handler’s run
hook and records the attempt.
If the run
hook completes successfully, the success hook will also be executed, if defined.
@raise [ExpiredError] when the task run_at is after the expired_at. @raise [AttemptsExhaustedError] when the task has been attempted more times than allowed by the queue settings.
# File lib/procrastinator/task.rb, line 33 def run raise ExpiredError, "task is over its expiry time of #{ @metadata.expire_at.iso8601 }" if @metadata.expired? @metadata.add_attempt result = Timeout.timeout(queue.timeout) do @handler.run end @metadata.clear_fails try_hook(:success, result) end
Convert the task into a human-legible string. @return [String] Including the queue name, id, and serialized data.
# File lib/procrastinator/task.rb, line 67 def to_s "#{ @metadata.queue.name }##{ id } [#{ serialized_data }]" end
Attempts to run the given optional event hook on the handler, catching any resultant errors to prevent the whole task from failing despite the actual work in run
completing.
# File lib/procrastinator/task.rb, line 59 def try_hook(method, *params) @handler.send(method, *params) if @handler.respond_to? method rescue StandardError => e warn "#{ method.to_s.capitalize } hook error: #{ e.message }" end