1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class PipelineInstance < ArvadosModel
8 include CommonApiTemplate
9 serialize :components, Hash
10 serialize :properties, Hash
11 serialize :components_summary, Hash
12 belongs_to :pipeline_template, :foreign_key => :pipeline_template_uuid, :primary_key => :uuid
14 before_validation :bootstrap_components
15 before_validation :update_state
16 before_validation :verify_status
17 before_validation :update_timestamps_when_state_changes
18 before_create :set_state_before_save
19 before_save :set_state_before_save
21 api_accessible :user, extend: :common do |t|
22 t.add :pipeline_template_uuid
27 t.add :components_summary
33 # Supported states for a pipeline instance
38 (RunningOnServer = 'RunningOnServer'),
39 (RunningOnClient = 'RunningOnClient'),
42 (Complete = 'Complete'),
45 def self.limit_index_columns_read
49 # if all components have input, the pipeline is Ready
50 def components_look_ready?
51 if !self.components || self.components.empty?
55 all_components_have_input = true
56 self.components.each do |name, component|
57 component['script_parameters'].andand.each do |parametername, parameter|
58 parameter = { 'value' => parameter } unless parameter.is_a? Hash
59 if parameter['value'].nil? and parameter['required']
60 if parameter['output_of']
63 all_components_have_input = false
68 return all_components_have_input
75 components['steps'].collect do |step|
77 row = [nrow, step['name']]
78 if step['complete'] and step['complete'] != 0
79 if step['output_data_locator']
90 row << (step['warehousejob']['id'] rescue nil)
91 row << (step['warehousejob']['revision'] rescue nil)
92 row << step['output_data_locator']
93 row << (Time.parse(step['warehousejob']['finishtime']) rescue nil)
103 return 0 if t.size < 1
104 t.collect { |r| r[2] }.inject(0.0) { |sum,a| sum += a } / t.size
108 self.where("state = 'RunningOnServer'")
111 def cancel(cascade: false, need_transaction: true)
113 ActiveRecord::Base.transaction do
114 cancel(cascade: cascade, need_transaction: false)
119 if self.state.in?([RunningOnServer, RunningOnClient])
122 elsif self.state != Paused
123 raise InvalidStateTransitionError
128 # cancel all child jobs
129 children = self.components.andand.collect{|_, c| c['job']}.compact.collect{|j| j['uuid']}.compact
131 return if children.empty?
133 Job.where(uuid: children, state: [Job::Queued, Job::Running]).each do |job|
134 job.cancel(cascade: cascade, need_transaction: false)
139 def bootstrap_components
140 if pipeline_template and (!components or components.empty?)
141 self.components = pipeline_template.components.deep_dup
146 if components and progress_ratio == 1.0
147 self.state = Complete
152 changed_attributes = self.changed
154 if new_record? or 'components'.in? changed_attributes
156 if (self.state == New) and self.components_look_ready?
161 if self.state.in?(States)
164 errors.add :state, "'#{state.inspect} must be one of: [#{States.join ', '}]"
169 def set_state_before_save
170 if self.components_look_ready? && (!self.state || self.state == New)
175 def update_timestamps_when_state_changes
176 return if not (state_changed? or new_record?)
179 when RunningOnServer, RunningOnClient
180 self.started_at ||= db_current_time
181 when Failed, Complete
182 current_time = db_current_time
183 self.started_at ||= current_time
184 self.finished_at ||= current_time