Class: Procrastinator::LoggedTask
- Defined in:
- lib/procrastinator/logged_task.rb
Overview
Task wrapper that adds logging to each step.
Constant Summary
Constants inherited from Task
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #fail(error) ⇒ Object
-
#initialize(task, logger: Logger.new(StringIO.new)) ⇒ LoggedTask
constructor
A new instance of LoggedTask.
-
#run ⇒ Object
Executes the Task Handler’s #run hook and records the attempt.
Methods inherited from Task
Constructor Details
#initialize(task, logger: Logger.new(StringIO.new)) ⇒ LoggedTask
Returns a new instance of LoggedTask.
21 22 23 24 |
# File 'lib/procrastinator/logged_task.rb', line 21 def initialize(task, logger: Logger.new(StringIO.new)) super(task) @logger = logger || raise(ArgumentError, 'Logger cannot be nil') end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
17 18 19 |
# File 'lib/procrastinator/logged_task.rb', line 17 def logger @logger end |
Instance Method Details
#fail(error) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/procrastinator/logged_task.rb', line 38 def fail(error) hook = task.fail(error) begin @logger.error("Task #{ hook }ed: #{ task }") rescue StandardError => e warn "Task logging error: #{ e.message }" end hook end |
#run ⇒ Object
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.
27 28 29 30 31 32 33 34 35 |
# File 'lib/procrastinator/logged_task.rb', line 27 def run task.run begin @logger.info("Task completed: #{ task }") rescue StandardError => e warn "Task logging error: #{ e.message }" end end |