+ if (uuid = mount.delete 'uuid')
+ c = Collection.
+ readable_by(current_user).
+ where(uuid: uuid).
+ select(:portable_data_hash).
+ first
+ if !c
+ raise ArvadosModel::UnresolvableContainerError.new "cannot mount collection #{uuid.inspect}: not found"
+ end
+ if mount['portable_data_hash'].nil?
+ # PDH not supplied by client
+ mount['portable_data_hash'] = c.portable_data_hash
+ elsif mount['portable_data_hash'] != c.portable_data_hash
+ # UUID and PDH supplied by client, but they don't agree
+ raise ArgumentError.new "cannot mount collection #{uuid.inspect}: current portable_data_hash #{c.portable_data_hash.inspect} does not match #{c['portable_data_hash'].inspect} in request"
+ end
+ end
+ end
+ return c_mounts
+ end
+
+ # Return a container_image PDH suitable for a Container.
+ def self.resolve_container_image(container_image)
+ coll = Collection.for_latest_docker_image(container_image)
+ if !coll
+ raise ArvadosModel::UnresolvableContainerError.new "docker image #{container_image.inspect} not found"
+ end
+ coll.portable_data_hash
+ end
+
+ def self.find_reusable(attrs)
+ log_reuse_info { "starting with #{Container.all.count} container records in database" }
+ candidates = Container.where_serialized(:command, attrs[:command])
+ log_reuse_info(candidates) { "after filtering on command #{attrs[:command].inspect}" }
+
+ candidates = candidates.where('cwd = ?', attrs[:cwd])
+ log_reuse_info(candidates) { "after filtering on cwd #{attrs[:cwd].inspect}" }
+
+ candidates = candidates.where_serialized(:environment, attrs[:environment])
+ log_reuse_info(candidates) { "after filtering on environment #{attrs[:environment].inspect}" }
+
+ candidates = candidates.where('output_path = ?', attrs[:output_path])
+ log_reuse_info(candidates) { "after filtering on output_path #{attrs[:output_path].inspect}" }
+
+ image = resolve_container_image(attrs[:container_image])
+ candidates = candidates.where('container_image = ?', image)
+ log_reuse_info(candidates) { "after filtering on container_image #{image.inspect} (resolved from #{attrs[:container_image].inspect})" }
+
+ candidates = candidates.where_serialized(:mounts, resolve_mounts(attrs[:mounts]))
+ log_reuse_info(candidates) { "after filtering on mounts #{attrs[:mounts].inspect}" }
+
+ candidates = candidates.where_serialized(:runtime_constraints, resolve_runtime_constraints(attrs[:runtime_constraints]))
+ log_reuse_info(candidates) { "after filtering on runtime_constraints #{attrs[:runtime_constraints].inspect}" }
+
+ log_reuse_info { "checking for state=Complete with readable output and log..." }
+
+ select_readable_pdh = Collection.
+ readable_by(current_user).
+ select(:portable_data_hash).
+ to_sql
+
+ usable = candidates.where(state: Complete, exit_code: 0)
+ log_reuse_info(usable) { "with state=Complete, exit_code=0" }
+
+ usable = usable.where("log IN (#{select_readable_pdh})")
+ log_reuse_info(usable) { "with readable log" }
+
+ usable = usable.where("output IN (#{select_readable_pdh})")
+ log_reuse_info(usable) { "with readable output" }
+
+ usable = usable.order('finished_at ASC').limit(1).first
+ if usable
+ log_reuse_info { "done, reusing container #{usable.uuid} with state=Complete" }
+ return usable