Class: Pigeon::Task

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

Defined Under Namespace

Classes: EngineRequired

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.



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

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#created_atObject (readonly)

Returns the value of attribute created_at.



16
17
18
# File 'lib/pigeon/task.rb', line 16

def created_at
  @created_at
end

#engineObject (readonly)

Returns the value of attribute engine.



14
15
16
# File 'lib/pigeon/task.rb', line 14

def engine
  @engine
end

#exceptionObject (readonly)

Returns the value of attribute exception.



15
16
17
# File 'lib/pigeon/task.rb', line 15

def exception
  @exception
end

#processorObject

Returns the value of attribute processor.



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

def processor
  @processor
end

#started_atObject (readonly)

Returns the value of attribute started_at.



17
18
19
# File 'lib/pigeon/task.rb', line 17

def started_at
  @started_at
end

#stateObject (readonly)

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



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

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.



23
24
25
# File 'lib/pigeon/task.rb', line 23

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.



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


66
67
68
# File 'lib/pigeon/task.rb', line 66

def finished?
  @state == :finished
end

#inspectObject



96
97
98
# File 'lib/pigeon/task.rb', line 96

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

#priorityObject

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.

Returns:

  • (Boolean)


81
82
83
# File 'lib/pigeon/task.rb', line 81

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