before_create :ensure_unique_submit_id
after_commit :trigger_crunch_dispatch_if_cancelled, :on => :update
before_validation :set_priority
- before_validation :update_timestamps_when_state_changes
before_validation :update_state_from_old_state_attrs
validate :ensure_script_version_is_commit
validate :find_docker_image_locator
validate :validate_status
+ validate :validate_state_change
+ before_save :update_timestamps_when_state_changes
has_many :commit_ancestors, :foreign_key => :descendant, :primary_key => :script_version
+ has_many(:nodes, foreign_key: :job_uuid, primary_key: :uuid)
class SubmitIdReused < StandardError
end
t.add :supplied_script_version
t.add :docker_image_locator
t.add :queue_position
+ t.add :node_uuids
t.add :description
end
running: false)
end
+ def node_uuids
+ nodes.map(&:uuid)
+ end
+
def self.queue
- self.where('started_at is ? and is_locked_by_uuid is ? and cancelled_at is ? and success is ?',
- nil, nil, nil, nil).
- order('priority desc, created_at')
+ self.where('state = ?', Queued).order('priority desc, created_at')
end
def queue_position
- i = 0
- Job::queue.each do |j|
- if j[:uuid] == self.uuid
- return i
+ Job::queue.each_with_index do |job, index|
+ if job[:uuid] == self.uuid
+ return index
end
end
nil
order('priority desc, created_at')
end
+ def lock locked_by_uuid
+ transaction do
+ self.reload
+ unless self.state == Queued and self.is_locked_by_uuid.nil?
+ raise AlreadyLockedError
+ end
+ self.state = Running
+ self.is_locked_by_uuid = locked_by_uuid
+ self.save!
+ end
+ end
+
protected
def foreign_key_attributes
end
def ensure_script_version_is_commit
- if self.is_locked_by_uuid and self.started_at
+ if self.state == Running
# Apparently client has already decided to go for it. This is
# needed to run a local job using a local working directory
# instead of a commit-ish.
end
def find_docker_image_locator
- # Do nothing if docker_image_locator is already set
- return true if not self.docker_image_locator.nil?
# Find the Collection that holds the Docker image specified in the
# runtime constraints, and store its locator in docker_image_locator.
unless runtime_constraints.is_a? Hash
success_changed? or
output_changed? or
log_changed? or
- tasks_summary_changed?
+ tasks_summary_changed? or
+ state_changed?
logger.warn "User #{current_user.uuid if current_user} tried to change protected job attributes on locked #{self.class.to_s} #{uuid_was}"
return false
end
def update_timestamps_when_state_changes
return if not (state_changed? or new_record?)
+
case state
when Running
self.started_at ||= Time.now
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
end