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, {
13 foreign_key: :pipeline_template_uuid,
18 before_validation :bootstrap_components
19 before_validation :update_state
20 before_validation :verify_status
21 before_validation :update_timestamps_when_state_changes
22 before_create :set_state_before_save
23 before_save :set_state_before_save
24 before_create :create_disabled
25 before_update :update_disabled
27 api_accessible :user, extend: :common do |t|
28 t.add :pipeline_template_uuid
33 t.add :components_summary
39 # Supported states for a pipeline instance
44 (RunningOnServer = 'RunningOnServer'),
45 (RunningOnClient = 'RunningOnClient'),
48 (Complete = 'Complete'),
51 def self.limit_index_columns_read
55 # if all components have input, the pipeline is Ready
56 def components_look_ready?
57 if !self.components || self.components.empty?
61 all_components_have_input = true
62 self.components.each do |name, component|
63 component['script_parameters'].andand.each do |parametername, parameter|
64 parameter = { 'value' => parameter } unless parameter.is_a? Hash
65 if parameter['value'].nil? and parameter['required']
66 if parameter['output_of']
69 all_components_have_input = false
74 return all_components_have_input
81 components['steps'].collect do |step|
83 row = [nrow, step['name']]
84 if step['complete'] and step['complete'] != 0
85 if step['output_data_locator']
96 row << (step['warehousejob']['id'] rescue nil)
97 row << (step['warehousejob']['revision'] rescue nil)
98 row << step['output_data_locator']
99 row << (Time.parse(step['warehousejob']['finishtime']) rescue nil)
109 return 0 if t.size < 1
110 t.collect { |r| r[2] }.inject(0.0) { |sum,a| sum += a } / t.size
114 self.where("state = 'RunningOnServer'")
117 def cancel(cascade: false, need_transaction: true)
118 raise "No longer supported"
122 def bootstrap_components
123 if pipeline_template and (!components or components.empty?)
124 self.components = pipeline_template.components.deep_dup
129 if components and progress_ratio == 1.0
130 self.state = Complete
135 changed_attributes = self.changed
137 if new_record? or 'components'.in? changed_attributes
139 if (self.state == New) and self.components_look_ready?
144 if !self.state.in?(States)
145 errors.add :state, "'#{state.inspect} must be one of: [#{States.join ', '}]"
150 def set_state_before_save
151 if self.components_look_ready? && (!self.state || self.state == New)
156 def update_timestamps_when_state_changes
157 return if not (state_changed? or new_record?)
160 when RunningOnServer, RunningOnClient
161 self.started_at ||= db_current_time
162 when Failed, Complete
163 current_time = db_current_time
164 self.started_at ||= current_time
165 self.finished_at ||= current_time