Class: Procrastinator::LoggedTask

Inherits:
Task
  • Object
show all
Defined in:
lib/procrastinator/logged_task.rb

Overview

Task wrapper that adds logging to each step.

See Also:

Author:

  • Robin Miller

Constant Summary

Constants inherited from Task

Task::TIME_FIELDS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Task

#to_s, #try_hook

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

#loggerObject (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

Parameters:

  • error (StandardError)
    • the error that caused the failure



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

#runObject

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.

Raises:

  • (ExpiredError)

    when the task run_at is after the expired_at.

  • (AttemptsExhaustedError)

    when the task has been attempted more times than allowed by the queue settings.



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