Sidekiq process in production with Systemd and Monit
Recently, we have upgraded our Sidekiq version from 5.2 to 6.5. Before Sidekiq 6.0 we were managing the Sidekiq process directly using Monit. With the release of Sidekiq 6, the team has removed the daemonization, logfile, and pidfile command line arguments and sidekiqctl binary. Managing services manually is more error-prone, let our operating system do it for us. We have three options to go with systemd, upstart, and foreman. We decided to go ahead with the systemd.
Systemd
Systemd is a system and service manager for linux. Systemd tasks are organized as units. Most common units are services(.service), mount points(.mount), devices(.device), sockets(.socket), or timers(.timer)
Systemctl
The systemctl command is a utility which is responsible for examining and controlling the systemd system and service manager.
Sidekiq
Simple, efficient background processing for Ruby.
Sidekiq running as Systemd Service
- To manage Sidekiq we need to create a service file for Sidekiq which can be used to start, stop or restart the Sidekiq process.
- Content in the Sidekiq.service. Sidekiq has provided us with the template for the service file here Sidekiq.service. We modified it according to our use case
-
Our Modified Configurations:
- As we were using system ruby and using Sidekiq with some custom configurations. To start Sidekiq we used.
- To stop Sidekiq we need to send a TSTP signal to process all the busy jobs before terminating Sidekiq.
- For Managing with Monit we need the process id, After starting or stopping the service we were maintaining the process id file.
- As we want to use our app user to run this service.
- And we want to restart only when there is a failure.
- Reload the systemctl daemon for the new created service
- Now we can start the Sidekiq service:
Monitor Sidekiq process with Monit
Now we have systemd to start, stop and restart the Sidekiq process. Now we will look at how to monitor the Sidekiq process with the help of monit.
Monit
Monit is a utility for managing and monitoring processes, programs, files, directories and filesystems on a Unix system.
- Modified monitrc
- We can check if sidekiq is up and running:
Monit will check the Sidekiq process and it will automatically start in case of the unexpected kill of the Sidekiq process.
We have successfully completed the Sidekiq process monitoring with the help of Monit and Systemd.