Class: Pigeon::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/pigeon/task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#contextObject

Returns the value of attribute context.



7
8
9
# File 'lib/pigeon/task.rb', line 7

def context
  @context
end

#created_atObject (readonly)

Returns the value of attribute created_at.



11
12
13
# File 'lib/pigeon/task.rb', line 11

def created_at
  @created_at
end

#engineObject (readonly)

Returns the value of attribute engine.



9
10
11
# File 'lib/pigeon/task.rb', line 9

def engine
  @engine
end

#exceptionObject (readonly)

Returns the value of attribute exception.



10
11
12
# File 'lib/pigeon/task.rb', line 10

def exception
  @exception
end

#processorObject

Returns the value of attribute processor.



8
9
10
# File 'lib/pigeon/task.rb', line 8

def processor
  @processor
end

#started_atObject (readonly)

Returns the value of attribute started_at.



12
13
14
# File 'lib/pigeon/task.rb', line 12

def started_at
  @started_at
end

#stateObject (readonly)

Properties ===========================================================



6
7
8
# File 'lib/pigeon/task.rb', line 6

def state
  @state
end

Class Method Details

.initial_stateObject

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_statesObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


56
57
58
# File 'lib/pigeon/task.rb', line 56

def finished?
  @state == :finished
end

#inspectObject



86
87
88
# File 'lib/pigeon/task.rb', line 86

def inspect
  "<#{self.class}\##{self.object_id}>"
end

#priorityObject

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.

Returns:

  • (Boolean)


71
72
73
# File 'lib/pigeon/task.rb', line 71

def terminal_state?
  self.class.terminal_states.include?(@state)
end