2019-05-03 08:17:27 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-01 23:57:50 +01:00
|
|
|
require "demon/base"
|
2013-10-10 13:33:52 +11:00
|
|
|
|
|
|
|
class Demon::Sidekiq < Demon::Base
|
2019-08-30 10:12:10 +10:00
|
|
|
RANDOM_HEX = SecureRandom.hex
|
|
|
|
QUEUE_IDS = []
|
|
|
|
|
|
|
|
def self.queues_last_heartbeat_hash_key
|
|
|
|
@@queues_last_heartbeat_hash_key ||= "#{RANDOM_HEX}_queues_last_heartbeat_hash"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.trigger_heartbeat(name)
|
|
|
|
$redis.hset(queues_last_heartbeat_hash_key, name, Time.new.to_i.to_s)
|
|
|
|
extend_expiry(queues_last_heartbeat_hash_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_queue_last_heartbeat(name)
|
|
|
|
extend_expiry(queues_last_heartbeat_hash_key)
|
|
|
|
$redis.hget(queues_last_heartbeat_hash_key, name).to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_heartbeat_queues!
|
|
|
|
$redis.del(queues_last_heartbeat_hash_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.before_start(count)
|
|
|
|
# cleans up heartbeat queues from previous boot up
|
|
|
|
Sidekiq::Queue.all.each { |queue| queue.clear if queue.name[/^\h{32}$/] }
|
|
|
|
count.times do
|
|
|
|
QUEUE_IDS << SecureRandom.hex
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.extend_expiry(key)
|
|
|
|
$redis.expire(key, 60 * 60)
|
|
|
|
end
|
2013-11-01 23:57:50 +01:00
|
|
|
|
2013-10-10 13:33:52 +11:00
|
|
|
def self.prefix
|
|
|
|
"sidekiq"
|
|
|
|
end
|
|
|
|
|
FEATURE: set UNICORN_STATS_SOCKET_DIR for status socket
eg:
sam@ubuntu stats_sockets % socat - UNIX-CONNECT:9622.sock
gc_stat
{"count":46,"heap_allocated_pages":2459,"heap_sorted_length":2460,"heap_allocatable_pages":0,"heap_available_slots":1002267,"heap_live_slots":647293,"heap_free_slots":354974,"heap_final_slots":0,"heap_marked_slots":503494,"heap_swept_slots":498773,"heap_eden_pages":2459,"heap_tomb_pages":0,"total_allocated_pages":2459,"total_freed_pages":0,"total_allocated_objects":4337014,"total_freed_objects":3689721,"malloc_increase_bytes":6448248,"malloc_increase_bytes_limit":29188387,"minor_gc_count":36,"major_gc_count":10,"remembered_wb_unprotected_objects":19958,"remembered_wb_unprotected_objects_limit":39842,"old_objects":462019,"old_objects_limit":895782,"oldmalloc_increase_bytes":6448696,"oldmalloc_increase_bytes_limit":19350882}
2017-04-21 11:36:51 -04:00
|
|
|
def self.after_fork(&blk)
|
2017-07-28 10:20:09 +09:00
|
|
|
blk ? (@blk = blk) : @blk
|
FEATURE: set UNICORN_STATS_SOCKET_DIR for status socket
eg:
sam@ubuntu stats_sockets % socat - UNIX-CONNECT:9622.sock
gc_stat
{"count":46,"heap_allocated_pages":2459,"heap_sorted_length":2460,"heap_allocatable_pages":0,"heap_available_slots":1002267,"heap_live_slots":647293,"heap_free_slots":354974,"heap_final_slots":0,"heap_marked_slots":503494,"heap_swept_slots":498773,"heap_eden_pages":2459,"heap_tomb_pages":0,"total_allocated_pages":2459,"total_freed_pages":0,"total_allocated_objects":4337014,"total_freed_objects":3689721,"malloc_increase_bytes":6448248,"malloc_increase_bytes_limit":29188387,"minor_gc_count":36,"major_gc_count":10,"remembered_wb_unprotected_objects":19958,"remembered_wb_unprotected_objects_limit":39842,"old_objects":462019,"old_objects_limit":895782,"oldmalloc_increase_bytes":6448696,"oldmalloc_increase_bytes_limit":19350882}
2017-04-21 11:36:51 -04:00
|
|
|
end
|
|
|
|
|
2019-08-30 10:12:10 +10:00
|
|
|
def run
|
|
|
|
@identifier = QUEUE_IDS[@index]
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2013-10-10 13:33:52 +11:00
|
|
|
private
|
|
|
|
|
2014-04-17 15:57:17 +10:00
|
|
|
def suppress_stdout
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def suppress_stderr
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2013-10-10 13:33:52 +11:00
|
|
|
def after_fork
|
FEATURE: set UNICORN_STATS_SOCKET_DIR for status socket
eg:
sam@ubuntu stats_sockets % socat - UNIX-CONNECT:9622.sock
gc_stat
{"count":46,"heap_allocated_pages":2459,"heap_sorted_length":2460,"heap_allocatable_pages":0,"heap_available_slots":1002267,"heap_live_slots":647293,"heap_free_slots":354974,"heap_final_slots":0,"heap_marked_slots":503494,"heap_swept_slots":498773,"heap_eden_pages":2459,"heap_tomb_pages":0,"total_allocated_pages":2459,"total_freed_pages":0,"total_allocated_objects":4337014,"total_freed_objects":3689721,"malloc_increase_bytes":6448248,"malloc_increase_bytes_limit":29188387,"minor_gc_count":36,"major_gc_count":10,"remembered_wb_unprotected_objects":19958,"remembered_wb_unprotected_objects_limit":39842,"old_objects":462019,"old_objects_limit":895782,"oldmalloc_increase_bytes":6448696,"oldmalloc_increase_bytes_limit":19350882}
2017-04-21 11:36:51 -04:00
|
|
|
Demon::Sidekiq.after_fork&.call
|
|
|
|
|
2017-11-03 16:57:48 +08:00
|
|
|
puts "Loading Sidekiq in process id #{Process.pid}"
|
2013-10-10 13:33:52 +11:00
|
|
|
require 'sidekiq/cli'
|
2014-06-03 17:17:10 +10:00
|
|
|
# CLI will close the logger, if we have one set we can be in big
|
|
|
|
# trouble, if STDOUT is closed in our process all sort of weird
|
|
|
|
# will ensue, resetting the logger ensures it will reinit correctly
|
|
|
|
# parent process is in charge of the file anyway.
|
|
|
|
Sidekiq::Logging.logger = nil
|
2013-11-01 23:57:50 +01:00
|
|
|
cli = Sidekiq::CLI.instance
|
2014-04-17 15:57:17 +10:00
|
|
|
|
2017-04-27 15:28:23 +08:00
|
|
|
options = ["-c", GlobalSetting.sidekiq_workers.to_s]
|
|
|
|
|
2019-08-30 10:12:10 +10:00
|
|
|
[['critical', 8], [@identifier, 8], ['default', 4], ['low', 2], ['ultra_low', 1]].each do |queue_name, weight|
|
2017-04-27 15:28:23 +08:00
|
|
|
custom_queue_hostname = ENV["UNICORN_SIDEKIQ_#{queue_name.upcase}_QUEUE_HOSTNAME"]
|
|
|
|
|
2017-06-19 14:45:51 +09:00
|
|
|
if !custom_queue_hostname || custom_queue_hostname.split(',').include?(`hostname`.strip)
|
2017-04-27 15:28:23 +08:00
|
|
|
options << "-q"
|
|
|
|
options << "#{queue_name},#{weight}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-09 09:29:14 +11:00
|
|
|
# Sidekiq not as high priority as web, in this environment it is forked so a web is very
|
|
|
|
# likely running
|
|
|
|
Discourse::Utils.execute_command('renice', '-n', '5', '-p', Process.pid.to_s)
|
|
|
|
|
2017-04-27 15:28:23 +08:00
|
|
|
cli.parse(options)
|
2015-12-09 11:48:41 +11:00
|
|
|
load Rails.root + "config/initializers/100-sidekiq.rb"
|
2013-11-01 23:57:50 +01:00
|
|
|
cli.run
|
|
|
|
rescue => e
|
|
|
|
STDERR.puts e.message
|
|
|
|
STDERR.puts e.backtrace.join("\n")
|
|
|
|
exit 1
|
2013-10-10 13:33:52 +11:00
|
|
|
end
|
2013-11-01 23:57:50 +01:00
|
|
|
|
2013-10-10 13:33:52 +11:00
|
|
|
end
|