2014-01-30 00:21:38 -05:00
|
|
|
# Initially we used sidetiq, this was a problem:
|
|
|
|
#
|
|
|
|
# 1. No mechnism to add "randomisation" into job execution
|
|
|
|
# 2. No stats about previous runs or failures
|
|
|
|
# 3. Dependency on ice_cube gem causes runaway CPU
|
|
|
|
|
2014-04-11 01:43:33 -04:00
|
|
|
require_dependency 'distributed_mutex'
|
|
|
|
|
2014-01-30 00:21:38 -05:00
|
|
|
module Scheduler
|
|
|
|
class Manager
|
2016-05-29 22:28:05 -04:00
|
|
|
attr_accessor :random_ratio, :redis, :enable_stats
|
2014-01-30 00:21:38 -05:00
|
|
|
|
|
|
|
class Runner
|
|
|
|
def initialize(manager)
|
2016-05-29 23:59:39 -04:00
|
|
|
@stopped = false
|
2014-02-11 21:32:34 -05:00
|
|
|
@mutex = Mutex.new
|
2014-01-30 00:21:38 -05:00
|
|
|
@queue = Queue.new
|
|
|
|
@manager = manager
|
2014-02-11 21:32:34 -05:00
|
|
|
@reschedule_orphans_thread = Thread.new do
|
2016-05-29 23:59:39 -04:00
|
|
|
while !@stopped
|
2014-02-11 21:32:34 -05:00
|
|
|
sleep 1.minute
|
|
|
|
@mutex.synchronize do
|
|
|
|
reschedule_orphans
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-02-11 21:32:34 -05:00
|
|
|
@keep_alive_thread = Thread.new do
|
2016-05-29 23:59:39 -04:00
|
|
|
while !@stopped
|
2014-02-11 21:32:34 -05:00
|
|
|
@mutex.synchronize do
|
|
|
|
keep_alive
|
|
|
|
end
|
|
|
|
sleep (@manager.keep_alive_duration / 2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@thread = Thread.new do
|
2016-05-29 23:59:39 -04:00
|
|
|
while !@stopped
|
2017-07-24 15:06:24 -04:00
|
|
|
process_queue
|
2014-02-11 21:32:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def keep_alive
|
|
|
|
@manager.keep_alive
|
|
|
|
rescue => ex
|
2017-07-27 21:20:09 -04:00
|
|
|
Discourse.handle_job_exception(ex, message: "Scheduling manager keep-alive")
|
2014-02-11 21:32:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reschedule_orphans
|
|
|
|
@manager.reschedule_orphans!
|
|
|
|
rescue => ex
|
2017-07-27 21:20:09 -04:00
|
|
|
Discourse.handle_job_exception(ex, message: "Scheduling manager orphan rescheduler")
|
2014-02-11 21:32:34 -05:00
|
|
|
end
|
|
|
|
|
2016-05-29 21:38:08 -04:00
|
|
|
def hostname
|
|
|
|
@hostname ||= begin
|
|
|
|
`hostname`
|
|
|
|
rescue
|
|
|
|
"unknown"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-11 21:32:34 -05:00
|
|
|
def process_queue
|
2017-07-24 15:06:24 -04:00
|
|
|
|
2014-02-11 21:32:34 -05:00
|
|
|
klass = @queue.deq
|
2016-05-29 23:59:39 -04:00
|
|
|
return unless klass
|
|
|
|
|
2014-02-11 21:32:34 -05:00
|
|
|
# hack alert, I need to both deq and set @running atomically.
|
|
|
|
@running = true
|
|
|
|
failed = false
|
|
|
|
start = Time.now.to_f
|
|
|
|
info = @mutex.synchronize { @manager.schedule_info(klass) }
|
2016-05-29 21:38:08 -04:00
|
|
|
stat = nil
|
2017-04-25 13:33:38 -04:00
|
|
|
error = nil
|
|
|
|
|
2014-02-11 21:32:34 -05:00
|
|
|
begin
|
|
|
|
info.prev_result = "RUNNING"
|
|
|
|
@mutex.synchronize { info.write! }
|
2017-07-24 15:06:24 -04:00
|
|
|
|
2016-05-29 22:28:05 -04:00
|
|
|
if @manager.enable_stats
|
2017-10-11 05:45:19 -04:00
|
|
|
RailsMultisite::ConnectionManagement.with_connection("default") do
|
2017-07-24 15:06:24 -04:00
|
|
|
stat = SchedulerStat.create!(
|
|
|
|
name: klass.to_s,
|
|
|
|
hostname: hostname,
|
|
|
|
pid: Process.pid,
|
|
|
|
started_at: Time.zone.now,
|
|
|
|
live_slots_start: GC.stat[:heap_live_slots]
|
|
|
|
)
|
|
|
|
end
|
2016-05-29 22:28:05 -04:00
|
|
|
end
|
2017-07-24 15:06:24 -04:00
|
|
|
|
2014-02-11 21:32:34 -05:00
|
|
|
klass.new.perform
|
|
|
|
rescue => e
|
2017-04-25 21:32:03 -04:00
|
|
|
if e.class != Jobs::HandledExceptionWrapper
|
|
|
|
Discourse.handle_job_exception(e, message: "Running a scheduled job", job: klass)
|
|
|
|
end
|
|
|
|
|
|
|
|
error = "#{e.class}: #{e.message} #{e.backtrace.join("\n")}"
|
2014-02-11 21:32:34 -05:00
|
|
|
failed = true
|
|
|
|
end
|
|
|
|
duration = ((Time.now.to_f - start) * 1000).to_i
|
|
|
|
info.prev_duration = duration
|
|
|
|
info.prev_result = failed ? "FAILED" : "OK"
|
|
|
|
info.current_owner = nil
|
2016-05-29 22:28:05 -04:00
|
|
|
if stat
|
2017-10-11 05:45:19 -04:00
|
|
|
RailsMultisite::ConnectionManagement.with_connection("default") do
|
2017-10-11 03:46:53 -04:00
|
|
|
stat.update!(
|
|
|
|
duration_ms: duration,
|
|
|
|
live_slots_finish: GC.stat[:heap_live_slots],
|
|
|
|
success: !failed,
|
|
|
|
error: error
|
|
|
|
)
|
2017-10-23 02:22:17 -04:00
|
|
|
DiscourseEvent.trigger(:scheduled_job_ran, stat)
|
2017-10-11 03:46:53 -04:00
|
|
|
end
|
2016-05-29 22:28:05 -04:00
|
|
|
end
|
2014-02-11 21:32:34 -05:00
|
|
|
attempts(3) do
|
|
|
|
@mutex.synchronize { info.write! }
|
|
|
|
end
|
|
|
|
rescue => ex
|
2017-07-27 21:20:09 -04:00
|
|
|
Discourse.handle_job_exception(ex, message: "Processing scheduled job queue")
|
2014-02-11 21:32:34 -05:00
|
|
|
ensure
|
|
|
|
@running = false
|
2017-07-24 15:06:24 -04:00
|
|
|
ActiveRecord::Base.connection_handler.clear_active_connections!
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def stop!
|
2017-07-24 17:28:24 -04:00
|
|
|
return if @stopped
|
|
|
|
|
2014-02-11 21:32:34 -05:00
|
|
|
@mutex.synchronize do
|
2016-05-29 23:59:39 -04:00
|
|
|
@stopped = true
|
|
|
|
|
2014-02-11 21:32:34 -05:00
|
|
|
@keep_alive_thread.kill
|
|
|
|
@reschedule_orphans_thread.kill
|
2016-05-29 23:59:39 -04:00
|
|
|
|
2017-07-24 17:28:24 -04:00
|
|
|
@keep_alive_thread.join
|
|
|
|
@reschedule_orphans_thread.join
|
|
|
|
|
2016-05-29 23:59:39 -04:00
|
|
|
enq(nil)
|
|
|
|
|
2017-07-24 17:28:24 -04:00
|
|
|
kill_thread = Thread.new do
|
2017-07-24 15:19:54 -04:00
|
|
|
sleep 0.5
|
2016-05-29 23:59:39 -04:00
|
|
|
@thread.kill
|
|
|
|
end
|
2017-07-24 15:17:48 -04:00
|
|
|
|
|
|
|
@thread.join
|
2017-07-24 17:28:24 -04:00
|
|
|
kill_thread.kill
|
|
|
|
kill_thread.join
|
2014-02-11 21:32:34 -05:00
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def enq(klass)
|
|
|
|
@queue << klass
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait_till_done
|
2014-02-06 19:21:28 -05:00
|
|
|
while !@queue.empty? && !(@queue.num_waiting > 0)
|
2014-01-30 00:21:38 -05:00
|
|
|
sleep 0.001
|
|
|
|
end
|
2014-02-11 21:32:34 -05:00
|
|
|
# this is a hack, but is only used for test anyway
|
|
|
|
sleep 0.001
|
|
|
|
while @running
|
|
|
|
sleep 0.001
|
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
2014-02-11 21:32:34 -05:00
|
|
|
|
|
|
|
def attempts(n)
|
|
|
|
n.times {
|
|
|
|
begin
|
|
|
|
yield; break
|
|
|
|
rescue
|
|
|
|
sleep Random.rand
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.without_runner(redis = nil)
|
2015-06-26 00:02:17 -04:00
|
|
|
self.new(redis, skip_runner: true)
|
2014-02-05 18:14:41 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(redis = nil, options = nil)
|
2014-01-30 00:21:38 -05:00
|
|
|
@redis = $redis || redis
|
|
|
|
@random_ratio = 0.1
|
2015-06-25 23:32:08 -04:00
|
|
|
unless options && options[:skip_runner]
|
2014-02-05 18:14:41 -05:00
|
|
|
@runner = Runner.new(self)
|
|
|
|
self.class.current = self
|
|
|
|
end
|
2015-06-25 23:32:08 -04:00
|
|
|
|
|
|
|
@hostname = options && options[:hostname]
|
2014-01-30 00:21:38 -05:00
|
|
|
@manager_id = SecureRandom.hex
|
2016-05-29 22:28:05 -04:00
|
|
|
|
|
|
|
if options && options.key?(:enable_stats)
|
|
|
|
@enable_stats = options[:enable_stats]
|
|
|
|
else
|
|
|
|
@enable_stats = true
|
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
2014-02-05 18:14:41 -05:00
|
|
|
def self.current
|
|
|
|
@current
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.current=(manager)
|
|
|
|
@current = manager
|
|
|
|
end
|
|
|
|
|
2015-06-25 23:32:08 -04:00
|
|
|
def hostname
|
|
|
|
@hostname ||= `hostname`.strip
|
|
|
|
end
|
|
|
|
|
2014-01-30 00:21:38 -05:00
|
|
|
def schedule_info(klass)
|
|
|
|
ScheduleInfo.new(klass, self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def next_run(klass)
|
|
|
|
schedule_info(klass).next_run
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_schedule!(klass)
|
|
|
|
lock do
|
|
|
|
schedule_info(klass).schedule!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove(klass)
|
|
|
|
lock do
|
|
|
|
schedule_info(klass).del!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-11 21:32:34 -05:00
|
|
|
def reschedule_orphans!
|
|
|
|
lock do
|
2015-06-25 23:32:08 -04:00
|
|
|
reschedule_orphans_on!
|
|
|
|
reschedule_orphans_on!(hostname)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def reschedule_orphans_on!(hostname = nil)
|
2015-06-25 23:32:08 -04:00
|
|
|
redis.zrange(Manager.queue_key(hostname), 0, -1).each do |key|
|
|
|
|
klass = get_klass(key)
|
|
|
|
next unless klass
|
|
|
|
info = schedule_info(klass)
|
|
|
|
|
|
|
|
if ['QUEUED', 'RUNNING'].include?(info.prev_result) &&
|
|
|
|
(info.current_owner.blank? || !redis.get(info.current_owner))
|
|
|
|
info.prev_result = 'ORPHAN'
|
|
|
|
info.next_run = Time.now.to_i
|
|
|
|
info.write!
|
2014-02-11 21:32:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_klass(name)
|
|
|
|
name.constantize
|
|
|
|
rescue NameError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2014-01-30 00:21:38 -05:00
|
|
|
def tick
|
|
|
|
lock do
|
2015-06-25 23:32:08 -04:00
|
|
|
schedule_next_job
|
|
|
|
schedule_next_job(hostname)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def schedule_next_job(hostname = nil)
|
2015-06-25 23:32:08 -04:00
|
|
|
(key, due), _ = redis.zrange Manager.queue_key(hostname), 0, 0, withscores: true
|
|
|
|
return unless key
|
2016-01-11 12:31:28 -05:00
|
|
|
|
2015-06-25 23:32:08 -04:00
|
|
|
if due.to_i <= Time.now.to_i
|
|
|
|
klass = get_klass(key)
|
|
|
|
unless klass
|
|
|
|
# corrupt key, nuke it (renamed job or something)
|
|
|
|
redis.zrem Manager.queue_key(hostname), key
|
|
|
|
return
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
2015-06-25 23:32:08 -04:00
|
|
|
info = schedule_info(klass)
|
|
|
|
info.prev_run = Time.now.to_i
|
|
|
|
info.prev_result = "QUEUED"
|
|
|
|
info.prev_duration = -1
|
|
|
|
info.next_run = nil
|
|
|
|
info.current_owner = identity_key
|
|
|
|
info.schedule!
|
|
|
|
@runner.enq(klass)
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def blocking_tick
|
|
|
|
tick
|
|
|
|
@runner.wait_till_done
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop!
|
|
|
|
@runner.stop!
|
2014-02-05 18:14:41 -05:00
|
|
|
self.class.current = nil
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
2014-02-11 21:32:34 -05:00
|
|
|
def keep_alive_duration
|
|
|
|
60
|
|
|
|
end
|
|
|
|
|
|
|
|
def keep_alive
|
|
|
|
redis.setex identity_key, keep_alive_duration, ""
|
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
|
|
|
|
def lock
|
2014-04-11 01:43:33 -04:00
|
|
|
DistributedMutex.new(Manager.lock_key).synchronize do
|
|
|
|
yield
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-05 18:14:41 -05:00
|
|
|
def self.discover_schedules
|
2014-04-17 01:57:17 -04:00
|
|
|
# hack for developemnt reloader is crazytown
|
|
|
|
# multiple classes with same name can be in
|
|
|
|
# object space
|
|
|
|
unique = Set.new
|
2014-02-05 18:14:41 -05:00
|
|
|
schedules = []
|
|
|
|
ObjectSpace.each_object(Scheduler::Schedule) do |schedule|
|
2014-04-17 01:57:17 -04:00
|
|
|
if schedule.scheduled?
|
|
|
|
next if unique.include?(schedule.to_s)
|
|
|
|
schedules << schedule
|
|
|
|
unique << schedule.to_s
|
|
|
|
end
|
2014-02-05 18:14:41 -05:00
|
|
|
end
|
|
|
|
schedules
|
|
|
|
end
|
|
|
|
|
2014-02-11 21:32:34 -05:00
|
|
|
@mutex = Mutex.new
|
|
|
|
def self.seq
|
|
|
|
@mutex.synchronize do
|
|
|
|
@i ||= 0
|
|
|
|
@i += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def identity_key
|
2015-06-25 23:32:08 -04:00
|
|
|
@identity_key ||= "_scheduler_#{hostname}:#{Process.pid}:#{self.class.seq}:#{SecureRandom.hex}"
|
2014-02-11 21:32:34 -05:00
|
|
|
end
|
|
|
|
|
2014-01-30 00:21:38 -05:00
|
|
|
def self.lock_key
|
|
|
|
"_scheduler_lock_"
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.queue_key(hostname = nil)
|
2015-06-25 23:32:08 -04:00
|
|
|
if hostname
|
|
|
|
"_scheduler_queue_#{hostname}_"
|
|
|
|
else
|
|
|
|
"_scheduler_queue_"
|
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.schedule_key(klass, hostname = nil)
|
2015-06-25 23:32:08 -04:00
|
|
|
if hostname
|
|
|
|
"_scheduler_#{klass}_#{hostname}"
|
|
|
|
else
|
|
|
|
"_scheduler_#{klass}"
|
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|