Class: Bricolage::DAO::JobExecution
- Inherits:
-
Object
- Object
- Bricolage::DAO::JobExecution
- Includes:
- SQLUtils
- Defined in:
- lib/bricolage/dao/jobexecution.rb
Defined Under Namespace
Classes: Attributes
Constant Summary collapse
- STATUS_WAITING =
'waiting'.freeze
- STATUS_SUCCEEDED =
'succeeded'.freeze
- STATUS_RUNNING =
'running'.freeze
- STATUS_FAILED =
'failed'.freeze
- STATUS_CANCELED =
'canceled'.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #cancel_jobnet(jobnet_ref, message) ⇒ Object
-
#delete_all ⇒ Object
For tests only.
- #enqueue_job(job, execution_sequence) ⇒ Object
- #enqueued_jobs(jobnet_ref) ⇒ Object
-
#initialize(datasource, connection: nil) ⇒ JobExecution
constructor
A new instance of JobExecution.
- #transition_to_failed(job_execution_id, message) ⇒ Object
- #transition_to_running(job_execution_id) ⇒ Object
- #transition_to_succeeded(job_execution_id) ⇒ Object
Constructor Details
#initialize(datasource, connection: nil) ⇒ JobExecution
Returns a new instance of JobExecution.
28 29 30 31 |
# File 'lib/bricolage/dao/jobexecution.rb', line 28 def initialize(datasource, connection: nil) @datasource = datasource @connection = connection end |
Class Method Details
.for_connection(conn) ⇒ Object
24 25 26 |
# File 'lib/bricolage/dao/jobexecution.rb', line 24 def JobExecution.for_connection(conn) new(nil, connection: conn) end |
.for_record(r) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/bricolage/dao/jobexecution.rb', line 15 def JobExecution.for_record(r) Attributes.new( job_id: r['job_id']&.to_i, job_execution_id: r['job_execution_id']&.to_i, subsystem: r['subsystem'], job_name: r['job_name'] ) end |
Instance Method Details
#cancel_jobnet(jobnet_ref, message) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/bricolage/dao/jobexecution.rb', line 99 def cancel_jobnet(jobnet_ref, ) connect {|conn| records = conn.execute_update(<<~EndSQL) update job_executions set status = #{s STATUS_CANCELED} , message = #{s } , finished_at = now() where job_id in ( select j.job_id from jobs j inner join jobnets n using (jobnet_id) where n.subsystem = #{s jobnet_ref.subsystem} and n.jobnet_name = #{s jobnet_ref.name} ) and status in (#{s STATUS_WAITING}, #{s STATUS_RUNNING}, #{s STATUS_FAILED}) returning job_execution_id ; EndSQL job_execution_ids = records.map {|r| r['job_execution_id'].to_i } unless job_execution_ids.empty? conn.execute_update(<<~EndSQL) insert into job_execution_states ( job_execution_id , job_id , created_at , status , message ) select job_execution_id , job_id , finished_at , status , message from job_executions where job_execution_id in (#{job_execution_ids.join(', ')}) ; EndSQL end } end |
#delete_all ⇒ Object
For tests only
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/bricolage/dao/jobexecution.rb', line 239 def delete_all connect {|conn| conn.execute_update(<<~EndSQL) delete from job_execution_states; delete from job_executions; delete from jobs; delete from jobnets; EndSQL } end |
#enqueue_job(job, execution_sequence) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/bricolage/dao/jobexecution.rb', line 67 def enqueue_job(job, execution_sequence) record = nil connect {|conn| records = conn.execute_update(<<~EndSQL) insert into job_executions ( job_id , execution_sequence , status , message , submitted_at ) values ( #{job.id} , #{execution_sequence} , #{s STATUS_WAITING} , '' , now() ) returning job_execution_id, job_id ; EndSQL record = records.first save_state_transition(conn, record['job_execution_id'], 'submitted_at') } exec = JobExecution.for_record(record) exec.subsystem = job.subsystem exec.job_name = job.job_name exec end |
#enqueued_jobs(jobnet_ref) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/bricolage/dao/jobexecution.rb', line 43 def enqueued_jobs(jobnet_ref) records = connect {|conn| conn.query_rows(<<~EndSQL) select e.job_execution_id , e.job_id , j.subsystem , j.job_name from job_executions e inner join jobs j using (job_id) inner join jobnets n using (jobnet_id) where n.subsystem = #{s jobnet_ref.subsystem} and n.jobnet_name = #{s jobnet_ref.name} and e.status in (#{s STATUS_WAITING}, #{s STATUS_RUNNING}, #{s STATUS_FAILED}) order by e.execution_sequence ; EndSQL } records.map {|r| JobExecution.for_record(r) } end |
#transition_to_failed(job_execution_id, message) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/bricolage/dao/jobexecution.rb', line 193 def transition_to_failed(job_execution_id, ) connect {|conn| records = conn.execute_update(<<~EndSQL) update job_executions set finished_at = now() , status = #{s STATUS_FAILED} , message = #{s } where job_execution_id = #{job_execution_id} and status = #{s STATUS_RUNNING} returning job_execution_id ; EndSQL if records.empty? raise IllegalJobStateException, "could not transition to failed state: job_execution_id=#{job_execution_id}" end save_state_transition(conn, job_execution_id, 'finished_at') } end |
#transition_to_running(job_execution_id) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/bricolage/dao/jobexecution.rb', line 148 def transition_to_running(job_execution_id) connect {|conn| records = conn.execute_update(<<~EndSQL) update job_executions set status = #{s STATUS_RUNNING} , message = '' , started_at = now() , finished_at = null where job_execution_id = #{job_execution_id} and status in (#{s STATUS_WAITING}, #{s STATUS_FAILED}) returning job_execution_id ; EndSQL if records.empty? raise IllegalJobStateException, "Could not run already running job: job_execution_id=#{job_execution_id}" end save_state_transition(conn, job_execution_id, 'started_at') } end |
#transition_to_succeeded(job_execution_id) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/bricolage/dao/jobexecution.rb', line 171 def transition_to_succeeded(job_execution_id) connect {|conn| records = conn.execute_update(<<~EndSQL) update job_executions set finished_at = now() , status = #{s STATUS_SUCCEEDED} , message = '' where job_execution_id = #{job_execution_id} and status = #{s STATUS_RUNNING} returning job_execution_id ; EndSQL if records.empty? raise IllegalJobStateException, "could not transition to succeeded state: job_execution_id=#{job_execution_id}" end save_state_transition(conn, job_execution_id, 'finished_at') } end |