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
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.
19 20 21 22 |
# File 'lib/procrastinator/task.rb', line 19 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.
47 48 49 50 51 52 |
# File 'lib/procrastinator/task.rb', line 47 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.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/procrastinator/task.rb', line 30 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.
64 65 66 |
# File 'lib/procrastinator/task.rb', line 64 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.
56 57 58 59 60 |
# File 'lib/procrastinator/task.rb', line 56 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 |