Class: Taskinator::Process

Inherits:
Object
  • Object
show all
Includes:
Comparable, Instrumentation, Persistence, Workflow
Defined in:
lib/taskinator/process.rb

Direct Known Subclasses

Concurrent, Sequential

Defined Under Namespace

Classes: Concurrent, Sequential

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

#cancelled_payload, #completed_payload, #enqueued_payload, #failed_payload, #instrument, #paused_payload, #processing_payload, #resumed_payload

Methods included from Persistence

add_process_to_list, deserialize, included, processes_list_key, serialize

Methods included from Workflow

#current_state, #current_state=, #transition

Constructor Details

#initialize(definition, options = {}) ⇒ Process

Returns a new instance of Process.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/taskinator/process.rb', line 33

def initialize(definition, options={})
  raise ArgumentError, 'definition' if definition.nil?
  raise ArgumentError, "#{definition.name} does not extend the #{Definition.name} module" unless definition.kind_of?(Definition)

  @uuid = options.delete(:uuid) || Taskinator.generate_uuid
  @definition = definition
  @options = options
  @scope = options.delete(:scope)
  @queue = options.delete(:queue)
  @created_at = Time.now.utc
  @updated_at = created_at
  @current_state = :initial
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



27
28
29
# File 'lib/taskinator/process.rb', line 27

def created_at
  @created_at
end

#definitionObject (readonly)

Returns the value of attribute definition.



23
24
25
# File 'lib/taskinator/process.rb', line 23

def definition
  @definition
end

#optionsObject (readonly)

Returns the value of attribute options.



24
25
26
# File 'lib/taskinator/process.rb', line 24

def options
  @options
end

#parentObject

in the case of sub process tasks, the containing task



31
32
33
# File 'lib/taskinator/process.rb', line 31

def parent
  @parent
end

#queueObject (readonly)

Returns the value of attribute queue.



26
27
28
# File 'lib/taskinator/process.rb', line 26

def queue
  @queue
end

#scopeObject (readonly)

Returns the value of attribute scope.



25
26
27
# File 'lib/taskinator/process.rb', line 25

def scope
  @scope
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



28
29
30
# File 'lib/taskinator/process.rb', line 28

def updated_at
  @updated_at
end

#uuidObject (readonly)

Returns the value of attribute uuid.



22
23
24
# File 'lib/taskinator/process.rb', line 22

def uuid
  @uuid
end

Class Method Details

.define_concurrent_process_for(definition, complete_on = CompleteOn::Default, options = {}) ⇒ Object



17
18
19
# File 'lib/taskinator/process.rb', line 17

def define_concurrent_process_for(definition, complete_on=CompleteOn::Default, options={})
  Process::Concurrent.new(definition, complete_on, options)
end

.define_sequential_process_for(definition, options = {}) ⇒ Object



13
14
15
# File 'lib/taskinator/process.rb', line 13

def define_sequential_process_for(definition, options={})
  Process::Sequential.new(definition, options)
end

Instance Method Details

#<=>(other) ⇒ Object



73
74
75
# File 'lib/taskinator/process.rb', line 73

def <=>(other)
  uuid <=> other.uuid
end

#accept(visitor) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/taskinator/process.rb', line 61

def accept(visitor)
  visitor.visit_attribute(:uuid)
  visitor.visit_task_reference(:parent)
  visitor.visit_type(:definition)
  visitor.visit_tasks(tasks)
  visitor.visit_args(:options)
  visitor.visit_attribute(:scope)
  visitor.visit_attribute(:queue)
  visitor.visit_attribute_time(:created_at)
  visitor.visit_attribute_time(:updated_at)
end

#cancel!Object



141
142
143
144
145
146
147
# File 'lib/taskinator/process.rb', line 141

def cancel!
  transition(:cancelled) do
    instrument('taskinator.process.cancelled', cancelled_payload) do
      cancel if respond_to?(:cancel)
    end
  end
end

#complete!Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/taskinator/process.rb', line 121

def complete!
  transition(:completed) do
    instrument('taskinator.process.completed', completed_payload) do
      complete if respond_to?(:complete)
      # notify the parent task (if there is one) that this process has completed
      # note: parent may be a proxy, so explicity check for nil?
      unless parent.nil?
        parent.complete!
      else
        cleanup
      end
    end
  end
end

#enqueueObject


subclasses must implement the following methods


Raises:

  • (NotImplementedError)


169
170
171
# File 'lib/taskinator/process.rb', line 169

def enqueue
  raise NotImplementedError
end

#enqueue!Object



81
82
83
84
85
86
87
88
89
# File 'lib/taskinator/process.rb', line 81

def enqueue!
  return if paused? || cancelled?

  transition(:enqueued) do
    instrument('taskinator.process.enqueued', enqueued_payload) do
      enqueue
    end
  end
end

#fail!(error) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/taskinator/process.rb', line 149

def fail!(error)
  transition(:failed) do
    instrument('taskinator.process.failed', failed_payload(error)) do
      fail(error) if respond_to?(:fail)
      # notify the parent task (if there is one) that this process has failed
      # note: parent may be a proxy, so explicity check for nil?
      parent.fail!(error) unless parent.nil?
    end
  end
end

#no_tasks_defined?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/taskinator/process.rb', line 57

def no_tasks_defined?
  tasks.empty?
end

#pause!Object



101
102
103
104
105
106
107
108
109
# File 'lib/taskinator/process.rb', line 101

def pause!
  return unless enqueued? || processing?

  transition(:paused) do
    instrument('taskinator.process.paused', paused_payload) do
      pause if respond_to?(:pause)
    end
  end
end

#resume!Object



111
112
113
114
115
116
117
118
119
# File 'lib/taskinator/process.rb', line 111

def resume!
  return unless paused?

  transition(:processing) do
    instrument('taskinator.process.resumed', resumed_payload) do
      resume if respond_to?(:resume)
    end
  end
end

#startObject

Raises:

  • (NotImplementedError)


173
174
175
# File 'lib/taskinator/process.rb', line 173

def start
  raise NotImplementedError
end

#start!Object



91
92
93
94
95
96
97
98
99
# File 'lib/taskinator/process.rb', line 91

def start!
  return if paused? || cancelled?

  transition(:processing) do
    instrument('taskinator.process.processing', processing_payload) do
      start
    end
  end
end

#task_completed(task) ⇒ Object

Raises:

  • (NotImplementedError)


177
178
179
# File 'lib/taskinator/process.rb', line 177

def task_completed(task)
  raise NotImplementedError
end

#task_failed(task, error) ⇒ Object



160
161
162
163
# File 'lib/taskinator/process.rb', line 160

def task_failed(task, error)
  # for now, fail this process
  fail!(error)
end

#tasksObject



53
54
55
# File 'lib/taskinator/process.rb', line 53

def tasks
  @tasks ||= Tasks.new
end

#tasks_completed?Boolean

Returns:

  • (Boolean)


136
137
138
139
# File 'lib/taskinator/process.rb', line 136

def tasks_completed?
  # TODO: optimize this
  tasks.all?(&:completed?)
end

#to_sObject



77
78
79
# File 'lib/taskinator/process.rb', line 77

def to_s
  "#<#{self.class.name}:#{uuid}>"
end