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',
17 before_validation :bootstrap_components
18 before_validation :update_state
19 before_validation :verify_status
20 before_validation :update_timestamps_when_state_changes
21 before_create :set_state_before_save
22 before_save :set_state_before_save
23 before_create :create_disabled
24 before_update :update_disabled
26 api_accessible :user, extend: :common do |t|
27 t.add :pipeline_template_uuid
32 t.add :components_summary
38 # Supported states for a pipeline instance
43 (RunningOnServer = 'RunningOnServer'),
44 (RunningOnClient = 'RunningOnClient'),
47 (Complete = 'Complete'),
50 def self.limit_index_columns_read
54 # if all components have input, the pipeline is Ready
55 def components_look_ready?
56 if !self.components || self.components.empty?
60 all_components_have_input = true
61 self.components.each do |name, component|
62 component['script_parameters'].andand.each do |parametername, parameter|
63 parameter = { 'value' => parameter } unless parameter.is_a? Hash
64 if parameter['value'].nil? and parameter['required']
65 if parameter['output_of']
68 all_components_have_input = false
73 return all_components_have_input
80 components['steps'].collect do |step|
82 row = [nrow, step['name']]
83 if step['complete'] and step['complete'] != 0
84 if step['output_data_locator']
95 row << (step['warehousejob']['id'] rescue nil)
96 row << (step['warehousejob']['revision'] rescue nil)
97 row << step['output_data_locator']
98 row << (Time.parse(step['warehousejob']['finishtime']) rescue nil)
108 return 0 if t.size < 1
109 t.collect { |r| r[2] }.inject(0.0) { |sum,a| sum += a } / t.size
113 self.where("state = 'RunningOnServer'")
116 def cancel(cascade: false, need_transaction: true)
117 raise "No longer supported"
121 def bootstrap_components
122 if pipeline_template and (!components or components.empty?)
123 self.components = pipeline_template.components.deep_dup
128 if components and progress_ratio == 1.0
129 self.state = Complete
134 changed_attributes = self.changed
136 if new_record? or 'components'.in? changed_attributes
138 if (self.state == New) and self.components_look_ready?
143 if !self.state.in?(States)
144 errors.add :state, "'#{state.inspect} must be one of: [#{States.join ', '}]"
149 def set_state_before_save
150 if self.components_look_ready? && (!self.state || self.state == New)
155 def update_timestamps_when_state_changes
156 return if not (state_changed? or new_record?)
159 when RunningOnServer, RunningOnClient
160 self.started_at ||= db_current_time
161 when Failed, Complete
162 current_time = db_current_time
163 self.started_at ||= current_time
164 self.finished_at ||= current_time