2019-08-20 11:25:04 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
class SidekiqMonitor < Daemon
|
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
NOTIFICATION_CHANNEL = 'sidekiq:cancel:notifications'.freeze
|
|
|
|
CANCEL_DEADLINE = 24.hours.seconds
|
2019-08-21 05:32:45 -04:00
|
|
|
RECONNECT_TIME = 3.seconds
|
2019-08-20 11:25:04 -04:00
|
|
|
|
|
|
|
# We use exception derived from `Exception`
|
|
|
|
# to consider this as an very low-level exception
|
|
|
|
# that should not be caught by application
|
|
|
|
CancelledError = Class.new(Exception) # rubocop:disable Lint/InheritException
|
|
|
|
|
|
|
|
attr_reader :jobs_thread
|
|
|
|
attr_reader :jobs_mutex
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super
|
|
|
|
|
|
|
|
@jobs_thread = {}
|
|
|
|
@jobs_mutex = Mutex.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def within_job(jid, queue)
|
|
|
|
jobs_mutex.synchronize do
|
|
|
|
jobs_thread[jid] = Thread.current
|
|
|
|
end
|
|
|
|
|
|
|
|
if cancelled?(jid)
|
|
|
|
Sidekiq.logger.warn(
|
2019-08-21 06:03:42 -04:00
|
|
|
class: self.class.to_s,
|
2019-08-20 11:25:04 -04:00
|
|
|
action: 'run',
|
|
|
|
queue: queue,
|
|
|
|
jid: jid,
|
2019-08-21 05:32:45 -04:00
|
|
|
canceled: true
|
|
|
|
)
|
2019-08-20 11:25:04 -04:00
|
|
|
raise CancelledError
|
|
|
|
end
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
jobs_mutex.synchronize do
|
|
|
|
jobs_thread.delete(jid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-21 05:32:45 -04:00
|
|
|
def self.cancel_job(jid)
|
|
|
|
payload = {
|
|
|
|
action: 'cancel',
|
|
|
|
jid: jid
|
|
|
|
}.to_json
|
|
|
|
|
|
|
|
::Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.setex(cancel_job_key(jid), CANCEL_DEADLINE, 1)
|
|
|
|
redis.publish(NOTIFICATION_CHANNEL, payload)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-08-20 11:25:04 -04:00
|
|
|
def start_working
|
|
|
|
Sidekiq.logger.info(
|
2019-08-21 06:03:42 -04:00
|
|
|
class: self.class.to_s,
|
2019-08-20 11:25:04 -04:00
|
|
|
action: 'start',
|
2019-08-21 05:32:45 -04:00
|
|
|
message: 'Starting Monitor Daemon'
|
|
|
|
)
|
|
|
|
|
|
|
|
while enabled?
|
|
|
|
process_messages
|
|
|
|
sleep(RECONNECT_TIME)
|
|
|
|
end
|
2019-08-20 11:25:04 -04:00
|
|
|
|
2019-08-21 05:32:45 -04:00
|
|
|
ensure
|
|
|
|
Sidekiq.logger.warn(
|
2019-08-21 06:03:42 -04:00
|
|
|
class: self.class.to_s,
|
2019-08-21 05:32:45 -04:00
|
|
|
action: 'stop',
|
|
|
|
message: 'Stopping Monitor Daemon'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop_working
|
|
|
|
thread.raise(Interrupt) if thread.alive?
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_messages
|
2019-08-20 11:25:04 -04:00
|
|
|
::Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.subscribe(NOTIFICATION_CHANNEL) do |on|
|
|
|
|
on.message do |channel, message|
|
|
|
|
process_message(message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
|
|
Sidekiq.logger.warn(
|
2019-08-21 06:03:42 -04:00
|
|
|
class: self.class.to_s,
|
2019-08-20 11:25:04 -04:00
|
|
|
action: 'exception',
|
2019-08-21 05:32:45 -04:00
|
|
|
message: e.message
|
|
|
|
)
|
2019-08-20 11:25:04 -04:00
|
|
|
|
2019-08-21 05:32:45 -04:00
|
|
|
# we re-raise system exceptions
|
|
|
|
raise e unless e.is_a?(StandardError)
|
2019-08-20 11:25:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def process_message(message)
|
|
|
|
Sidekiq.logger.info(
|
2019-08-21 06:03:42 -04:00
|
|
|
class: self.class.to_s,
|
2019-08-20 11:25:04 -04:00
|
|
|
channel: NOTIFICATION_CHANNEL,
|
|
|
|
message: 'Received payload on channel',
|
2019-08-21 05:32:45 -04:00
|
|
|
payload: message
|
|
|
|
)
|
2019-08-20 11:25:04 -04:00
|
|
|
|
|
|
|
message = safe_parse(message)
|
|
|
|
return unless message
|
|
|
|
|
|
|
|
case message['action']
|
|
|
|
when 'cancel'
|
|
|
|
process_job_cancel(message['jid'])
|
|
|
|
else
|
|
|
|
# unknown message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def safe_parse(message)
|
|
|
|
JSON.parse(message)
|
|
|
|
rescue JSON::ParserError
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_job_cancel(jid)
|
|
|
|
return unless jid
|
|
|
|
|
2019-08-20 15:09:04 -04:00
|
|
|
# try to find thread without lock
|
|
|
|
return unless find_thread_unsafe(jid)
|
2019-08-20 11:25:04 -04:00
|
|
|
|
2019-08-20 15:09:04 -04:00
|
|
|
Thread.new do
|
|
|
|
# try to find a thread, but with guaranteed
|
2019-08-21 05:32:45 -04:00
|
|
|
# that handle for thread corresponds to actually
|
|
|
|
# running job
|
2019-08-20 15:09:04 -04:00
|
|
|
find_thread_with_lock(jid) do |thread|
|
2019-08-20 11:25:04 -04:00
|
|
|
Sidekiq.logger.warn(
|
2019-08-21 06:03:42 -04:00
|
|
|
class: self.class.to_s,
|
2019-08-20 11:25:04 -04:00
|
|
|
action: 'cancel',
|
|
|
|
message: 'Canceling thread with CancelledError',
|
|
|
|
jid: jid,
|
2019-08-21 05:32:45 -04:00
|
|
|
thread_id: thread.object_id
|
|
|
|
)
|
2019-08-20 11:25:04 -04:00
|
|
|
|
|
|
|
thread&.raise(CancelledError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# This method needs to be thread-safe
|
|
|
|
# This is why it passes thread in block,
|
|
|
|
# to ensure that we do process this thread
|
2019-08-20 15:09:04 -04:00
|
|
|
def find_thread_unsafe(jid)
|
|
|
|
jobs_thread[jid]
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_thread_with_lock(jid)
|
|
|
|
# don't try to lock if we cannot find the thread
|
|
|
|
return unless find_thread_unsafe(jid)
|
2019-08-20 11:25:04 -04:00
|
|
|
|
|
|
|
jobs_mutex.synchronize do
|
2019-08-20 15:09:04 -04:00
|
|
|
find_thread_unsafe(jid).tap do |thread|
|
|
|
|
yield(thread) if thread
|
|
|
|
end
|
2019-08-20 11:25:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cancelled?(jid)
|
|
|
|
::Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.exists(self.class.cancel_job_key(jid))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cancel_job_key(jid)
|
|
|
|
"sidekiq:cancel:#{jid}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|