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, 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)


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

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) || SecureRandom.uuid
  @definition = definition
  @options = options
  @queue = options.delete(:queue)
  @created_at = Time.now.utc
  @updated_at = created_at
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#definitionObject (readonly)

Returns the value of attribute definition.



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

def definition
  @definition
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#parentObject

in the case of sub process tasks, the containing task



34
35
36
# File 'lib/taskinator/process.rb', line 34

def parent
  @parent
end

#queueObject (readonly)

Returns the value of attribute queue.



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

def queue
  @queue
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



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

def updated_at
  @updated_at
end

#uuidObject (readonly)

Returns the value of attribute uuid.



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

def uuid
  @uuid
end

Class Method Details

.base_keyObject



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

def base_key
  'process'
end

.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



67
68
69
# File 'lib/taskinator/process.rb', line 67

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

#accept(visitor) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/taskinator/process.rb', line 56

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(:queue)
  visitor.visit_attribute_time(:created_at)
  visitor.visit_attribute_time(:updated_at)
end

#cancel!Object



131
132
133
134
135
136
137
# File 'lib/taskinator/process.rb', line 131

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

#complete!Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/taskinator/process.rb', line 115

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?
      parent.complete! unless parent.nil?
    end
  end
end

#enqueueObject


subclasses must implement the following methods


Raises:

  • (NotImplementedError)


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

def enqueue
  raise NotImplementedError
end

#enqueue!Object



75
76
77
78
79
80
81
82
83
# File 'lib/taskinator/process.rb', line 75

def enqueue!
  return if paused? || cancelled?

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

#fail!(error) ⇒ Object



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

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)


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

def no_tasks_defined?
  tasks.empty?
end

#pause!Object



95
96
97
98
99
100
101
102
103
# File 'lib/taskinator/process.rb', line 95

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



105
106
107
108
109
110
111
112
113
# File 'lib/taskinator/process.rb', line 105

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)


163
164
165
# File 'lib/taskinator/process.rb', line 163

def start
  raise NotImplementedError
end

#start!Object



85
86
87
88
89
90
91
92
93
# File 'lib/taskinator/process.rb', line 85

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)


167
168
169
# File 'lib/taskinator/process.rb', line 167

def task_completed(task)
  raise NotImplementedError
end

#task_failed(task, error) ⇒ Object



150
151
152
153
# File 'lib/taskinator/process.rb', line 150

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

#tasksObject



48
49
50
# File 'lib/taskinator/process.rb', line 48

def tasks
  @tasks ||= Tasks.new
end

#tasks_completed?Boolean

Returns:

  • (Boolean)


126
127
128
129
# File 'lib/taskinator/process.rb', line 126

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

#to_sObject



71
72
73
# File 'lib/taskinator/process.rb', line 71

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