Setup sidekiq with systemd and capistrano integration

Application

(1) Configure sidekiq.yml accordingly.

---
:concurrency: 5
staging:
  :concurrency: 10
production:
  :concurrency: 20
:queues:
  - critical
  - default
  - low

(2) Configure sidekiq.rb with Redis information.

Sidekiq.configure_server do |config|
  config.redis = { url: 'redis://0.0.0.0:6379/0', namespace: 'myapp' }
end

Sidekiq.configure_client do |config|
  config.redis = { url: 'redis://0.0.0.0:6379/0', namespace: 'myapp' }
end

(3) Add sidekiq tasks and event hooks in deploy.rb

namespace :sidekiq do
  task :quiet do
    on roles(:app) do
      puts capture("pgrep -f 'sidekiq' | xargs kill -TSTP")
    end
  end
  task :restart do
    on roles(:app) do
      execute :sudo, :systemctl, :restart, :sidekiq
    end
  end
end

after 'deploy:starting', 'sidekiq:quiet'
after 'deploy:reverted', 'sidekiq:restart'
after 'deploy:published', 'sidekiq:restart'

Ubuntu

(1) Create systemd script for Sidekiq

$sudo pico /lib/systemd/system/sidekiq.service
[Unit]
Description=sidekiq
After=syslog.target network.target

[Service]
Type=simple
WorkingDirectory=/path/to/your-app/current

ExecStart=/home/ubuntu/.rbenv/shims/bundle exec sidekiq -e production -C /path/to/your-app/current/config/sidekiq.yml -L /path/to/your-app/current/log/sidekiq.log
User=ubuntu
Group=ubuntu
UMask=0002

Environment=MALLOC_ARENA_MAX=2

# if we crash, restart
RestartSec=1
Restart=on-failure

# output goes to /var/log/syslog
StandardOutput=syslog
StandardError=syslog

# This will default to "bundler" if we don't specify it
SyslogIdentifier=sidekiq

[Install]
WantedBy=multi-user.target

Update the working directory, config file path, and log path accordingly.

(2) Deploy the app via Capistrano and validate Sidekiq is started.


AI Summary
Chrome On-device AI 2024-12-06 19:07:45

Share Article