- def index
- return super unless @where.is_a? Hash
- want_ancestor = @where[:script_version_descends_from]
- if want_ancestor
- # Check for missing commit_ancestor rows, and create them if
- # possible.
- @objects.
- dup.
- includes(:commit_ancestors). # I wish Rails would let me
- # specify here which
- # commit_ancestors I am
- # interested in.
- each do |o|
- if o.commit_ancestors.
- select { |ca| ca.ancestor == want_ancestor }.
- empty? and !o.script_version.nil?
- begin
- o.commit_ancestors << CommitAncestor.find_or_create_by_descendant_and_ancestor(o.script_version, want_ancestor)
- rescue
+ def create
+ [:repository, :script, :script_version, :script_parameters].each do |r|
+ if !resource_attrs[r]
+ return send_error("#{r} attribute must be specified",
+ status: :unprocessable_entity)
+ end
+ end
+
+ # We used to ask for the minimum_, exclude_, and no_reuse params
+ # in the job resource. Now we advertise them as flags that alter
+ # the behavior of the create action.
+ [:minimum_script_version, :exclude_script_versions].each do |attr|
+ if resource_attrs.has_key? attr
+ params[attr] = resource_attrs.delete attr
+ end
+ end
+ if resource_attrs.has_key? :no_reuse
+ params[:find_or_create] = !resource_attrs.delete(:no_reuse)
+ end
+
+ if params[:find_or_create]
+ load_filters_param
+ if @filters.empty? # Translate older creation parameters into filters.
+ @filters = [:repository, :script].map do |attrsym|
+ [attrsym.to_s, "=", resource_attrs[attrsym]]
+ end
+ @filters.append(["script_version", "in",
+ Commit.find_commit_range(current_user,
+ resource_attrs[:repository],
+ params[:minimum_script_version],
+ resource_attrs[:script_version],
+ params[:exclude_script_versions])])
+ if image_search = resource_attrs[:runtime_constraints].andand["docker_image"]
+ image_tag = resource_attrs[:runtime_constraints]["docker_image_tag"]
+ image_locator = Collection.
+ uuids_for_docker_image(image_search, image_tag, @read_users).first
+ return super if image_locator.nil? # We won't find anything to reuse.
+ @filters.append(["docker_image_locator", "=", image_locator])
+ else
+ @filters.append(["docker_image_locator", "=", nil])
+ end
+ else # Check specified filters for some reasonableness.
+ filter_names = @filters.map { |f| f.first }.uniq
+ ["repository", "script"].each do |req_filter|
+ if not filter_names.include?(req_filter)
+ raise ArgumentError.new("#{req_filter} filter required")
+ end
+ end
+ end
+
+ # Search for a reusable Job, and return it if found.
+ @objects = Job.readable_by(current_user)
+ apply_filters
+ @object = nil
+ incomplete_job = nil
+ @objects.each do |j|
+ if j.nondeterministic != true and
+ ((j.success == true and j.output != nil) or j.running == true) and
+ j.script_parameters == resource_attrs[:script_parameters]
+ if j.running
+ # We'll use this if we don't find a job that has completed
+ incomplete_job ||= j
+ else
+ # Record the first job in the list
+ if !@object
+ @object = j
+ end
+ # Ensure that all candidate jobs actually did produce the same output
+ if @object.output != j.output
+ @object = nil
+ break
+ end