- # We'll use this if we don't find a job that has completed
- incomplete_job ||= j
- next
- end
-
- if chosen == false
- # We have already decided not to reuse any completed job
- next
- elsif chosen
- if chosen.output != j.output
+ if !incomplete_job
+ # We'll use this if we don't find a job that has completed
+ log_reuse_info { "job #{j.uuid} is reusable, but unfinished; continuing search for completed jobs" }
+ incomplete_job = j
+ else
+ log_reuse_info { "job #{j.uuid} is unfinished and we already have #{incomplete_job.uuid}; ignoring" }
+ end
+ elsif chosen == false
+ # 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 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.reuse_job_if_outputs_differ
+ 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_output
+ if chosen_output != j.output