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
20 before_create :create_disabled
21 before_update :update_disabled
23 api_accessible :user, extend: :common do |t|
24 t.add :pipeline_template_uuid
29 t.add :components_summary
35 # Supported states for a pipeline instance
40 (RunningOnServer = 'RunningOnServer'),
41 (RunningOnClient = 'RunningOnClient'),
44 (Complete = 'Complete'),
47 def self.limit_index_columns_read
51 # if all components have input, the pipeline is Ready
52 def components_look_ready?
53 if !self.components || self.components.empty?
57 all_components_have_input = true
58 self.components.each do |name, component|
59 component['script_parameters'].andand.each do |parametername, parameter|
60 parameter = { 'value' => parameter } unless parameter.is_a? Hash
61 if parameter['value'].nil? and parameter['required']
62 if parameter['output_of']
65 all_components_have_input = false
70 return all_components_have_input
77 components['steps'].collect do |step|
79 row = [nrow, step['name']]
80 if step['complete'] and step['complete'] != 0
81 if step['output_data_locator']
92 row << (step['warehousejob']['id'] rescue nil)
93 row << (step['warehousejob']['revision'] rescue nil)
94 row << step['output_data_locator']
95 row << (Time.parse(step['warehousejob']['finishtime']) rescue nil)
105 return 0 if t.size < 1
106 t.collect { |r| r[2] }.inject(0.0) { |sum,a| sum += a } / t.size
110 self.where("state = 'RunningOnServer'")
113 def cancel(cascade: false, need_transaction: true)
114 raise "No longer supported"
118 def bootstrap_components
119 if pipeline_template and (!components or components.empty?)
120 self.components = pipeline_template.components.deep_dup
125 if components and progress_ratio == 1.0
126 self.state = Complete
131 changed_attributes = self.changed
133 if new_record? or 'components'.in? changed_attributes
135 if (self.state == New) and self.components_look_ready?
140 if !self.state.in?(States)
141 errors.add :state, "'#{state.inspect} must be one of: [#{States.join ', '}]"
146 def set_state_before_save
147 if self.components_look_ready? && (!self.state || self.state == New)
152 def update_timestamps_when_state_changes
153 return if not (state_changed? or new_record?)
156 when RunningOnServer, RunningOnClient
157 self.started_at ||= db_current_time
158 when Failed, Complete
159 current_time = db_current_time
160 self.started_at ||= current_time
161 self.finished_at ||= current_time