X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/620a10bed55b85294baad9dba965ea8dad59e884..b25d2ec2950f6e2ffa9943a15c87441ebd6672b2:/services/api/script/cancel_stale_jobs.rb diff --git a/services/api/script/cancel_stale_jobs.rb b/services/api/script/cancel_stale_jobs.rb index dde4cbed0c..4949ec0880 100755 --- a/services/api/script/cancel_stale_jobs.rb +++ b/services/api/script/cancel_stale_jobs.rb @@ -1,5 +1,6 @@ #!/usr/bin/env ruby + if ENV["CRUNCH_DISPATCH_LOCKFILE"] lockfilename = ENV.delete "CRUNCH_DISPATCH_LOCKFILE" lockfile = File.open(lockfilename, File::RDWR|File::CREAT, 0644) @@ -13,25 +14,31 @@ ENV["RAILS_ENV"] = ARGV[0] || ENV["RAILS_ENV"] || "development" require File.dirname(__FILE__) + '/../config/boot' require File.dirname(__FILE__) + '/../config/environment' -def cancel_stale_jobs - Job.running.each do |jobrecord| - f = Log.where("object_uuid=?", jobrecord.uuid).limit(1).order("created_at desc").first - if f - age = (Time.now - f.created_at) - if age > 300 - $stderr.puts "dispatch: failing orphan job #{jobrecord.uuid}, last log is #{age} seconds old" - # job is marked running, but not known to crunch-dispatcher, and - # hasn't produced any log entries for 5 minutes, so mark it as failed. - jobrecord.running = false - jobrecord.cancelled_at ||= Time.now - jobrecord.finished_at ||= Time.now - if jobrecord.success.nil? - jobrecord.success = false +class CancelJobs + include ApplicationHelper + + def cancel_stale_jobs + act_as_system_user do + Job.running.each do |jobrecord| + f = Log.where("object_uuid=?", jobrecord.uuid).limit(1).order("created_at desc").first + if f + age = (Time.now - f.created_at) + if age > 300 + $stderr.puts "dispatch: failing orphan job #{jobrecord.uuid}, last log is #{age} seconds old" + # job is marked running, but not known to crunch-dispatcher, and + # hasn't produced any log entries for 5 minutes, so mark it as failed. + jobrecord.running = false + jobrecord.cancelled_at ||= Time.now + jobrecord.finished_at ||= Time.now + if jobrecord.success.nil? + jobrecord.success = false + end + jobrecord.save! + end end - jobrecord.save! end end end end -cancel_stale_jobs +CancelJobs.new.cancel_stale_jobs