X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/bb19e060336c4e734b3e1922c5be3c4b40ff7da8..e2f3f8e7347a8ded697d3c0127f82325d672e7ab:/services/api/app/models/pipeline_instance.rb?ds=inline diff --git a/services/api/app/models/pipeline_instance.rb b/services/api/app/models/pipeline_instance.rb index 77a0736b00..75903ca7d0 100644 --- a/services/api/app/models/pipeline_instance.rb +++ b/services/api/app/models/pipeline_instance.rb @@ -10,6 +10,7 @@ class PipelineInstance < ArvadosModel before_validation :bootstrap_components before_validation :update_state before_validation :verify_status + before_validation :update_timestamps_when_state_changes before_create :set_state_before_save before_save :set_state_before_save @@ -99,6 +100,33 @@ class PipelineInstance < ArvadosModel self.where("state = 'RunningOnServer'") end + def cancel(cascade: false, need_transaction: true) + if need_transaction + ActiveRecord::Base.transaction do + cancel(cascade: cascade, need_transaction: false) + end + return + end + + if self.state.in?([RunningOnServer, RunningOnClient]) + self.state = Paused + self.save! + elsif self.state != Paused + raise InvalidStateTransitionError + end + + return if !cascade + + # cancel all child jobs + children = self.components.andand.collect{|_, c| c['job']}.compact.collect{|j| j['uuid']}.compact + + return if children.empty? + + Job.where(uuid: children, state: [Job::Queued, Job::Running]).each do |job| + job.cancel(cascade: cascade, need_transaction: false) + end + end + protected def bootstrap_components if pipeline_template and (!components or components.empty?) @@ -136,4 +164,17 @@ class PipelineInstance < ArvadosModel end end + def update_timestamps_when_state_changes + return if not (state_changed? or new_record?) + + case state + when RunningOnServer, RunningOnClient + self.started_at ||= db_current_time + when Failed, Complete + current_time = db_current_time + self.started_at ||= current_time + self.finished_at ||= current_time + end + end + end