Class: Pigeon::Task
- Inherits:
-
Object
- Object
- Pigeon::Task
- Defined in:
- lib/pigeon/task.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#processor ⇒ Object
Returns the value of attribute processor.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#state ⇒ Object
readonly
Properties ===========================================================.
Class Method Summary collapse
-
.initial_state ⇒ Object
Defines the initial state of this type of task.
-
.terminal_states ⇒ Object
Returns an array of the terminal states for this task.
Instance Method Summary collapse
- #<=>(task) ⇒ Object
-
#dispatch(&block) ⇒ Object
Dispatches a block to be run as soon as possible.
-
#exception? ⇒ Boolean
Returns true if an exception was thrown, false otherwise.
-
#failed? ⇒ Boolean
Returns true if the task is in the failed state, false otherwise.
-
#finished? ⇒ Boolean
Returns true if the task is in the finished state, false otherwise.
-
#initialize(context = nil, engine = nil) ⇒ Task
constructor
Creates a new instance of a Task with a series of context.
- #inspect ⇒ Object
-
#priority ⇒ Object
Returns a numerical priority order.
-
#run!(processor = nil, initial_state = nil, &callback) ⇒ Object
Kicks off the task.
-
#terminal_state? ⇒ Boolean
Returns true if the task is in any terminal state.
Constructor Details
#initialize(context = nil, engine = nil) ⇒ Task
Creates a new instance of a Task with a series of context. An optional engine parameter indicates which engine this task should be associated with. An arbitrary context object can be specified which will persist in the context property.
34 35 36 37 38 39 40 |
# File 'lib/pigeon/task.rb', line 34 def initialize(context = nil, engine = nil) @context = context @engine = engine || Pigeon::Engine.default_engine @created_at = Time.now after_initialized end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
7 8 9 |
# File 'lib/pigeon/task.rb', line 7 def context @context end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
11 12 13 |
# File 'lib/pigeon/task.rb', line 11 def created_at @created_at end |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
9 10 11 |
# File 'lib/pigeon/task.rb', line 9 def engine @engine end |
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
10 11 12 |
# File 'lib/pigeon/task.rb', line 10 def exception @exception end |
#processor ⇒ Object
Returns the value of attribute processor.
8 9 10 |
# File 'lib/pigeon/task.rb', line 8 def processor @processor end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
12 13 14 |
# File 'lib/pigeon/task.rb', line 12 def started_at @started_at end |
#state ⇒ Object (readonly)
Properties ===========================================================
6 7 8 |
# File 'lib/pigeon/task.rb', line 6 def state @state end |
Class Method Details
.initial_state ⇒ Object
Defines the initial state of this type of task. Default is :initialized but this can be customized in a subclass.
18 19 20 |
# File 'lib/pigeon/task.rb', line 18 def self.initial_state :initialized end |
.terminal_states ⇒ Object
Returns an array of the terminal states for this task. Default is :failed, :finished but this can be customized in a subclass.
24 25 26 |
# File 'lib/pigeon/task.rb', line 24 def self.terminal_states @terminal_states ||= [ :failed, :finished ].freeze end |
Instance Method Details
#<=>(task) ⇒ Object
90 91 92 |
# File 'lib/pigeon/task.rb', line 90 def <=>(task) self.priority <=> task.priority end |
#dispatch(&block) ⇒ Object
Dispatches a block to be run as soon as possible.
76 77 78 |
# File 'lib/pigeon/task.rb', line 76 def dispatch(&block) @engine.dispatch(&block) end |
#exception? ⇒ Boolean
Returns true if an exception was thrown, false otherwise.
66 67 68 |
# File 'lib/pigeon/task.rb', line 66 def exception? !!@exception end |
#failed? ⇒ Boolean
Returns true if the task is in the failed state, false otherwise.
61 62 63 |
# File 'lib/pigeon/task.rb', line 61 def failed? @state == :failed end |
#finished? ⇒ Boolean
Returns true if the task is in the finished state, false otherwise.
56 57 58 |
# File 'lib/pigeon/task.rb', line 56 def finished? @state == :finished end |
#inspect ⇒ Object
86 87 88 |
# File 'lib/pigeon/task.rb', line 86 def inspect "<#{self.class}\##{self.object_id}>" end |
#priority ⇒ Object
Returns a numerical priority order. If redefined in a subclass, should return a comparable value.
82 83 84 |
# File 'lib/pigeon/task.rb', line 82 def priority @created_at.to_f end |
#run!(processor = nil, initial_state = nil, &callback) ⇒ Object
Kicks off the task. The processor execurting the task should be supplied as the first argument. An optional callback is executed just before each state is excuted and is passed the state name as a symbol.
45 46 47 48 49 50 51 52 53 |
# File 'lib/pigeon/task.rb', line 45 def run!(processor = nil, initial_state = nil, &callback) @callback = callback if (block_given?) @state = initial_state || self.class.initial_state @started_at = Time.now @processor = processor run_state!(@state) end |
#terminal_state? ⇒ Boolean
Returns true if the task is in any terminal state.
71 72 73 |
# File 'lib/pigeon/task.rb', line 71 def terminal_state? self.class.terminal_states.include?(@state) end |