X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/2d4263c16812a906589cbc13be26535a85691bd8..2eb7a28fa7900a005bf48dc40dd1af16d0bc455d:/services/api/app/models/pipeline_instance.rb diff --git a/services/api/app/models/pipeline_instance.rb b/services/api/app/models/pipeline_instance.rb index 6de861d005..f84c4a310f 100644 --- a/services/api/app/models/pipeline_instance.rb +++ b/services/api/app/models/pipeline_instance.rb @@ -1,27 +1,63 @@ -class PipelineInstance < OrvosModel - include AssignUuid +class PipelineInstance < ArvadosModel + include HasUuid include KindAndEtag include CommonApiTemplate serialize :components, Hash serialize :properties, Hash + serialize :components_summary, Hash belongs_to :pipeline_template, :foreign_key => :pipeline_template_uuid, :primary_key => :uuid - attr_accessor :pipeline_template before_validation :bootstrap_components - before_validation :update_success + 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 - api_accessible :superuser, :extend => :common do |t| + api_accessible :user, extend: :common do |t| t.add :pipeline_template_uuid - t.add :pipeline_template, :if => :pipeline_template t.add :name t.add :components - t.add :success - t.add :active - t.add :dependencies + t.add :properties + t.add :state + t.add :components_summary + t.add :description + t.add :started_at + t.add :finished_at end - def dependencies - dependency_search(self.components).keys + # Supported states for a pipeline instance + States = + [ + (New = 'New'), + (Ready = 'Ready'), + (RunningOnServer = 'RunningOnServer'), + (RunningOnClient = 'RunningOnClient'), + (Paused = 'Paused'), + (Failed = 'Failed'), + (Complete = 'Complete'), + ] + + # if all components have input, the pipeline is Ready + def components_look_ready? + if !self.components || self.components.empty? + return false + end + + all_components_have_input = true + self.components.each do |name, component| + component['script_parameters'].andand.each do |parametername, parameter| + parameter = { 'value' => parameter } unless parameter.is_a? Hash + if parameter['value'].nil? and parameter['required'] + if parameter['output_of'] + next + end + all_components_have_input = false + break + end + end + end + return all_components_have_input end def progress_table @@ -40,7 +76,7 @@ class PipelineInstance < OrvosModel else row << 0.0 if step['failed'] - self.success = false + self.state = Failed end end row << (step['warehousejob']['id'] rescue nil) @@ -60,40 +96,58 @@ class PipelineInstance < OrvosModel t.collect { |r| r[2] }.inject(0.0) { |sum,a| sum += a } / t.size end + def self.queue + self.where("state = 'RunningOnServer'") + end + protected def bootstrap_components if pipeline_template and (!components or components.empty?) - self.components = pipeline_template.components + self.components = pipeline_template.components.deep_dup end end - def update_success + def update_state if components and progress_ratio == 1.0 - self.success = true + self.state = Complete end end - def dependency_search(haystack) - if haystack.is_a? String - if (re = haystack.match /^([0-9a-f]{32}(\+[^,]+)*)+/) - {re[1] => true} - else - {} - end - elsif haystack.is_a? Array - deps = {} - haystack.each do |value| - deps.merge! dependency_search(value) - end - deps - elsif haystack.respond_to? :keys - deps = {} - haystack.each do |key, value| - deps.merge! dependency_search(value) + def verify_status + changed_attributes = self.changed + + if new_record? or 'components'.in? changed_attributes + self.state ||= New + if (self.state == New) and self.components_look_ready? + self.state = Ready end - deps + end + + if self.state.in?(States) + true else - {} + errors.add :state, "'#{state.inspect} must be one of: [#{States.join ', '}]" + false + end + end + + def set_state_before_save + if self.components_look_ready? && (!self.state || self.state == New) + self.state = Ready 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