Class: Burstflow::Workflow::Builder
- Inherits:
-
Object
- Object
- Burstflow::Workflow::Builder
- Defined in:
- lib/burstflow/workflow/builder.rb
Instance Method Summary collapse
- #as_json ⇒ Object
- #find_job(id_or_klass) ⇒ Object
-
#initialize(workflow, *args, &block) ⇒ Builder
constructor
A new instance of Builder.
- #resolve_dependencies ⇒ Object
- #run(klass, opts = {}) ⇒ Object
Constructor Details
#initialize(workflow, *args, &block) ⇒ Builder
Returns a new instance of Builder.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/burstflow/workflow/builder.rb', line 5 def initialize(workflow, *args, &block) @workflow = workflow @deps = [] @jobs_by_class = {} @jobs_by_id = {} @workflow.jobs_config.each_pair do |id, job_hash| job = Burstflow::Job.from_hash(@workflow, job_hash) @jobs_by_class[job.klass.to_s] ||= [] @jobs_by_class[job.klass.to_s] << job @jobs_by_id[id] = job job.incoming.each do |from| @deps << { from: from, to: id } end job.outgoing.each do |to| @deps << { from: id, to: to } end @deps.uniq! end instance_exec *args, &block resolve_dependencies end |
Instance Method Details
#as_json ⇒ Object
86 87 88 89 90 |
# File 'lib/burstflow/workflow/builder.rb', line 86 def as_json @jobs_by_id.each_with_object({}) do |(_id, job), json| json[job.id] = job.as_json end end |
#find_job(id_or_klass) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/burstflow/workflow/builder.rb', line 58 def find_job(id_or_klass) id = if @jobs_by_id.key?(id_or_klass) id_or_klass else jobs = @jobs_by_class[id_or_klass.to_s] raise "No job with #{id_or_klass} klass or id found" if jobs.count == 0 raise "Duplicated jobs with #{id_or_klass} klass or id detected" if jobs.count > 1 jobs.first.id end @jobs_by_id[id] end |
#resolve_dependencies ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/burstflow/workflow/builder.rb', line 73 def resolve_dependencies @deps.each do |dependency| from = find_job(dependency[:from].to_s) to = find_job(dependency[:to].to_s) to.incoming << from.id from.outgoing << to.id to.incoming.uniq! from.outgoing.uniq! end end |
#run(klass, opts = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/burstflow/workflow/builder.rb', line 32 def run(klass, opts = {}) opts = opts.with_indifferent_access before_deps = opts.delete(:before) || [] after_deps = opts.delete(:after) || [] job = klass.new(@workflow, opts) [*before_deps].each do |dep| @deps << { from: job.id, to: dep.to_s } end [*after_deps].each do |dep| @deps << { from: dep.to_s, to: job.id } end @jobs_by_class[klass.to_s] ||= [] @jobs_by_class[klass.to_s] << job raise 'Job id duplication' if @jobs_by_id.key?(job.id) @jobs_by_id[job.id] = job job.id end |