1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/examples/systemd/sidekiq.service
H. Wade Minter 34f61e8bc6 Change systemd restart method to "always"
I'm running Sidekiq 6.4.1 on an Ubuntu 20.04 system with Ruby 3.1.1 installed via Snap for a Rails 7 application.

    Using this systemd template, I noticed something odd when deploying with Capistrano. When my cap script ran `sudo systemctl restart sidekiq`, the Sidekiq process would restart, briefly go active, then go inactive and stop running. I would need to SSH in and manually do a `systemctl restart sidekiq` in order to get the process back up and running.

    With some experimentation, I found that changing the `Restart` configuration from `on-failure` to `always` seems to do the trick. Now, when the Sidekiq process goes inactive, it properly restarts and stays restarted.

    I'm not 100% sure of the "why" behind what's going on, but this change seems to be effective and I have not noticed any side effects.

    Documentation of previous behavior

    Prior to deploy (process running)

    ```
    root@stage-worker01:~# systemctl status sidekiq
    ● sidekiq.service - sidekiq
         Loaded: loaded (/lib/systemd/system/sidekiq.service; enabled; vendor prese>
         Active: active (running) since Thu 2022-03-24 16:34:32 UTC; 51s ago
       Main PID: 118856 (bundle)
          Tasks: 30 (limit: 1120)
         Memory: 291.2M
         CGroup: /system.slice/sidekiq.service
                 └─118856 sidekiq 6.4.1 staging [0 of 20 busy]
    ```

    Immediately post deploy (process restarting)

    ```
    root@stage-worker01:~# systemctl status sidekiq
    ● sidekiq.service - sidekiq
         Loaded: loaded (/lib/systemd/system/sidekiq.service; enabled; vendor prese>
         Active: activating (start) since Thu 2022-03-24 16:38:35 UTC; 9s ago
       Main PID: 119572 (bundle)
          Tasks: 0 (limit: 1120)
         Memory: 43.4M
         CGroup: /system.slice/sidekiq.service
                 ‣ 119572 /var/www/sox/staging/shared/bundle/ruby/3.1.0/bin/sidekiq>
    ```

    After deploy (process started)

    ```
    Mar 24 16:38:35 stage-worker01.suggestionox.com systemd[1]: Starting sidekiq...
    root@stage-worker01:~# systemctl status sidekiq
    ● sidekiq.service - sidekiq
         Loaded: loaded (/lib/systemd/system/sidekiq.service; enabled; vendor prese>
         Active: active (running) since Thu 2022-03-24 16:38:45 UTC; 3s ago
       Main PID: 119572 (bundle)
          Tasks: 0 (limit: 1120)
         Memory: 43.4M
         CGroup: /system.slice/sidekiq.service
                 ‣ 119572 sidekiq 6.4.1 staging [0 of 20 busy]

    Mar 24 16:38:35 stage-worker01.suggestionox.com systemd[1]: Starting sidekiq...
    Mar 24 16:38:45 stage-worker01.suggestionox.com systemd[1]: Started sidekiq.
    ```

    Failure (process inactive)

    ```
    root@stage-worker01:~# systemctl status sidekiq
    ● sidekiq.service - sidekiq
         Loaded: loaded (/lib/systemd/system/sidekiq.service; enabled; vendor prese>
         Active: inactive (dead) since Thu 2022-03-24 16:38:56 UTC; 2s ago
        Process: 119572 ExecStart=/usr/bin/env bundle exec sidekiq -e staging (code>
       Main PID: 119572 (code=exited, status=0/SUCCESS)

    Mar 24 16:38:35 stage-worker01.suggestionox.com systemd[1]: Starting sidekiq...
    Mar 24 16:38:45 stage-worker01.suggestionox.com systemd[1]: Started sidekiq.
    Mar 24 16:38:56 stage-worker01.suggestionox.com systemd[1]: sidekiq.service: Su>
    ```
2022-03-24 12:53:37 -04:00

89 lines
3.4 KiB
Desktop File

#
# 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.
#
# If you are going to run this as a user service (or you are going to use capistrano-sidekiq)
# Customize and copy this to ~/.config/systemd/user
# Then run:
# - systemctl --user enable sidekiq
# - systemctl --user {start,stop,restart} sidekiq
#
# If you are going to run this as a system service
# Customize and copy this into /usr/lib/systemd/system (CentOS) or /lib/systemd/system (Ubuntu).
# Then run:
# - systemctl enable sidekiq
# - systemctl {start,stop,restart} 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,
# consider adding redis-server.service if Redis is local and systemd-managed.
After=syslog.target network.target
# See these pages for lots of options:
#
# https://www.freedesktop.org/software/systemd/man/systemd.service.html
# https://www.freedesktop.org/software/systemd/man/systemd.exec.html
#
# THOSE PAGES ARE CRITICAL FOR ANY LINUX DEVOPS WORK; read them multiple
# times! systemd is a critical tool for all developers to know and understand.
#
[Service]
#
# !!!! !!!! !!!!
#
# As of v6.0.6, Sidekiq automatically supports systemd's `Type=notify` and watchdog service
# monitoring. If you are using an earlier version of Sidekiq, change this to `Type=simple`
# and remove the `WatchdogSec` line.
#
# !!!! !!!! !!!!
#
Type=notify
# If your Sidekiq process locks up, systemd's watchdog will restart it within seconds.
WatchdogSec=10
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 without gemset and your ruby version is 2.6.5
# ExecStart=/home/deploy/.rvm/gems/ruby-2.6.5/wrappers/bundle exec sidekiq -e production
# If you use rvm in production with gemset and your ruby version is 2.6.5
# ExecStart=/home/deploy/.rvm/gems/ruby-2.6.5@gemset-name/wrappers/bundle exec sidekiq -e production
# If you use rvm in production with gemset and ruby version/gemset is specified in .ruby-version,
# .ruby-gemsetor or .rvmrc file in the working directory
ExecStart=/home/deploy/.rvm/bin/rvm in /opt/myapp/current do bundle exec sidekiq -e production
# Use `systemctl kill -s TSTP sidekiq` to quiet the Sidekiq process
# Uncomment this if you are going to use this as a system service
# if using as a user service then leave commented out, or you will get an error trying to start the service
# !!! Change this to your deploy user account if you are using this as a system service !!!
# User=deploy
# Group=deploy
# UMask=0002
# Greatly reduce Ruby memory fragmentation and heap usage
# https://www.mikeperham.com/2018/04/25/taming-rails-memory-bloat/
Environment=MALLOC_ARENA_MAX=2
# if we crash, restart
RestartSec=1
Restart=always
# output goes to /var/log/syslog (Ubuntu) or /var/log/messages (CentOS)
StandardOutput=syslog
StandardError=syslog
# This will default to "bundler" if we don't specify it
SyslogIdentifier=sidekiq
[Install]
WantedBy=multi-user.target