2017-04-28 03:54:48 +08:00
|
|
|
#
|
|
|
|
# Copyright (C) 2011 - present Instructure, Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Canvas.
|
|
|
|
#
|
|
|
|
# Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along
|
|
|
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-03-24 05:06:56 +08:00
|
|
|
Delayed::Job.include(JobLiveEventsContext)
|
|
|
|
|
2014-10-01 02:41:19 +08:00
|
|
|
Delayed::Backend::Base.class_eval do
|
|
|
|
attr_writer :current_shard
|
2012-08-16 23:21:18 +08:00
|
|
|
|
2014-10-01 02:41:19 +08:00
|
|
|
def current_shard
|
|
|
|
@current_shard || Shard.birth
|
2012-08-16 23:21:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-09 03:23:33 +08:00
|
|
|
# if the method was defined by a previous module, use the existing
|
|
|
|
# implementation, but provide a default otherwise
|
2018-03-03 05:37:50 +08:00
|
|
|
module Delayed::Backend::DefaultJobAccount
|
|
|
|
def account
|
2018-03-09 03:23:33 +08:00
|
|
|
if defined?(super)
|
|
|
|
super
|
|
|
|
else
|
|
|
|
Account.default
|
|
|
|
end
|
2018-03-03 05:37:50 +08:00
|
|
|
end
|
|
|
|
end
|
2018-03-09 03:23:33 +08:00
|
|
|
Delayed::Backend::ActiveRecord::Job.include(Delayed::Backend::DefaultJobAccount)
|
|
|
|
Delayed::Backend::Redis::Job.include(Delayed::Backend::DefaultJobAccount)
|
2018-03-03 05:37:50 +08:00
|
|
|
|
2017-10-14 03:46:25 +08:00
|
|
|
Delayed::Settings.max_attempts = 1
|
2014-10-01 02:41:19 +08:00
|
|
|
Delayed::Settings.queue = "canvas_queue"
|
|
|
|
Delayed::Settings.sleep_delay = ->{ Setting.get('delayed_jobs_sleep_delay', '2.0').to_f }
|
|
|
|
Delayed::Settings.sleep_delay_stagger = ->{ Setting.get('delayed_jobs_sleep_delay_stagger', '2.0').to_f }
|
|
|
|
Delayed::Settings.fetch_batch_size = ->{ Setting.get('jobs_get_next_batch_size', '5').to_i }
|
|
|
|
Delayed::Settings.select_random_from_batch = ->{ Setting.get('jobs_select_random', 'false') == 'true' }
|
|
|
|
Delayed::Settings.num_strands = ->(strand_name){ Setting.get("#{strand_name}_num_strands", nil) }
|
|
|
|
Delayed::Settings.worker_procname_prefix = ->{ "#{Shard.current(:delayed_jobs).id}~" }
|
|
|
|
Delayed::Settings.pool_procname_suffix = " (#{Canvas.revision})" if Canvas.revision
|
|
|
|
|
|
|
|
Delayed::Settings.default_job_options = ->{
|
|
|
|
{
|
|
|
|
current_shard: Shard.current,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# load our periodic_jobs.yml (cron overrides config file)
|
|
|
|
Delayed::Periodic.add_overrides(ConfigFile.load('periodic_jobs') || {})
|
|
|
|
|
2016-02-24 01:42:01 +08:00
|
|
|
if ActiveRecord::Base.configurations[Rails.env]['queue']
|
|
|
|
ActiveSupport::Deprecation.warn("A queue section in database.yml is no longer supported. Please run migrations, then remove it.")
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
|
|
|
|
2017-08-26 01:02:29 +08:00
|
|
|
# configure autoscaling plugin
|
|
|
|
if (config = Delayed::CLI.instance&.config&.[](:auto_scaling))
|
|
|
|
require 'jobs_autoscaling'
|
|
|
|
if config[:asg_name]
|
|
|
|
action = JobsAutoscaling::AwsAction.new(asg_name: config[:asg_name], aws_config: config[:aws_config])
|
|
|
|
else
|
|
|
|
action = JobsAutoscaling::LoggerAction.new
|
|
|
|
end
|
|
|
|
autoscaler = JobsAutoscaling::Monitor.new(action: action)
|
|
|
|
autoscaler.activate!
|
|
|
|
end
|
|
|
|
|
2011-02-01 09:57:29 +08:00
|
|
|
Delayed::Worker.on_max_failures = proc do |job, err|
|
2013-02-21 06:07:56 +08:00
|
|
|
# We don't want to keep around max_attempts failed jobs that failed because the
|
|
|
|
# underlying AR object was destroyed.
|
|
|
|
# All other failures are kept for inspection.
|
|
|
|
err.is_a?(Delayed::Backend::RecordNotFound)
|
2011-02-01 09:57:29 +08:00
|
|
|
end
|
2014-02-13 01:29:09 +08:00
|
|
|
|
2014-10-01 02:41:19 +08:00
|
|
|
### lifecycle callbacks
|
|
|
|
|
|
|
|
Delayed::Pool.on_fork = ->{
|
|
|
|
Canvas.reconnect_redis
|
|
|
|
}
|
|
|
|
|
2014-02-13 01:29:09 +08:00
|
|
|
Delayed::Worker.lifecycle.around(:perform) do |worker, job, &block|
|
2017-09-14 03:28:05 +08:00
|
|
|
Canvas::Reloader.reload! if Canvas::Reloader.pending_reload
|
|
|
|
|
2014-10-01 02:41:19 +08:00
|
|
|
# context for our custom logger
|
|
|
|
Thread.current[:context] = {
|
|
|
|
# these 2 keys aren't terribly well named for this, since they were intended for http requests
|
|
|
|
:request_id => job.id,
|
|
|
|
:session_id => worker.name,
|
|
|
|
}
|
|
|
|
|
2017-03-24 05:06:56 +08:00
|
|
|
LiveEvents.set_context(job.live_events_context)
|
2015-03-14 04:07:54 +08:00
|
|
|
|
2018-03-03 05:37:50 +08:00
|
|
|
HostUrl.reset_cache!
|
|
|
|
old_root_account = Attachment.current_root_account
|
|
|
|
Attachment.current_root_account = job.account
|
|
|
|
|
2014-02-13 01:29:09 +08:00
|
|
|
starting_mem = Canvas.sample_memory()
|
|
|
|
starting_cpu = Process.times()
|
2014-11-12 02:56:59 +08:00
|
|
|
lag = ((Time.now - job.run_at) * 1000).round
|
2016-12-21 07:29:38 +08:00
|
|
|
obj_tag, method_tag = job.tag.split(/[\.#]/, 2).map do |v|
|
|
|
|
CanvasStatsd::Statsd.escape(v).gsub("::", "-")
|
|
|
|
end
|
|
|
|
method_tag ||= "unknown"
|
2014-12-27 00:35:11 +08:00
|
|
|
shard_id = job.current_shard.try(:id).to_i
|
2016-12-21 07:29:38 +08:00
|
|
|
stats = ["delayedjob.queue", "delayedjob.queue.tag.#{obj_tag}.#{method_tag}", "delayedjob.queue.shard.#{shard_id}"]
|
2014-05-13 03:03:59 +08:00
|
|
|
stats << "delayedjob.queue.jobshard.#{job.shard.id}" if job.respond_to?(:shard)
|
|
|
|
CanvasStatsd::Statsd.timing(stats, lag)
|
2018-03-03 05:37:50 +08:00
|
|
|
|
2014-02-13 01:29:09 +08:00
|
|
|
begin
|
2016-12-21 07:29:38 +08:00
|
|
|
stats = ["delayedjob.perform", "delayedjob.perform.tag.#{obj_tag}.#{method_tag}", "delayedjob.perform.shard.#{shard_id}"]
|
2014-05-13 03:03:59 +08:00
|
|
|
stats << "delayedjob.perform.jobshard.#{job.shard.id}" if job.respond_to?(:shard)
|
|
|
|
CanvasStatsd::Statsd.time(stats) do
|
|
|
|
block.call(worker, job)
|
|
|
|
end
|
2014-02-13 01:29:09 +08:00
|
|
|
ensure
|
|
|
|
ending_cpu = Process.times()
|
|
|
|
ending_mem = Canvas.sample_memory()
|
|
|
|
user_cpu = ending_cpu.utime - starting_cpu.utime
|
|
|
|
system_cpu = ending_cpu.stime - starting_cpu.stime
|
|
|
|
|
2018-03-03 05:37:50 +08:00
|
|
|
Attachment.current_root_account = old_root_account
|
|
|
|
|
2015-03-14 04:07:54 +08:00
|
|
|
LiveEvents.clear_context!
|
|
|
|
|
2014-02-13 01:29:09 +08:00
|
|
|
Rails.logger.info "[STAT] #{starting_mem} #{ending_mem} #{ending_mem - starting_mem} #{user_cpu} #{system_cpu}"
|
2015-10-03 05:01:40 +08:00
|
|
|
|
|
|
|
Thread.current[:context] = nil
|
2014-02-13 01:29:09 +08:00
|
|
|
end
|
|
|
|
end
|
2014-05-13 03:03:59 +08:00
|
|
|
|
|
|
|
Delayed::Worker.lifecycle.around(:pop) do |worker, &block|
|
2014-08-07 03:00:44 +08:00
|
|
|
CanvasStatsd::Statsd.time(["delayedjob.pop", "delayedjob.pop.jobshard.#{Shard.current(:delayed_jobs).id}"]) do
|
2014-05-13 03:03:59 +08:00
|
|
|
block.call(worker)
|
|
|
|
end
|
|
|
|
end
|
2014-05-03 00:35:29 +08:00
|
|
|
|
2017-02-18 06:22:38 +08:00
|
|
|
Delayed::Worker.lifecycle.around(:work_queue_pop) do |worker, config, &block|
|
2017-02-14 08:01:03 +08:00
|
|
|
CanvasStatsd::Statsd.time(["delayedjob.workqueuepop", "delayedjob.workqueuepop.jobshard.#{Shard.current(:delayed_jobs).id}"]) do
|
2017-02-18 06:22:38 +08:00
|
|
|
block.call(worker, config)
|
2017-02-14 08:01:03 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-04 00:38:36 +08:00
|
|
|
Delayed::Worker.lifecycle.before(:perform) do |_worker, _job|
|
2014-05-03 00:35:29 +08:00
|
|
|
# Since AdheresToPolicy::Cache uses an instance variable class cache lets clear
|
|
|
|
# it so we start with a clean slate.
|
|
|
|
AdheresToPolicy::Cache.clear
|
2014-08-26 22:53:26 +08:00
|
|
|
LoadAccount.clear_shard_cache
|
2014-05-03 00:35:29 +08:00
|
|
|
end
|
2014-10-01 02:41:19 +08:00
|
|
|
|
2017-10-04 00:38:36 +08:00
|
|
|
Delayed::Worker.lifecycle.around(:perform) do |worker, job, &block|
|
2017-03-22 02:03:20 +08:00
|
|
|
CanvasStatsd::Statsd.batch do
|
2017-10-04 00:38:36 +08:00
|
|
|
block.call(worker, job)
|
2017-03-22 02:03:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-01 02:41:19 +08:00
|
|
|
Delayed::Worker.lifecycle.before(:exceptional_exit) do |worker, exception|
|
2015-06-18 11:19:24 +08:00
|
|
|
info = Canvas::Errors::WorkerInfo.new(worker)
|
2015-04-05 10:39:49 +08:00
|
|
|
Canvas::Errors.capture(exception, info.to_h)
|
|
|
|
end
|
|
|
|
|
|
|
|
Delayed::Worker.lifecycle.before(:error) do |worker, job, exception|
|
|
|
|
info = Canvas::Errors::JobInfo.new(job, worker)
|
2015-08-12 00:54:37 +08:00
|
|
|
begin
|
|
|
|
(job.current_shard || Shard.default).activate do
|
|
|
|
Canvas::Errors.capture(exception, info.to_h)
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
Canvas::Errors.capture(exception, info.to_h)
|
|
|
|
end
|
2014-10-01 02:41:19 +08:00
|
|
|
end
|