Merge branch '15396-trgm-docs'
[arvados.git] / services / api / app / models / job.rb
index fa38ece244afb52b2c2159f8cad6950f6b16468c..4d63deb99cd1d236b348996228c0d3b036416e6e 100644 (file)
@@ -1,3 +1,7 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 require 'log_reuse_info'
 require 'safe_json'
 
@@ -8,7 +12,6 @@ class Job < ArvadosModel
   extend CurrentApiClient
   extend LogReuseInfo
   serialize :components, Hash
-  attr_protected :arvados_sdk_version, :docker_image_locator
   serialize :script_parameters, Hash
   serialize :runtime_constraints, Hash
   serialize :tasks_summary, Hash
@@ -29,7 +32,7 @@ class Job < ArvadosModel
   has_many :commit_ancestors, :foreign_key => :descendant, :primary_key => :script_version
   has_many(:nodes, foreign_key: :job_uuid, primary_key: :uuid)
 
-  class SubmitIdReused < StandardError
+  class SubmitIdReused < RequestError
   end
 
   api_accessible :user, extend: :common do |t|
@@ -79,6 +82,10 @@ class Job < ArvadosModel
     ["components"]
   end
 
+  def self.protected_attributes
+    [:arvados_sdk_version, :docker_image_locator]
+  end
+
   def assert_finished
     update_attributes(finished_at: finished_at || db_current_time,
                       success: success.nil? ? false : success,
@@ -261,6 +268,7 @@ class Job < ArvadosModel
     log_reuse_info(candidates) { "after filtering on repo, script, and custom filters #{filters.inspect}" }
 
     chosen = nil
+    chosen_output = nil
     incomplete_job = nil
     candidates.each do |j|
       if j.state != Job::Complete
@@ -275,17 +283,25 @@ class Job < ArvadosModel
         # Ignore: we have already decided not to reuse any completed
         # job.
         log_reuse_info { "job #{j.uuid} with output #{j.output} ignored, see above" }
-      elsif Rails.configuration.reuse_job_if_outputs_differ
-        if Collection.readable_by(current_user).find_by_portable_data_hash(j.output)
-          log_reuse_info { "job #{j.uuid} with output #{j.output} is reusable; decision is final." }
-          return j
-        else
-          # Ignore: keep locking for an incomplete job or one whose
+      elsif j.output.nil?
+        log_reuse_info { "job #{j.uuid} has nil output" }
+      elsif j.log.nil?
+        log_reuse_info { "job #{j.uuid} has nil log" }
+      elsif Rails.configuration.Containers.JobsAPI.ReuseJobIfOutputsDiffer
+        if !Collection.readable_by(current_user).find_by_portable_data_hash(j.output)
+          # Ignore: keep looking for an incomplete job or one whose
           # output is readable.
           log_reuse_info { "job #{j.uuid} output #{j.output} unavailable to user; continuing search" }
+        elsif !Collection.readable_by(current_user).find_by_portable_data_hash(j.log)
+          # Ignore: keep looking for an incomplete job or one whose
+          # log is readable.
+          log_reuse_info { "job #{j.uuid} log #{j.log} unavailable to user; continuing search" }
+        else
+          log_reuse_info { "job #{j.uuid} with output #{j.output} is reusable; decision is final." }
+          return j
         end
-      elsif chosen
-        if chosen.output != j.output
+      elsif chosen_output
+        if chosen_output != j.output
           # If two matching jobs produced different outputs, run a new
           # job (or use one that's already running/queued) instead of
           # choosing one arbitrarily.
@@ -304,9 +320,15 @@ class Job < ArvadosModel
         # any further investigation of reusable jobs is futile.
         log_reuse_info { "job #{j.uuid} output #{j.output} is unavailable to user; this means no finished job can be reused (see reuse_job_if_outputs_differ in application.default.yml)" }
         chosen = false
+      elsif !Collection.readable_by(current_user).find_by_portable_data_hash(j.log)
+        # This user cannot read the log of this job, don't try to reuse the
+        # job but consider if the output is consistent.
+        log_reuse_info { "job #{j.uuid} log #{j.log} is unavailable to user; continuing search" }
+        chosen_output = j.output
       else
         log_reuse_info { "job #{j.uuid} with output #{j.output} can be reused; continuing search in case other candidates have different outputs" }
         chosen = j
+        chosen_output = j.output
       end
     end
     j = chosen || incomplete_job
@@ -469,9 +491,9 @@ class Job < ArvadosModel
   end
 
   def find_docker_image_locator
-    if runtime_constraints.is_a? Hash
+    if runtime_constraints.is_a? Hash and Rails.configuration.Containers.JobsAPI.DefaultDockerImage != ""
       runtime_constraints['docker_image'] ||=
-        Rails.configuration.default_docker_image_for_jobs
+        Rails.configuration.Containers.JobsAPI.DefaultDockerImage
     end
 
     resolve_runtime_constraint("docker_image",
@@ -547,7 +569,7 @@ class Job < ArvadosModel
 
   def trigger_crunch_dispatch_if_cancelled
     if @need_crunch_dispatch_trigger
-      File.open(Rails.configuration.crunch_refresh_trigger, 'wb') do
+      File.open(Rails.configuration.Containers.JobsAPI.CrunchRefreshTrigger, 'wb') do
         # That's all, just create/touch a file for crunch-job to see.
       end
     end