1 class Arvados::V1::JobsController < ApplicationController
2 accept_attribute_as_json :script_parameters, Hash
3 accept_attribute_as_json :runtime_constraints, Hash
4 accept_attribute_as_json :tasks_summary, Hash
5 skip_before_filter :find_object_by_uuid, :only => :queue
6 skip_before_filter :render_404_if_no_object, :only => :queue
9 [:repository, :script, :script_version, :script_parameters].each do |r|
12 :error => "#{r} attribute must be specified"
13 }, status: :unprocessable_entity
17 # We used to ask for the minimum_, exclude_, and no_reuse params
18 # in the job resource. Now we advertise them as flags that alter
19 # the behavior of the create action.
20 [:minimum_script_version, :exclude_script_versions].each do |attr|
21 if resource_attrs.has_key? attr
22 params[attr] = resource_attrs.delete attr
25 if resource_attrs.has_key? :no_reuse
26 params[:find_or_create] = !resource_attrs.delete(:no_reuse)
29 if params[:find_or_create]
30 r = Commit.find_commit_range(current_user,
31 resource_attrs[:repository],
32 params[:minimum_script_version],
33 resource_attrs[:script_version],
34 params[:exclude_script_versions])
35 # Search for jobs whose script_version is in the list of commits
36 # returned by find_commit_range
39 Job.readable_by(current_user).where(script: resource_attrs[:script],
42 if j.nondeterministic != true and
43 ((j.success == true and j.output != nil) or j.running == true) and
44 j.script_parameters == resource_attrs[:script_parameters]
46 # We'll use this if we don't find a job that has completed
49 # Record the first job in the list
53 # Ensure that all candidate jobs actually did produce the same output
54 if @object.output != j.output
60 @object ||= incomplete_job
71 reload_object_before_update
72 @object.update_attributes! cancelled_at: Time.now
77 Q_UPDATE_INTERVAL = 12
78 def initialize(job, opts={})
84 yield "#{@job.uuid} finished at #{@job.finished_at}\n"
87 while not @job.started_at
88 # send a summary (job queue + available nodes) to the client
89 # every few seconds while waiting for the job to start
90 last_ack_at ||= Time.now - Q_UPDATE_INTERVAL - 1
91 if Time.now - last_ack_at >= Q_UPDATE_INTERVAL
92 nodes_in_state = {idle: 0, alloc: 0}
93 ActiveRecord::Base.uncached do
94 Node.where('hostname is not ?', nil).collect do |n|
95 if n.info[:slurm_state]
96 nodes_in_state[n.info[:slurm_state]] ||= 0
97 nodes_in_state[n.info[:slurm_state]] += 1
101 job_queue = Job.queue
102 n_queued_before_me = 0
103 job_queue.each do |j|
104 break if j.uuid == @job.uuid
105 n_queued_before_me += 1
107 yield "#{Time.now}" \
108 " job #{@job.uuid}" \
109 " queue_position #{n_queued_before_me}" \
110 " queue_size #{job_queue.size}" \
111 " nodes_idle #{nodes_in_state[:idle]}" \
112 " nodes_alloc #{nodes_in_state[:alloc]}\n"
113 last_ack_at = Time.now
116 ActiveRecord::Base.uncached do
120 @redis = Redis.new(:timeout => 0)
121 if @redis.exists @job.uuid
122 # A log buffer exists. Start by showing the last few KB.
124 getrange(@job.uuid, 0 - [@opts[:buffer_size], 1].max, -1).
125 sub(/^[^\n]*\n?/, '').
131 # TODO: avoid missing log entries between getrange() above and
133 @redis.subscribe(@job.uuid) do |event|
134 event.message do |channel, msg|
136 @redis.unsubscribe @job.uuid
145 def self._log_tail_follow_requires_parameters
147 buffer_size: {type: 'integer', required: false, default: 2**13}
151 if !@object.andand.uuid
152 return render_not_found
154 if client_accepts_plain_text_stream
155 self.response.headers['Last-Modified'] = Time.now.ctime.to_s
156 self.response_body = LogStreamer.new @object, {
157 buffer_size: (params[:buffer_size].to_i rescue 2**13)
161 href: url_for(uuid: @object.uuid),
162 comment: ('To retrieve the log stream as plain text, ' +
163 'use a request header like "Accept: text/plain"')
172 is_locked_by_uuid: nil,
176 params[:order] ||= 'priority desc, created_at'
177 find_objects_for_index
181 def self._queue_requires_parameters
182 self._index_requires_parameters