Merge branch 'master' into 7607-getting-started-flag
[arvados.git] / services / api / app / models / container_request.rb
index 1011f6fd755d6beb1354244a127b3aad994ac836..acb751c89401424e04d03b950d5acc675dceaca8 100644 (file)
@@ -62,6 +62,12 @@ class ContainerRequest < ArvadosModel
     %w(modified_by_client_uuid container_uuid requesting_container_uuid)
   end
 
+  def container_completed!
+    # may implement retry logic here in the future.
+    self.state = ContainerRequest::Final
+    self.save!
+  end
+
   protected
 
   def fill_field_defaults
@@ -70,7 +76,6 @@ class ContainerRequest < ArvadosModel
     self.runtime_constraints ||= {}
     self.mounts ||= {}
     self.cwd ||= "."
-    self.priority ||= 1
   end
 
   # Turn a container request into a container.
@@ -119,6 +124,10 @@ class ContainerRequest < ArvadosModel
         errors.add :container_uuid, "has not been resolved to a container."
       end
 
+      if priority.nil?
+        errors.add :priority, "cannot be nil"
+      end
+
       # Can update priority, container count.
       permitted.push :priority, :container_count_max, :container_uuid
 
@@ -131,6 +140,10 @@ class ContainerRequest < ArvadosModel
       end
 
     when Final
+      if not current_user.andand.is_admin
+        errors.add :state, "of container request can only be set to Final by system."
+      end
+
       if self.state_changed?
           permitted.push :state
       else
@@ -145,12 +158,16 @@ class ContainerRequest < ArvadosModel
   end
 
   def update_priority
-    if self.state == Committed and (self.state_changed? or
-                                    self.priority_changed? or
-                                    self.container_uuid_changed?)
-      c = Container.find_by_uuid self.container_uuid
-      act_as_system_user do
-        c.update_priority!
+    if [Committed, Final].include? self.state and (self.state_changed? or
+                                                   self.priority_changed? or
+                                                   self.container_uuid_changed?)
+      [self.container_uuid_was, self.container_uuid].each do |cuuid|
+        unless cuuid.nil?
+          c = Container.find_by_uuid cuuid
+          act_as_system_user do
+            c.update_priority!
+          end
+        end
       end
     end
   end