Class: Pigeon::Task
- Inherits:
-
Object
- Object
- Pigeon::Task
- Defined in:
- lib/pigeon/task.rb
Defined Under Namespace
Classes: EngineRequired
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.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/pigeon/task.rb', line 39 def initialize(context = nil, engine = nil) @context = context @engine = engine || Pigeon::Engine.default_engine @created_at = Time.now @state = nil unless (@engine) raise EngineRequired, "Task creation requires an active Pigeon::Engine" end after_initialized end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
12 13 14 |
# File 'lib/pigeon/task.rb', line 12 def context @context end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
16 17 18 |
# File 'lib/pigeon/task.rb', line 16 def created_at @created_at end |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
14 15 16 |
# File 'lib/pigeon/task.rb', line 14 def engine @engine end |
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
15 16 17 |
# File 'lib/pigeon/task.rb', line 15 def exception @exception end |
#processor ⇒ Object
Returns the value of attribute processor.
13 14 15 |
# File 'lib/pigeon/task.rb', line 13 def processor @processor end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
17 18 19 |
# File 'lib/pigeon/task.rb', line 17 def started_at @started_at end |
#state ⇒ Object (readonly)
Properties ===========================================================
11 12 13 |
# File 'lib/pigeon/task.rb', line 11 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.
23 24 25 |
# File 'lib/pigeon/task.rb', line 23 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.
29 30 31 |
# File 'lib/pigeon/task.rb', line 29 def self.terminal_states @terminal_states ||= [ :failed, :finished ].freeze end |
Instance Method Details
#<=>(task) ⇒ Object
100 101 102 |
# File 'lib/pigeon/task.rb', line 100 def <=>(task) self.priority <=> task.priority end |
#dispatch(&block) ⇒ Object
Dispatches a block to be run as soon as possible.
86 87 88 |
# File 'lib/pigeon/task.rb', line 86 def dispatch(&block) @engine.dispatch(&block) end |
#exception? ⇒ Boolean
Returns true if an exception was thrown, false otherwise.
76 77 78 |
# File 'lib/pigeon/task.rb', line 76 def exception? !!@exception end |
#failed? ⇒ Boolean
Returns true if the task is in the failed state, false otherwise.
71 72 73 |
# File 'lib/pigeon/task.rb', line 71 def failed? @state == :failed end |
#finished? ⇒ Boolean
Returns true if the task is in the finished state, false otherwise.
66 67 68 |
# File 'lib/pigeon/task.rb', line 66 def finished? @state == :finished end |
#inspect ⇒ Object
96 97 98 |
# File 'lib/pigeon/task.rb', line 96 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.
92 93 94 |
# File 'lib/pigeon/task.rb', line 92 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.
55 56 57 58 59 60 61 62 63 |
# File 'lib/pigeon/task.rb', line 55 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.
81 82 83 |
# File 'lib/pigeon/task.rb', line 81 def terminal_state? self.class.terminal_states.include?(@state) end |