2014-08-19 00:04:58 -04:00
|
|
|
require 'thread'
|
|
|
|
|
|
|
|
class SidekiqPauser
|
2019-02-13 20:22:40 -05:00
|
|
|
TTL = 60
|
|
|
|
PAUSED_KEY = "sidekiq_is_paused_v2"
|
|
|
|
|
2014-08-19 00:04:58 -04:00
|
|
|
def initialize
|
|
|
|
@mutex = Mutex.new
|
2019-02-13 20:22:40 -05:00
|
|
|
@dbs ||= Set.new
|
2014-08-19 00:04:58 -04:00
|
|
|
end
|
|
|
|
|
2019-02-18 21:55:53 -05:00
|
|
|
def pause!(value = "paused")
|
|
|
|
$redis.setex PAUSED_KEY, TTL, value
|
2019-02-19 20:46:08 -05:00
|
|
|
extend_lease_thread
|
2014-02-12 23:27:04 -05:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2014-08-19 00:04:58 -04:00
|
|
|
def paused?
|
2019-02-13 20:22:40 -05:00
|
|
|
!!$redis.get(PAUSED_KEY)
|
2014-02-12 23:27:04 -05:00
|
|
|
end
|
|
|
|
|
2019-02-13 21:33:58 -05:00
|
|
|
def unpause_all!
|
|
|
|
@mutex.synchronize do
|
|
|
|
@dbs = Set.new
|
|
|
|
stop_extend_lease_thread
|
|
|
|
end
|
|
|
|
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do
|
|
|
|
unpause! if paused?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def paused_dbs
|
|
|
|
dbs = []
|
2019-02-18 21:55:53 -05:00
|
|
|
|
2019-02-13 21:33:58 -05:00
|
|
|
RailsMultisite::ConnectionManagement.each_connection do
|
|
|
|
dbs << RailsMultisite::ConnectionManagement.current_db if paused?
|
|
|
|
end
|
2019-02-18 21:55:53 -05:00
|
|
|
|
2019-02-13 21:33:58 -05:00
|
|
|
dbs
|
|
|
|
end
|
|
|
|
|
2014-08-19 00:04:58 -04:00
|
|
|
def unpause!
|
|
|
|
@mutex.synchronize do
|
2019-02-13 20:22:40 -05:00
|
|
|
@dbs.delete(RailsMultisite::ConnectionManagement.current_db)
|
2019-02-13 21:33:58 -05:00
|
|
|
stop_extend_lease_thread if @dbs.size == 0
|
2014-08-19 00:04:58 -04:00
|
|
|
end
|
|
|
|
|
2019-02-13 20:22:40 -05:00
|
|
|
$redis.del(PAUSED_KEY)
|
2014-02-12 23:27:04 -05:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-02-13 21:33:58 -05:00
|
|
|
def stop_extend_lease_thread
|
|
|
|
# should always be called from a mutex
|
|
|
|
if t = @extend_lease_thread
|
|
|
|
@extend_lease_thread = nil
|
|
|
|
while t.alive?
|
2019-02-19 00:33:34 -05:00
|
|
|
begin
|
|
|
|
t.wakeup
|
|
|
|
rescue ThreadError => e
|
|
|
|
unless e.message =~ /killed thread/
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-13 21:33:58 -05:00
|
|
|
sleep 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-19 01:50:17 -04:00
|
|
|
def extend_lease_thread
|
2019-02-19 20:46:08 -05:00
|
|
|
@mutex.synchronize do
|
|
|
|
@dbs << RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
@extend_lease_thread ||= Thread.new do
|
|
|
|
while true do
|
2019-02-19 21:25:43 -05:00
|
|
|
break if !@extend_lease_thread
|
2019-02-19 20:46:08 -05:00
|
|
|
|
2019-02-19 21:25:43 -05:00
|
|
|
@mutex.synchronize do
|
2019-02-19 20:46:08 -05:00
|
|
|
@dbs.each do |db|
|
|
|
|
RailsMultisite::ConnectionManagement.with_connection(db) do
|
|
|
|
if !$redis.expire(PAUSED_KEY, TTL)
|
|
|
|
# if it was unpaused in another process we got to remove the
|
|
|
|
# bad key
|
|
|
|
@dbs.delete(db)
|
|
|
|
end
|
|
|
|
end
|
2019-02-13 21:33:58 -05:00
|
|
|
end
|
2019-02-13 20:22:40 -05:00
|
|
|
end
|
2014-08-19 01:50:17 -04:00
|
|
|
|
2019-02-19 20:46:08 -05:00
|
|
|
sleep(Rails.env.test? ? 0.01 : TTL / 2)
|
|
|
|
end
|
2019-02-13 20:22:40 -05:00
|
|
|
end
|
|
|
|
end
|
2014-02-12 23:27:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-19 00:04:58 -04:00
|
|
|
module Sidekiq
|
|
|
|
@pauser = SidekiqPauser.new
|
2019-02-18 21:55:53 -05:00
|
|
|
|
|
|
|
def self.pause!(key = nil)
|
|
|
|
key ? @pauser.pause!(key) : @pauser.pause!
|
2014-08-19 00:04:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.paused?
|
|
|
|
@pauser.paused?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.unpause!
|
|
|
|
@pauser.unpause!
|
|
|
|
end
|
2019-02-13 21:33:58 -05:00
|
|
|
|
|
|
|
def self.unpause_all!
|
|
|
|
@pauser.unpause_all!
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.paused_dbs
|
|
|
|
@pauser.paused_dbs
|
|
|
|
end
|
2014-08-19 00:04:58 -04:00
|
|
|
end
|
|
|
|
|
2014-02-12 23:27:04 -05:00
|
|
|
# server middleware that will reschedule work whenever Sidekiq is paused
|
|
|
|
class Sidekiq::Pausable
|
|
|
|
|
|
|
|
def initialize(delay = 5.seconds)
|
|
|
|
@delay = delay
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(worker, msg, queue)
|
2019-02-13 20:22:40 -05:00
|
|
|
if sidekiq_paused?(msg) && !(Jobs::RunHeartbeat === worker)
|
2016-01-11 12:31:28 -05:00
|
|
|
worker.class.perform_in(@delay, *msg['args'])
|
2014-02-12 23:27:04 -05:00
|
|
|
else
|
2017-10-23 02:30:17 -04:00
|
|
|
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
|
|
result = yield
|
|
|
|
duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
|
|
|
|
DiscourseEvent.trigger(:sidekiq_job_ran, worker, msg, queue, duration)
|
|
|
|
result
|
2014-02-12 23:27:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-13 20:22:40 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def sidekiq_paused?(msg)
|
|
|
|
if site_id = msg["args"]&.first&.dig("current_site_id")
|
|
|
|
RailsMultisite::ConnectionManagement.with_connection(site_id) do
|
|
|
|
Sidekiq.paused?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-12 23:27:04 -05:00
|
|
|
end
|