Remove daemonization, pidfile and logfile options, #4045

This commit is contained in:
Mike Perham 2018-12-07 13:41:26 -08:00
parent 5904d4ead3
commit 5987a8c352
8 changed files with 10 additions and 253 deletions

View File

@ -4,95 +4,18 @@ require 'fileutils'
require 'sidekiq/api'
class Sidekiqctl
DEFAULT_KILL_TIMEOUT = 30
CMD = File.basename($0)
attr_reader :stage, :pidfile, :kill_timeout
def self.print_usage
puts "#{CMD} - control Sidekiq from the command line."
puts
puts "Usage: #{CMD} quiet <pidfile> <kill_timeout>"
puts " #{CMD} stop <pidfile> <kill_timeout>"
puts " #{CMD} status <section>"
puts "Usage: #{CMD} status <section>"
puts
puts " <pidfile> is path to a pidfile"
puts " <kill_timeout> is number of seconds to wait until Sidekiq exits"
puts " (default: #{Sidekiqctl::DEFAULT_KILL_TIMEOUT}), after which Sidekiq will be KILL'd"
puts
puts " <section> (optional) view a specific section of the status output"
puts " Valid sections are: #{Sidekiqctl::Status::VALID_SECTIONS.join(', ')}"
puts
puts "Be sure to set the kill_timeout LONGER than Sidekiq's -t timeout. If you want"
puts "to wait 60 seconds for jobs to finish, use `sidekiq -t 60` and `sidekiqctl stop"
puts " path_to_pidfile 61`"
puts
end
def initialize(stage, pidfile, timeout)
@stage = stage
@pidfile = pidfile
@kill_timeout = timeout
done('No pidfile given', :error) if !pidfile
done("Pidfile #{pidfile} does not exist", :warn) if !File.exist?(pidfile)
done('Invalid pidfile content', :error) if pid == 0
fetch_process
begin
send(stage)
rescue NoMethodError
done "Invalid command: #{stage}", :error
end
end
def fetch_process
Process.kill(0, pid)
rescue Errno::ESRCH
done "Process doesn't exist", :error
# We were not allowed to send a signal, but the process must have existed
# when Process.kill() was called.
rescue Errno::EPERM
return pid
end
def done(msg, error = nil)
puts msg
exit(exit_signal(error))
end
def exit_signal(error)
(error == :error) ? 1 : 0
end
def pid
@pid ||= File.read(pidfile).to_i
end
def quiet
`kill -TSTP #{pid}`
end
def stop
`kill -TERM #{pid}`
kill_timeout.times do
begin
Process.kill(0, pid)
rescue Errno::ESRCH
FileUtils.rm_f pidfile
done 'Sidekiq shut down gracefully.'
rescue Errno::EPERM
done 'Not permitted to shut down Sidekiq.'
end
sleep 1
end
`kill -9 #{pid}`
FileUtils.rm_f pidfile
done 'Sidekiq shut down forcefully.'
end
alias_method :shutdown, :stop
class Status
VALID_SECTIONS = %w[all version overview processes queues]
def display(section = nil)
@ -227,11 +150,4 @@ end
if ARGV.length < 2
Sidekiqctl.print_usage
else
stage = ARGV[0]
pidfile = ARGV[1]
timeout = ARGV[2].to_i
timeout = Sidekiqctl::DEFAULT_KILL_TIMEOUT if timeout == 0
Sidekiqctl.new(stage, pidfile, timeout)
end

View File

