Class: Gush::Workflow
- Inherits:
-
Object
- Object
- Gush::Workflow
- Defined in:
- lib/gush/workflow.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#id ⇒ Object
Returns the value of attribute id.
-
#jobs ⇒ Object
Returns the value of attribute jobs.
-
#persisted ⇒ Object
Returns the value of attribute persisted.
-
#stopped ⇒ Object
Returns the value of attribute stopped.
Class Method Summary collapse
Instance Method Summary collapse
- #configure(*args) ⇒ Object
- #failed? ⇒ Boolean
- #find_job(name) ⇒ Object
- #finished? ⇒ Boolean
- #finished_at ⇒ Object
- #initial_jobs ⇒ Object
-
#initialize(*args) ⇒ Workflow
constructor
A new instance of Workflow.
- #mark_as_persisted ⇒ Object
- #mark_as_started ⇒ Object
- #mark_as_stopped ⇒ Object
- #persist! ⇒ Object
- #reload ⇒ Object
- #resolve_dependencies ⇒ Object
- #run(klass, opts = {}) ⇒ Object
- #running? ⇒ Boolean
- #save ⇒ Object
- #start! ⇒ Object
- #started? ⇒ Boolean
- #started_at ⇒ Object
- #status ⇒ Object
- #stopped? ⇒ Boolean
- #to_hash ⇒ Object
- #to_json(options = {}) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Workflow
Returns a new instance of Workflow.
7 8 9 10 11 12 13 14 |
# File 'lib/gush/workflow.rb', line 7 def initialize(*args) @id = id @jobs = [] @dependencies = [] @persisted = false @stopped = false @arguments = args end |
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments.
5 6 7 |
# File 'lib/gush/workflow.rb', line 5 def arguments @arguments end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/gush/workflow.rb', line 5 def id @id end |
#jobs ⇒ Object
Returns the value of attribute jobs.
5 6 7 |
# File 'lib/gush/workflow.rb', line 5 def jobs @jobs end |
#persisted ⇒ Object
Returns the value of attribute persisted.
5 6 7 |
# File 'lib/gush/workflow.rb', line 5 def persisted @persisted end |
#stopped ⇒ Object
Returns the value of attribute stopped.
5 6 7 |
# File 'lib/gush/workflow.rb', line 5 def stopped @stopped end |
Class Method Details
.create(*args) ⇒ Object
20 21 22 23 24 |
# File 'lib/gush/workflow.rb', line 20 def self.create(*args) flow = new(*args) flow.save flow end |
.descendants ⇒ Object
162 163 164 |
# File 'lib/gush/workflow.rb', line 162 def self.descendants ObjectSpace.each_object(Class).select { |klass| klass < self } end |
.find(id) ⇒ Object
16 17 18 |
# File 'lib/gush/workflow.rb', line 16 def self.find(id) Gush::Client.new.find_workflow(id) end |
Instance Method Details
#configure(*args) ⇒ Object
32 33 |
# File 'lib/gush/workflow.rb', line 32 def configure(*args) end |
#failed? ⇒ Boolean
81 82 83 |
# File 'lib/gush/workflow.rb', line 81 def failed? jobs.any?(&:failed?) end |
#find_job(name) ⇒ Object
65 66 67 |
# File 'lib/gush/workflow.rb', line 65 def find_job(name) jobs.find { |node| node.name == name.to_s || node.class.to_s == name.to_s } end |
#finished? ⇒ Boolean
69 70 71 |
# File 'lib/gush/workflow.rb', line 69 def finished? jobs.all?(&:finished?) end |
#finished_at ⇒ Object
137 138 139 |
# File 'lib/gush/workflow.rb', line 137 def finished_at last_job ? last_job.finished_at : nil end |
#initial_jobs ⇒ Object
114 115 116 |
# File 'lib/gush/workflow.rb', line 114 def initial_jobs jobs.select(&:has_no_dependencies?) end |
#mark_as_persisted ⇒ Object
47 48 49 |
# File 'lib/gush/workflow.rb', line 47 def mark_as_persisted @persisted = true end |
#mark_as_started ⇒ Object
51 52 53 |
# File 'lib/gush/workflow.rb', line 51 def mark_as_started @stopped = false end |
#mark_as_stopped ⇒ Object
35 36 37 |
# File 'lib/gush/workflow.rb', line 35 def mark_as_stopped @stopped = true end |
#persist! ⇒ Object
43 44 45 |
# File 'lib/gush/workflow.rb', line 43 def persist! client.persist_workflow(self) end |
#reload ⇒ Object
110 111 112 |
# File 'lib/gush/workflow.rb', line 110 def reload self.class.find(id) end |
#resolve_dependencies ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/gush/workflow.rb', line 55 def resolve_dependencies @dependencies.each do |dependency| from = find_job(dependency[:from]) to = find_job(dependency[:to]) to.incoming << dependency[:from] from.outgoing << dependency[:to] end end |
#run(klass, opts = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/gush/workflow.rb', line 89 def run(klass, opts = {}) = node = klass.new(self, { name: klass.to_s, params: opts.fetch(:params, {}) }) jobs << node deps_after = [*opts[:after]] deps_after.each do |dep| @dependencies << {from: dep.to_s, to: klass.to_s } end deps_before = [*opts[:before]] deps_before.each do |dep| @dependencies << {from: klass.to_s, to: dep.to_s } end end |
#running? ⇒ Boolean
77 78 79 |
# File 'lib/gush/workflow.rb', line 77 def running? started? && !finished? end |
#save ⇒ Object
26 27 28 29 30 |
# File 'lib/gush/workflow.rb', line 26 def save configure(*@arguments) resolve_dependencies client.persist_workflow(self) end |
#start! ⇒ Object
39 40 41 |
# File 'lib/gush/workflow.rb', line 39 def start! client.start_workflow(self) end |
#started? ⇒ Boolean
73 74 75 |
# File 'lib/gush/workflow.rb', line 73 def started? !!started_at end |
#started_at ⇒ Object
133 134 135 |
# File 'lib/gush/workflow.rb', line 133 def started_at first_job ? first_job.started_at : nil end |
#status ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/gush/workflow.rb', line 118 def status case when failed? :failed when running? :running when finished? :finished when stopped? :stopped else :running end end |
#stopped? ⇒ Boolean
85 86 87 |
# File 'lib/gush/workflow.rb', line 85 def stopped? stopped end |
#to_hash ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/gush/workflow.rb', line 141 def to_hash name = self.class.to_s { name: name, id: id, arguments: @arguments, total: jobs.count, finished: jobs.count(&:finished?), klass: name, jobs: jobs.map(&:as_json), status: status, stopped: stopped, started_at: started_at, finished_at: finished_at } end |