lib/gush/workflow.rb in gush-3.0.0 vs lib/gush/workflow.rb in gush-4.0.0

- old
+ new

@@ -1,28 +1,36 @@ require 'securerandom' module Gush class Workflow - attr_accessor :id, :jobs, :stopped, :persisted, :arguments + attr_accessor :jobs, :dependencies, :stopped, :persisted, :arguments, :kwargs, :globals + attr_writer :id - def initialize(*args) - @id = id - @jobs = [] - @dependencies = [] - @persisted = false - @stopped = false + def initialize(*args, globals: nil, internal_state: {}, **kwargs) @arguments = args + @kwargs = kwargs + @globals = globals || {} - setup + @id = internal_state[:id] || id + @jobs = internal_state[:jobs] || [] + @dependencies = internal_state[:dependencies] || [] + @persisted = internal_state[:persisted] || false + @stopped = internal_state[:stopped] || false + + setup unless internal_state[:skip_setup] end def self.find(id) Gush::Client.new.find_workflow(id) end - def self.create(*args) - flow = new(*args) + def self.page(start=0, stop=99, order: :asc) + Gush::Client.new.workflows(start, stop, order: order) + end + + def self.create(*args, **kwargs) + flow = new(*args, **kwargs) flow.save flow end def continue @@ -36,11 +44,11 @@ def save persist! end - def configure(*args) + def configure(*args, **kwargs) end def mark_as_stopped @stopped = true end @@ -51,11 +59,11 @@ def persist! client.persist_workflow(self) end - def expire! (ttl=nil) + def expire!(ttl=nil) client.expire_workflow(self, ttl) end def mark_as_persisted @persisted = true @@ -109,11 +117,11 @@ def run(klass, opts = {}) node = klass.new({ workflow_id: id, id: client.next_free_job_id(id, klass.to_s), - params: opts.fetch(:params, {}), + params: (@globals || {}).merge(opts.fetch(:params, {})), queue: opts[:queue], wait: opts[:wait] }) jobs << node @@ -155,11 +163,11 @@ when finished? :finished when stopped? :stopped else - :running + :pending end end def started_at first_job ? first_job.started_at : nil @@ -173,13 +181,17 @@ name = self.class.to_s { name: name, id: id, arguments: @arguments, + kwargs: @kwargs, + globals: @globals, + dependencies: @dependencies, total: jobs.count, finished: jobs.count(&:finished?), klass: name, + job_klasses: jobs.map(&:class).map(&:to_s).uniq, status: status, stopped: stopped, started_at: started_at, finished_at: finished_at } @@ -198,10 +210,10 @@ end private def setup - configure(*@arguments) + configure(*@arguments, **@kwargs) resolve_dependencies end def client @client ||= Client.new