From 1d9800b0ce6e20138ff47824a7efd511a94cf615 Mon Sep 17 00:00:00 2001 From: Mike Perham Date: Tue, 17 Mar 2020 10:12:25 -0700 Subject: [PATCH] Fixes for systemd notify integration, #4488 --- examples/systemd/sidekiq.service | 29 +++++++++++++++++------------ lib/sidekiq.rb | 1 - lib/sidekiq/cli.rb | 2 ++ lib/sidekiq/systemd.rb | 4 +++- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/examples/systemd/sidekiq.service b/examples/systemd/sidekiq.service index ae62ee74..1c4fbb2c 100644 --- a/examples/systemd/sidekiq.service +++ b/examples/systemd/sidekiq.service @@ -1,15 +1,17 @@ # -# systemd unit file for CentOS 7+, Ubuntu 15.04+ +# This file tells systemd how to run Sidekiq as a 24/7 long-running daemon. # # Customize this file based on your bundler location, app directory, etc. -# Put this in /usr/lib/systemd/system (CentOS) or /lib/systemd/system (Ubuntu). -# Run: +# Customize and copy this into /usr/lib/systemd/system (CentOS) or /lib/systemd/system (Ubuntu). +# Then run: # - systemctl enable sidekiq # - systemctl {start,stop,restart,reload} sidekiq # # This file corresponds to a single Sidekiq process. Add multiple copies # to run multiple processes (sidekiq-1, sidekiq-2, etc). # +# Use `journalctl -u sidekiq -rn 100` to view the last 100 lines of log output. +# [Unit] Description=sidekiq # start us only once the network and logging subsystems are available, @@ -25,20 +27,23 @@ After=syslog.target network.target # times! systemd is a critical tool for all developers to know and understand. # [Service] -# You may want to use -# Type=notify -# to ensure service is not marked as started before it actually did. -# Include sd_notify gem to send a message on sidekiq startup like -# Sidekiq.configure_server do |config| -# config.on(:startup) { SdNotify.ready } -# end -# to let systemd know when the service is actually started. -Type=simple +# +# WARNING WARNING WARNING +# +# As of v6.0.6, Sidekiq automatically supports systemd's `notify` service +# monitoring. If you are using an earlier version, change this to `Type=simple`. +# +# WARNING WARNING WARNING +# +Type=notify +WatchdogSec=5 + WorkingDirectory=/opt/myapp/current # If you use rbenv: # ExecStart=/bin/bash -lc 'exec /home/deploy/.rbenv/shims/bundle exec sidekiq -e production' # If you use the system's ruby: ExecStart=/usr/local/bin/bundle exec sidekiq -e production +# If you use rvm in production, don't. # use `systemctl reload sidekiq` to send the quiet signal to Sidekiq # at the start of your deploy process. diff --git a/lib/sidekiq.rb b/lib/sidekiq.rb index 28e918f9..34d6abbb 100644 --- a/lib/sidekiq.rb +++ b/lib/sidekiq.rb @@ -258,4 +258,3 @@ module Sidekiq end require "sidekiq/rails" if defined?(::Rails::Engine) -require "sidekiq/systemd" diff --git a/lib/sidekiq/cli.rb b/lib/sidekiq/cli.rb index 865bc831..10bbe492 100644 --- a/lib/sidekiq/cli.rb +++ b/lib/sidekiq/cli.rb @@ -389,3 +389,5 @@ module Sidekiq end end end + +require "sidekiq/systemd" diff --git a/lib/sidekiq/systemd.rb b/lib/sidekiq/systemd.rb index 0dd18fb7..64ea99d7 100644 --- a/lib/sidekiq/systemd.rb +++ b/lib/sidekiq/systemd.rb @@ -13,6 +13,7 @@ module Sidekiq # "It is recommended that a daemon sends a keep-alive notification message # to the service manager every half of the time returned here." ping_f = sec_f / 2 + Sidekiq.logger.info "Pinging systemd watchdog every #{ping_f.round(1)} sec" Thread.new do loop do Sidekiq::SdNotify.watchdog @@ -24,11 +25,12 @@ end if ENV["NOTIFY_SOCKET"] Sidekiq.configure_server do |config| + Sidekiq.logger.info "Enabling systemd notification integration" require "sidekiq/sd_notify" config.on(:startup) do Sidekiq::SdNotify.ready end - config.on(:terminate) do + config.on(:shutdown) do Sidekiq::SdNotify.stopping end Sidekiq.start_watchdog if Sidekiq::SdNotify.watchdog?