@ -41,8 +41,6 @@ module Sidekiq
# global process state irreversibly. PRs which improve the
# test coverage of Sidekiq::CLI are welcomed.
def run
daemonize if options[:daemon]
write_pid
boot_system
print_banner if environment == 'development' && $stdout.tty?
@ -96,7 +94,7 @@ module Sidekiq
end
def launch(self_read)
if !options[:daemon]
if environment == 'development' && $stdout.tty?
logger.info 'Starting processing, hit Ctrl-C to stop'
end
@ -150,12 +148,6 @@ module Sidekiq
Sidekiq.logger.info "Received TSTP, no longer accepting new work"
cli.launcher.quiet
},
'USR2' => ->(cli) {
if Sidekiq.options[:logfile]
Sidekiq.logger.info "Received USR2, reopening log file"
Sidekiq::Logging.reopen_logs
end
},
'TTIN' => ->(cli) {
Thread.list.each do |thread|
Sidekiq.logger.warn "Thread TID-#{(thread.object_id ^ ::Process.pid).to_s(36)} #{thread['sidekiq_label']}"
@ -186,31 +178,6 @@ module Sidekiq
puts "\e[0m"
end
def daemonize
raise ArgumentError, "You really should set a logfile if you're going to daemonize" unless options[:logfile]
files_to_reopen = ObjectSpace.each_object(File).reject { |f| f.closed? }
::Process.daemon(true, true)
files_to_reopen.each do |file|
begin
file.reopen file.path, "a+"
file.sync = true
rescue ::Exception
end
end
[$stdout, $stderr].each do |io|
File.open(options[:logfile], 'ab') do |f|
io.reopen(f)
end
io.sync = true
end
$stdin.reopen('/dev/null')
initialize_logger
end
def set_environment(cli_env)
@environment = cli_env || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
end
@ -307,8 +274,7 @@ module Sidekiq
end
o.on '-d', '--daemon', "Daemonize process" do |arg|
opts[:daemon] = arg
puts "WARNING: Daemonization mode will be removed in Sidekiq 6.0, please use a proper process supervisor to start and manage your services"
puts "WARNING: Daemonization mode was removed in Sidekiq 6.0, please use a proper process supervisor to start and manage your services"
end
o.on '-e', '--environment ENV', "Application environment" do |arg|
@ -319,12 +285,6 @@ module Sidekiq
opts[:tag] = arg
end
# this index remains here for backwards compatibility but none of the Sidekiq
# family use this value anymore. it was used by Pro's original reliable_fetch.
o.on '-i', '--index INT', "unique process index on this machine" do |arg|
opts[:index] = Integer(arg.match(/\d+/)[0])
end
o.on "-q", "--queue QUEUE[,WEIGHT]", "Queues to process with optional weights" do |arg|
queue, weight = arg.split(",")
parse_queue opts, queue, weight
@ -347,13 +307,11 @@ module Sidekiq
end
o.on '-L', '--logfile PATH', "path to writable logfile" do |arg|
opts[:logfile] = arg
puts "WARNING: Logfile redirection will be removed in Sidekiq 6.0, Sidekiq will only log to STDOUT"
puts "WARNING: Logfile redirection was removed in Sidekiq 6.0, Sidekiq will only log to STDOUT"
end
o.on '-P', '--pidfile PATH', "path to pidfile" do |arg|
opts[:pidfile] = arg
puts "WARNING: PID file creation will be removed in Sidekiq 6.0, please use a proper process supervisor to start and manage your services"
puts "WARNING: PID file creation was removed in Sidekiq 6.0, please use a proper process supervisor to start and manage your services"
end
o.on '-V', '--version', "Print version and exit" do |arg|
@ -377,20 +335,11 @@ module Sidekiq
end
def initialize_logger
Sidekiq::Logging.initialize_logger(options[:logfile]) if options[:logfile]
Sidekiq::Logging.initialize_logger
Sidekiq.logger.level = ::Logger::DEBUG if options[:verbose]
end
def write_pid
if path = options[:pidfile]
pidfile = File.expand_path(path)
File.open(pidfile, 'w') do |f|
f.puts ::Process.pid
end
end
end
def parse_config(cfile)
opts = {}
if File.exist?(cfile)

View File

@ -50,12 +50,11 @@ module Sidekiq
Thread.current[:sidekiq_context].pop
end
def self.initialize_logger(log_target = STDOUT)
oldlogger = defined?(@logger) ? @logger : nil
@logger = Logger.new(log_target)
def self.initialize_logger
return @logger if defined?(@logger)
@logger = Logger.new(STDOUT)
@logger.level = Logger::INFO
@logger.formatter = ENV['DYNO'] ? WithoutTimestamp.new : Pretty.new
oldlogger.close if oldlogger && !$TESTING # don't want to close testing's STDOUT logging
@logger
end
@ -67,54 +66,6 @@ module Sidekiq
@logger = (log ? log : Logger.new(File::NULL))
end
# This reopens ALL logfiles in the process that have been rotated
# using logrotate(8) (without copytruncate) or similar tools.
# A +File+ object is considered for reopening if it is:
# 1) opened with the O_APPEND and O_WRONLY flags
# 2) the current open file handle does not match its original open path
# 3) unbuffered (as far as userspace buffering goes, not O_SYNC)
# Returns the number of files reopened
def self.reopen_logs
to_reopen = []
append_flags = File::WRONLY | File::APPEND
ObjectSpace.each_object(File) do |fp|
begin
if !fp.closed? && fp.stat.file? && fp.sync && (fp.fcntl(Fcntl::F_GETFL) & append_flags) == append_flags
to_reopen << fp
end
rescue IOError, Errno::EBADF
end
end
nr = 0
to_reopen.each do |fp|
orig_st = begin
fp.stat
rescue IOError, Errno::EBADF
next
end
begin
b = File.stat(fp.path)
next if orig_st.ino == b.ino && orig_st.dev == b.dev
rescue Errno::ENOENT
end
begin
File.open(fp.path, 'a') { |tmpfp| fp.reopen(tmpfp) }
fp.sync = true
nr += 1
rescue IOError, Errno::EBADF
# not much we can do...
end
end
nr
rescue RuntimeError => ex
# RuntimeError: ObjectSpace is disabled; each_object will only work with Class, pass -X+O to enable
puts "Unable to reopen logs: #{ex.message}"
end
def logger
Sidekiq::Logging.logger
end

View File

@ -1,8 +1,6 @@
---
:verbose: false
:require: ./test/fake_env.rb
:pidfile: /tmp/sidekiq-config-test.pid
:logfile: /tmp/sidekiq.log
:concurrency: 50
:queues:
- [<%="very_"%>often, 2]

View File

@ -1,11 +1,9 @@
---
:pidfile: /tmp/sidekiq-config-test.pid
:concurrency: 50
staging:
:verbose: false
:require: ./test/fake_env.rb
:logfile: /tmp/sidekiq.log
:concurrency: 50
:queues:
- [<%="very_"%>often, 2]

View File

@ -1,8 +1,6 @@
---
verbose: false
require: ./test/fake_env.rb
pidfile: /tmp/sidekiq-config-test.pid
logfile: /tmp/sidekiq.log
concurrency: 50
queues:
- [<%="very_"%>often, 2]

View File

@ -119,20 +119,6 @@ class TestCLI < Minitest::Test
end
end
describe 'process index' do
it 'accepts with -i' do
@cli.parse(%w[sidekiq -i 7 -r ./test/fake_env.rb])
assert_equal 7, Sidekiq.options[:index]
end
it 'accepts stringy value' do
@cli.parse(%w[sidekiq -i worker.7 -r ./test/fake_env.rb])
assert_equal 7, Sidekiq.options[:index]
end
end
describe 'timeout' do
it 'accepts with -t' do
@cli.parse(%w[sidekiq -t 30 -r ./test/fake_env.rb])
@ -141,14 +127,6 @@ class TestCLI < Minitest::Test
end
end
describe 'logfile' do
it 'accepts wiht -L' do
@cli.parse(%w[sidekiq -L /tmp/sidekiq.log -r ./test/fake_env.rb])
assert_equal '/tmp/sidekiq.log', Sidekiq.options[:logfile]
end
end
describe 'verbose' do
it 'accepts with -v' do
@cli.parse(%w[sidekiq -v -r ./test/fake_env.rb])
@ -157,14 +135,6 @@ class TestCLI < Minitest::Test
end
end
describe 'pidfile' do
it 'accepts with -P' do
@cli.parse(%w[sidekiq -P /tmp/sidekiq.pid -r ./test/fake_env.rb])
assert_equal '/tmp/sidekiq.pid', Sidekiq.options[:pidfile]
end
end
describe 'config file' do
it 'accepts with -C' do
@cli.parse(%w[sidekiq -C ./test/config.yml])
@ -174,8 +144,6 @@ class TestCLI < Minitest::Test
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
assert_nil Sidekiq.options[:environment]
assert_equal 50, Sidekiq.options[:concurrency]
assert_equal '/tmp/sidekiq-config-test.pid', Sidekiq.options[:pidfile]
assert_equal '/tmp/sidekiq.log', Sidekiq.options[:logfile]
assert_equal 2, Sidekiq.options[:queues].count { |q| q == 'very_often' }
assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' }
end
@ -188,8 +156,6 @@ class TestCLI < Minitest::Test
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
assert_nil Sidekiq.options[:environment]
assert_equal 50, Sidekiq.options[:concurrency]
assert_equal '/tmp/sidekiq-config-test.pid', Sidekiq.options[:pidfile]
assert_equal '/tmp/sidekiq.log', Sidekiq.options[:logfile]
assert_equal 2, Sidekiq.options[:queues].count { |q| q == 'very_often' }
assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' }
end
@ -202,8 +168,6 @@ class TestCLI < Minitest::Test
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
assert_equal 'staging', Sidekiq.options[:environment]
assert_equal 50, Sidekiq.options[:concurrency]
assert_equal '/tmp/sidekiq-config-test.pid', Sidekiq.options[:pidfile]
assert_equal '/tmp/sidekiq.log', Sidekiq.options[:logfile]
assert_equal 2, Sidekiq.options[:queues].count { |q| q == 'very_often' }
assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' }
end
@ -217,8 +181,6 @@ class TestCLI < Minitest::Test
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
assert_nil Sidekiq.options[:environment]
assert_equal 10, Sidekiq.options[:concurrency]
assert_nil Sidekiq.options[:pidfile]
assert_nil Sidekiq.options[:logfile]
assert_equal ['default'], Sidekiq.options[:queues]
end
end
@ -229,7 +191,6 @@ class TestCLI < Minitest::Test
-e snoop
-c 100
-r ./test/fake_env.rb
-P /tmp/sidekiq.pid
-q often,7
-q seldom,3])
@ -238,8 +199,6 @@ class TestCLI < Minitest::Test
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
assert_equal 'snoop', Sidekiq.options[:environment]
assert_equal 100, Sidekiq.options[:concurrency]
assert_equal '/tmp/sidekiq.pid', Sidekiq.options[:pidfile]
assert_equal '/tmp/sidekiq.log', Sidekiq.options[:logfile]
assert_equal 7, Sidekiq.options[:queues].count { |q| q == 'often' }
assert_equal 3, Sidekiq.options[:queues].count { |q| q == 'seldom' }
end
@ -277,17 +236,6 @@ class TestCLI < Minitest::Test
after do
Sidekiq.logger = @logger
end
describe 'pidfile' do
it 'writes process pid to file' do
Sidekiq.options[:pidfile] = '/tmp/sidekiq.pid'
@cli.stub(:launch, nil) do
@cli.run
end
assert_equal Process.pid, File.read('/tmp/sidekiq.pid').chop.to_i
end
end
describe 'require workers' do
describe 'when path is a rails directory' do
@ -365,7 +313,7 @@ class TestCLI < Minitest::Test
%w(TSTP USR1).each do |sig|
describe sig do
it 'quites with a corresponding event' do
it 'quiets with a corresponding event' do
quiet = false
Sidekiq.on(:quiet) do

View File

@ -235,7 +235,6 @@ class TestRetry < Minitest::Test
after do
Sidekiq.logger = @old_logger
Sidekiq.options.delete(:logfile)
File.unlink @tmp_log_path if File.exist?(@tmp_log_path)
end