X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/a3c5fac3f7849cab38bedd313b522b994be17b15..f60b9d7057fef32a7b61ea4de55b3d95b5b28f6c:/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 5c60178438..7bb814c60d 100644 --- a/services/api/app/models/pipeline_instance.rb +++ b/services/api/app/models/pipeline_instance.rb @@ -1,5 +1,5 @@ class PipelineInstance < ArvadosModel - include AssignUuid + include HasUuid include KindAndEtag include CommonApiTemplate serialize :components, Hash @@ -9,8 +9,9 @@ class PipelineInstance < ArvadosModel before_validation :bootstrap_components before_validation :update_success - before_create :verify_status - before_save :verify_status + before_validation :verify_status + before_create :set_state_before_save + before_save :set_state_before_save api_accessible :user, extend: :common do |t| t.add :pipeline_template_uuid @@ -26,30 +27,32 @@ class PipelineInstance < ArvadosModel end # Supported states for a pipeline instance - New = 'New' - Ready = 'Ready' - RunningOnServer = 'RunningOnServer' - RunningOnClient = 'RunningOnClient' - Paused = 'Paused' - Failed = 'Failed' - Complete = 'Complete' + States = + [ + (New = 'New'), + (Ready = 'Ready'), + (RunningOnServer = 'RunningOnServer'), + (RunningOnClient = 'RunningOnClient'), + (Paused = 'Paused'), + (Failed = 'Failed'), + (Complete = 'Complete'), + ] def dependencies dependency_search(self.components).keys end # if all components have input, the pipeline is Ready - def self.is_ready components - if !components || components.empty? # is this correct? - return true + def components_look_ready? + if !self.components || self.components.empty? + return false end all_components_have_input = true - components.each do |name, component| - component['script_parameters'].each do |parametername, parameter| + 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 - ![false,'false',0,'0'].index parameter['required'] + if parameter['value'].nil? and parameter['required'] if parameter['output_of'] next end @@ -98,7 +101,7 @@ class PipelineInstance < ArvadosModel end def self.queue - self.where('active = true') + self.where("state = 'RunningOnServer'") end protected @@ -139,50 +142,81 @@ class PipelineInstance < ArvadosModel end def verify_status - if active_changed? - if self.active - self.state = RunningOnServer - else - if PipelineInstance.is_ready self.components - self.state = Ready - else - self.state = New - end - end - elsif success_changed? - if self.success - self.active = false - self.state = Complete - else - self.active = false - self.state = Failed - end - elsif state_changed? + changed_attributes = self.changed + + if 'state'.in? changed_attributes case self.state - when New, Ready - self.active = false + when New, Ready, Paused + self.active = nil self.success = nil when RunningOnServer self.active = true self.success = nil when RunningOnClient - self.active = false + self.active = nil self.success = nil when Failed self.active = false self.success = false + self.state = Failed # before_validation will fail if false is returned in the previous line when Complete self.active = false self.success = true + else + return false end - else # new object create or save - if !self.state || self.state == New || !self.active - if PipelineInstance.is_ready self.components + elsif 'success'.in? changed_attributes + logger.info "pipeline_instance changed_attributes has success for #{self.uuid}" + if self.success + self.active = false + self.state = Complete + else + self.active = false + self.state = Failed + end + elsif 'active'.in? changed_attributes + logger.info "pipeline_instance changed_attributes has active for #{self.uuid}" + if self.active + if self.state.in? [New, Ready, Paused] + self.state = RunningOnServer + end + else + if self.state == RunningOnServer # state was RunningOnServer + self.active = nil + self.state = Paused + elsif self.components_look_ready? self.state = Ready else self.state = New end end + elsif new_record? and self.state.nil? + # No state, active, or success given + self.state = New + end + + if new_record? or 'components'.in? changed_attributes + self.state ||= New + if self.state == New and self.components_look_ready? + self.state = Ready + end + 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.state || self.state == New || self.state == Ready || self.state == Paused + if self.active + self.state = RunningOnServer + elsif self.components_look_ready? && (!self.state || self.state == New) + self.state = Ready + end end end