Class: Dwf::Workflow
- Inherits:
-
Object
- Object
- Dwf::Workflow
- Defined in:
- lib/dwf/workflow.rb
Constant Summary collapse
- CALLBACK_TYPES =
[ BUILD_IN = 'build-in', SK_BATCH = 'sk-batch' ].freeze
Instance Attribute Summary collapse
-
#callback_type ⇒ Object
readonly
Returns the value of attribute callback_type.
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
#finished_at ⇒ Object
readonly
Returns the value of attribute finished_at.
-
#jobs ⇒ Object
readonly
Returns the value of attribute jobs.
-
#persisted ⇒ Object
readonly
Returns the value of attribute persisted.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#stopped ⇒ Object
readonly
Returns the value of attribute stopped.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
- #cb_build_in? ⇒ Boolean
- #configure ⇒ Object
- #failed? ⇒ Boolean
- #find_job(name) ⇒ Object
- #finished? ⇒ Boolean
- #id ⇒ Object
-
#initialize(options = {}) ⇒ Workflow
constructor
A new instance of Workflow.
- #mark_as_persisted ⇒ Object
- #mark_as_started ⇒ Object
- #run(klass, options = {}) ⇒ Object
- #running? ⇒ Boolean
- #save ⇒ Object
- #start! ⇒ Object
- #started? ⇒ Boolean
- #status ⇒ Object
- #stopped? ⇒ Boolean
- #to_hash ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Workflow
Returns a new instance of Workflow.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/dwf/workflow.rb', line 24 def initialize( = {}) @dependencies = [] @id = id @jobs = [] @persisted = false @stopped = false @callback_type = [:callback_type] || BUILD_IN setup end |
Instance Attribute Details
#callback_type ⇒ Object (readonly)
Returns the value of attribute callback_type.
13 14 15 |
# File 'lib/dwf/workflow.rb', line 13 def callback_type @callback_type end |
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
13 14 15 |
# File 'lib/dwf/workflow.rb', line 13 def dependencies @dependencies end |
#finished_at ⇒ Object (readonly)
Returns the value of attribute finished_at.
13 14 15 |
# File 'lib/dwf/workflow.rb', line 13 def finished_at @finished_at end |
#jobs ⇒ Object (readonly)
Returns the value of attribute jobs.
13 14 15 |
# File 'lib/dwf/workflow.rb', line 13 def jobs @jobs end |
#persisted ⇒ Object (readonly)
Returns the value of attribute persisted.
13 14 15 |
# File 'lib/dwf/workflow.rb', line 13 def persisted @persisted end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
13 14 15 |
# File 'lib/dwf/workflow.rb', line 13 def started_at @started_at end |
#stopped ⇒ Object (readonly)
Returns the value of attribute stopped.
13 14 15 |
# File 'lib/dwf/workflow.rb', line 13 def stopped @stopped end |
Class Method Details
.create(*args) ⇒ Object
17 18 19 20 21 |
# File 'lib/dwf/workflow.rb', line 17 def create(*args) flow = new(*args) flow.save flow end |
Instance Method Details
#as_json ⇒ Object
102 103 104 |
# File 'lib/dwf/workflow.rb', line 102 def as_json to_hash.to_json end |
#cb_build_in? ⇒ Boolean
48 49 50 |
# File 'lib/dwf/workflow.rb', line 48 def cb_build_in? callback_type == BUILD_IN end |
#configure ⇒ Object
56 |
# File 'lib/dwf/workflow.rb', line 56 def configure; end |
#failed? ⇒ Boolean
118 119 120 |
# File 'lib/dwf/workflow.rb', line 118 def failed? jobs.any?(&:failed?) end |
#find_job(name) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/dwf/workflow.rb', line 73 def find_job(name) match_data = /(?<klass>\w*[^-])-(?<identifier>.*)/.match(name.to_s) if match_data.nil? job = jobs.find { |node| node.klass.to_s == name.to_s } else job = jobs.find { |node| node.name.to_s == name.to_s } end job end |
#finished? ⇒ Boolean
106 107 108 |
# File 'lib/dwf/workflow.rb', line 106 def finished? jobs.all?(&:finished?) end |
#id ⇒ Object
52 53 54 |
# File 'lib/dwf/workflow.rb', line 52 def id @id ||= client.build_workflow_id end |
#mark_as_persisted ⇒ Object
135 136 137 |
# File 'lib/dwf/workflow.rb', line 135 def mark_as_persisted @persisted = true end |
#mark_as_started ⇒ Object
139 140 141 |
# File 'lib/dwf/workflow.rb', line 139 def mark_as_started @stopped = false end |
#run(klass, options = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/dwf/workflow.rb', line 58 def run(klass, = {}) node = klass.new( workflow_id: id, id: client.build_job_id(id, klass.to_s), params: .fetch(:params, {}), queue: [:queue], callback_type: callback_type ) jobs << node build_dependencies_structure(node, ) node.name end |
#running? ⇒ Boolean
114 115 116 |
# File 'lib/dwf/workflow.rb', line 114 def running? started? && !finished? end |
#save ⇒ Object
41 42 43 44 45 46 |
# File 'lib/dwf/workflow.rb', line 41 def save client.persist_workflow(self) jobs.each(&:persist!) mark_as_persisted true end |
#start! ⇒ Object
35 36 37 38 39 |
# File 'lib/dwf/workflow.rb', line 35 def start! initial_jobs.each do |job| cb_build_in? ? job.persist_and_perform_async! : Dwf::Callback.new.start(job) end end |
#started? ⇒ Boolean
110 111 112 |
# File 'lib/dwf/workflow.rb', line 110 def started? !!started_at end |
#status ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/dwf/workflow.rb', line 126 def status return :failed if failed? return :running if running? return :finished if finished? return :stopped if stopped? :running end |
#stopped? ⇒ Boolean
122 123 124 |
# File 'lib/dwf/workflow.rb', line 122 def stopped? stopped end |
#to_hash ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/dwf/workflow.rb', line 85 def to_hash name = self.class.to_s { name: name, id: id, arguments: @arguments, total: jobs.count, finished: jobs.count(&:finished?), klass: name, status: status, stopped: stopped, started_at: started_at, finished_at: finished_at, callback_type: callback_type } end |