Merge branch '3381-job-progress-bar-bug' closes #3381
[arvados.git] / services / api / app / models / job.rb
index 72cd38115f33f7ade7ad1ad3ee4abc00b501bc19..864e60832f4e8312307fd628ba333ba3e64a2dec 100644 (file)
@@ -9,12 +9,12 @@ class Job < ArvadosModel
   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)
@@ -75,10 +75,9 @@ class Job < ArvadosModel
   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
@@ -93,7 +92,7 @@ class Job < ArvadosModel
     transaction do
       self.reload
       unless self.state == Queued and self.is_locked_by_uuid.nil?
-        raise ConflictError.new
+        raise AlreadyLockedError
       end
       self.state = Running
       self.is_locked_by_uuid = locked_by_uuid
@@ -262,6 +261,7 @@ class Job < ArvadosModel
 
   def update_timestamps_when_state_changes
     return if not (state_changed? or new_record?)
+
     case state
     when Running
       self.started_at ||= Time.now