Class: Procrastinator::Task
- Inherits:
-
Object
- Object
- Procrastinator::Task
- Extended by:
- Forwardable
- Defined in:
- lib/procrastinator/task.rb
Overview
Wraps a task handler and task metadata
Direct Known Subclasses
Defined Under Namespace
Classes: AttemptsExhaustedError, ExpiredError
Constant Summary collapse
- TIME_FIELDS =
Fields that store time information
[:run_at, :initial_run_at, :expire_at, :last_fail_at].freeze
Instance Method Summary collapse
-
#fail(error) ⇒ Object
Records a failure in metadata and attempts to run the handler’s #fail hook if present.
-
#initialize(metadata, handler) ⇒ Task
constructor
A new instance of Task.
-
#run ⇒ Object
(also: #call)
Executes the Task Handler’s #run hook and records the attempt.
-
#to_s ⇒ String
Convert the task into a human-legible string.
-
#try_hook(method, *params) ⇒ Object
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.
Constructor Details
#initialize(metadata, handler) ⇒ Task
Returns a new instance of Task.
22 23 24 25 |
# File 'lib/procrastinator/task.rb', line 22 def initialize(, handler) = @handler = handler end |
Instance Method Details
#fail(error) ⇒ Object
Records a failure in metadata and attempts to run the handler’s #fail hook if present.
50 51 52 53 54 55 |
# File 'lib/procrastinator/task.rb', line 50 def fail(error) hook = .failure(error) try_hook(hook, error) hook end |
#run ⇒ Object Also known as: call
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.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/procrastinator/task.rb', line 33 def run raise ExpiredError, "task is over its expiry time of #{ @metadata.expire_at.iso8601 }" if .expired? .add_attempt result = Timeout.timeout(queue.timeout) do @handler.run end .clear_fails try_hook(:success, result) end |
#to_s ⇒ String
Convert the task into a human-legible string.
67 68 69 |
# File 'lib/procrastinator/task.rb', line 67 def to_s "#{ @metadata.queue.name }##{ id } [#{ serialized_data }]" end |
#try_hook(method, *params) ⇒ Object
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.
59 60 61 62 63 |
# 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 |