Class: Burst::Workflow

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Builder, WorkflowHelper
Defined in:
lib/burst/workflow.rb

Constant Summary collapse

INITIAL =
'initial'.freeze
RUNNING =
'running'.freeze
FINISHED =
'finished'.freeze
FAILED =
'failed'.freeze
SUSPENDED =
'suspended'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#job_cacheObject

Returns the value of attribute job_cache.



14
15
16
# File 'lib/burst/workflow.rb', line 14

def job_cache
  @job_cache
end

#managerObject

Returns the value of attribute manager.



14
15
16
# File 'lib/burst/workflow.rb', line 14

def manager
  @manager
end

Class Method Details

.build(*args) ⇒ Object



38
39
40
41
42
43
# File 'lib/burst/workflow.rb', line 38

def self.build(*args)
  wf = new
  wf.configure(*args)
  wf.resolve_dependencies
  wf
end

Instance Method Details

#all_jobsObject



97
98
99
100
101
102
103
# File 'lib/burst/workflow.rb', line 97

def all_jobs
  Enumerator.new do |y|
    jobs.keys.each do |id|
      y << get_job(id)
    end
  end
end

#attributesObject



29
30
31
32
33
34
35
36
# File 'lib/burst/workflow.rb', line 29

def attributes
  {
    id: self.id,
    jobs: self.jobs,
    klass: self.klass,
    status: status
  }
end

#failed?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/burst/workflow.rb', line 89

def failed?
  all_jobs.any?(&:failed?)
end

#find_job(id_or_klass) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/burst/workflow.rb', line 123

def find_job(id_or_klass)
  id = if jobs.key?(id_or_klass)
         id_or_klass
       else
         find_id_by_klass(id_or_klass)
  end

  get_job(id)
end

#finished?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/burst/workflow.rb', line 77

def finished?
  all_jobs.all?(&:finished?)
end

#get_job(id) ⇒ Object



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

def get_job(id)
  if job = @job_cache[id]
    job
  else
    job = Burst::Job.from_hash(self, jobs[id].deep_dup)
    @job_cache[job.id] = job
    job
  end
end

#get_job_hash(id) ⇒ Object



133
134
135
# File 'lib/burst/workflow.rb', line 133

def get_job_hash(id)
  jobs[id]
end

#initial?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/burst/workflow.rb', line 73

def initial?
  status == INITIAL
end

#initial_jobsObject



119
120
121
# File 'lib/burst/workflow.rb', line 119

def initial_jobs
  all_jobs.select(&:initial?)
end

#reload(options = nil) ⇒ Object



45
46
47
48
# File 'lib/burst/workflow.rb', line 45

def reload(options = nil)
  self.job_cache = {}
  super
end

#resume!(job_id, data) ⇒ Object



55
56
57
# File 'lib/burst/workflow.rb', line 55

def resume!(job_id, data)
  manager.resume!(get_job(job_id), data)
end

#running?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/burst/workflow.rb', line 85

def running?
  started? && !finished?
end

#set_job(job) ⇒ Object



115
116
117
# File 'lib/burst/workflow.rb', line 115

def set_job(job)
  jobs[job.id] = job.as_json
end

#start!Object



50
51
52
53
# File 'lib/burst/workflow.rb', line 50

def start!
  save!
  manager.start
end

#started?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/burst/workflow.rb', line 81

def started?
  !!started_at
end

#statusObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/burst/workflow.rb', line 59

def status
  if failed?
    FAILED
  elsif suspended?
    SUSPENDED
  elsif running?
    RUNNING
  elsif finished?
    FINISHED
  else
    INITIAL
  end
end

#suspended?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/burst/workflow.rb', line 93

def suspended?
  !failed? && all_jobs.any?(&:suspended?)
end