+
+ def update_timestamps_when_state_changes
+ return if not (state_changed? or new_record?)
+
+ case state
+ when Running
+ self.started_at ||= db_current_time
+ when Failed, Complete
+ self.finished_at ||= db_current_time
+ when Cancelled
+ self.cancelled_at ||= db_current_time
+ end
+
+ # TODO: Remove the following case block when old "success" and
+ # "running" attrs go away. Until then, this ensures we still
+ # expose correct success/running flags to older clients, even if
+ # some new clients are writing only the new state attribute.
+ case state
+ when Queued
+ self.running = false
+ self.success = nil
+ when Running
+ self.running = true
+ self.success = nil
+ when Cancelled, Failed
+ self.running = false
+ self.success = false
+ when Complete
+ self.running = false
+ self.success = true
+ end
+ self.running ||= false # Default to false instead of nil.
+
+ @need_crunch_dispatch_trigger = true
+
+ true
+ end
+
+ def update_state_from_old_state_attrs
+ # If a client has touched the legacy state attrs, update the
+ # "state" attr to agree with the updated values of the legacy
+ # attrs.
+ #
+ # TODO: Remove this method when old "success" and "running" attrs
+ # go away.
+ if cancelled_at_changed? or
+ success_changed? or
+ running_changed? or
+ state.nil?
+ if cancelled_at
+ self.state = Cancelled
+ elsif success == false
+ self.state = Failed
+ elsif success == true
+ self.state = Complete
+ elsif running == true
+ self.state = Running
+ else
+ self.state = Queued
+ end
+ end
+ true
+ end
+
+ def validate_status
+ if self.state.in?(States)
+ true
+ else
+ errors.add :state, "#{state.inspect} must be one of: #{States.inspect}"
+ false
+ end
+ end
+
+ def validate_state_change
+ ok = true
+ if self.state_changed?
+ ok = case self.state_was
+ when nil
+ # state isn't set yet
+ true
+ when Queued
+ # Permit going from queued to any state
+ true
+ when Running
+ # From running, may only transition to a finished state
+ [Complete, Failed, Cancelled].include? self.state
+ when Complete, Failed, Cancelled
+ # Once in a finished state, don't permit any more state changes
+ false
+ else
+ # Any other state transition is also invalid
+ false
+ end
+ if not ok
+ errors.add :state, "invalid change from #{self.state_was} to #{self.state}"
+ end
+ end
+ ok
+ end
+
+ def ensure_no_collection_uuids_in_script_params
+ # recursive_hash_search searches recursively through hashes and
+ # arrays in 'thing' for string fields matching regular expression
+ # 'pattern'. Returns true if pattern is found, false otherwise.
+ def recursive_hash_search thing, pattern
+ if thing.is_a? Hash
+ thing.each do |k, v|
+ return true if recursive_hash_search v, pattern
+ end
+ elsif thing.is_a? Array
+ thing.each do |k|
+ return true if recursive_hash_search k, pattern
+ end
+ elsif thing.is_a? String
+ return true if thing.match pattern
+ end
+ false
+ end
+
+ # Fail validation if any script_parameters field includes a string containing a
+ # collection uuid pattern.
+ if self.script_parameters_changed?
+ if recursive_hash_search(self.script_parameters, Collection.uuid_regex)
+ self.errors.add :script_parameters, "must use portable_data_hash instead of collection uuid"
+ return false
+ end
+ end
+ true
+ end