rename foreign uuid attributes
[arvados.git] / services / api / app / controllers / arvados / v1 / jobs_controller.rb
1 class Arvados::V1::JobsController < ApplicationController
2   accept_attribute_as_json :script_parameters, Hash
3   accept_attribute_as_json :resource_limits, Hash
4   accept_attribute_as_json :tasks_summary, Hash
5   skip_before_filter :find_object_by_uuid, :only => :queue
6
7   def index
8     want_ancestor = @where[:script_version_descends_from]
9     if want_ancestor
10       # Check for missing commit_ancestor rows, and create them if
11       # possible.
12       @objects.
13         dup.
14         includes(:commit_ancestors). # I wish Rails would let me
15                                      # specify here which
16                                      # commit_ancestors I am
17                                      # interested in.
18         each do |o|
19         if o.commit_ancestors.
20             select { |ca| ca.ancestor == want_ancestor }.
21             empty? and !o.script_version.nil?
22           begin
23             o.commit_ancestors << CommitAncestor.find_or_create_by_descendant_and_ancestor(o.script_version, want_ancestor)
24           rescue
25           end
26         end
27         o.commit_ancestors.
28           select { |ca| ca.ancestor == want_ancestor }.
29           select(&:is).
30           first
31       end
32       # Now it is safe to do an .includes().where() because we are no
33       # longer interested in jobs that have other ancestors but not
34       # want_ancestor.
35       @objects = @objects.
36         includes(:commit_ancestors).
37         where('commit_ancestors.ancestor = ? and commit_ancestors.is = ?',
38               want_ancestor, true)
39     end
40     super
41   end
42
43   class LogStreamer
44     def initialize(job)
45       @job = job
46     end
47     def each
48       if @job.finished_at
49         yield "#{@job.uuid} finished at #{@job.finished_at}\n"
50         return
51       end
52       @redis = Redis.new(:timeout => 0)
53       @redis.subscribe(@job.uuid) do |event|
54         event.message do |channel, msg|
55           if msg == "end"
56             @redis.unsubscribe @job.uuid
57           else
58             yield "#{msg}\n"
59           end
60         end
61       end
62     end
63   end
64
65   def log_tail_follow
66     if !@object.andand.uuid
67       return render_not_found
68     end
69     self.response.headers['Last-Modified'] = Time.now.ctime.to_s
70     self.response_body = LogStreamer.new @object
71   end
72
73   def queue
74     load_where_param
75     @where.merge!({
76                     started_at: nil,
77                     is_locked_by_uuid: nil,
78                     cancelled_at: nil
79                   })
80     params[:order] ||= 'priority desc, created_at'
81     find_objects_for_index
82     index
83   end
84
85   def self._queue_requires_parameters
86     self._index_requires_parameters
87   end
88 end