Capistrano 的 Sidekiq 集成
Sidekiq integration for Capistrano - providing systemd service management and deployment coordination for Sidekiq 7+.
Upgrading from v2.x? See the Migration Guide for breaking changes and upgrade instructions.
For a complete working example of this gem in action, see the capistrano-example-app which demonstrates:
Add to your Gemfile:
gem 'capistrano-sidekiq', group: :developmentThen execute:
$ bundle installAdd to your Capfile:
# Capfile
require 'capistrano/sidekiq'
install_plugin Capistrano::Sidekiq # Default sidekiq tasks (REQUIRED!)
install_plugin Capistrano::Sidekiq::Systemd # Systemd integration (REQUIRED!)Important: Both require and install_plugin lines are necessary. The require loads the gem code, while install_plugin actually registers the Capistrano tasks. Without install_plugin, commands like cap sidekiq:start will not be available.
# config/deploy.rb
set :sidekiq_roles, :worker # Default role for Sidekiq processes
set :sidekiq_default_hooks, true # Enable default deployment hooks
set :sidekiq_env, fetch(:rack_env, fetch(:rails_env, fetch(:stage))) # Environment for Sidekiq processes
# Single config file
set :sidekiq_config_files, ['sidekiq.yml']
# Multiple config files for different Sidekiq processes
set :sidekiq_config_files, ['sidekiq.yml', 'sidekiq-high-priority.yml']This gem follows the Capistrano convention of sharing common settings across multiple service gems (e.g., capistrano-puma, capistrano-sidekiq). The following settings are shared:
:service_unit_user - Determines if services run as system or user services:systemctl_bin - Path to the systemctl binary:lingering_user - User for systemd lingering (for user services):service_unit_env_files - Shared environment files:service_unit_env_vars - Shared environment variablesEach gem can override these with prefixed versions (e.g., :sidekiq_systemctl_user).
…You can configure Sidekiq differently for specific servers, allowing you to run different Sidekiq processes with different configurations on different servers.
# config/deploy/production.rb
server 'worker1.example.com',
roles: [:worker],
sidekiq_config_files: ['sidekiq_high_priority.yml']
server 'worker2.example.com',
roles: [:worker],
sidekiq_config_files: ['sidekiq_low_priority.yml']# Different users and multiple processes per server
server 'worker1.example.com',
roles: [:worker],
sidekiq_config_files: ['sidekiq_critical.yml', 'sidekiq_default.yml'],
sidekiq_user: 'sidekiq_critical',
sidekiq_systemctl_user: :system # Run as system service on this server
server 'worker2.example.com',
roles: [:worker],
sidekiq_config_files: ['sidekiq_batch.yml'],
sidekiq_user: 'sidekiq_batch',
sidekiq_service_unit_env_vars: ['MALLOC_ARENA_MAX=4'] # Server-specific env varssidekiq_config_files_sidekiq_ for the default sidekiq.yml, or _sidekiq_. for additional configsconfig/sidekiq_high_priority.yml:
:concurrency: 10
:queues:
- [critical, 2]
- [high, 1]config/sidekiq_low_priority.yml:
:concurrency: 5
:queues:
- [low, 1]
- [default, 1]When using per-server configurations, Capistrano will:
cap production sidekiq:installcap production sidekiq:startYou can also target specific servers:
cap production sidekiq:restart --hosts=worker1.example.comThere are two ways to run multiple Sidekiq processes:
Create separate config files for different workloads:
# config/sidekiq_critical.yml
:concurrency: 5
:queues:
- [critical, 2]
# config/sidekiq_default.yml
:concurrency: 10
:queues:
- [default, 1]
- [low, 1]Configure in deploy.rb:
set :sidekiq_config_files, ['sidekiq_critical.yml', 'sidekiq_default.yml']This creates separate services:
myapp_sidekiq_production.sidekiq_criticalmyapp_sidekiq_production.sidekiq_defaultBenefits:
For simple scaling with identical processes:
set :sidekiq_command, 'sidekiqswarm'
set :sidekiq_service_unit_env_vars, ['SIDEKIQ_COUNT=10']This runs 10 identical processes with a single service.
Use the helper task to generate config files:
# Generate 5 config files
bundle exec cap production sidekiq:helpers:generate_configs[5]
# This creates:
# - config/sidekiq.yml (critical, high queues)
# - config/sidekiq_1.yml (default, medium queues)
# - config/sidekiq_2.yml (low, background queues)
# - config/sidekiq_3.yml (low, background queues)
# - config/sidekiq_4.yml (low, background queues)Then add to your deploy.rb:
set :sidekiq_config_files, ['sidekiq.yml', 'sidekiq_1.yml', 'sidekiq_2.yml', 'sidekiq_3.yml', 'sidekiq_4.yml']…For detailed information about systemd integration, see Systemd Integration Guide.
Sidekiq 7 introduced metrics with deployment tracking. Enable it to see deployment markers in your Sidekiq metrics graphs:
# config/deploy.rb
# Enable deployment marking
set :sidekiq_mark_deploy, true
# Optional: Custom deploy label (defaults to git commit description)
set :sidekiq_deploy_label, "v#{fetch(:current_revision)[0..6]} deployed to #{fetch(:stage)}"When enabled, the gem will:
This helps correlate performance changes with deployments.
This gem supports Sidekiq Enterprise's sidekiqswarm command for managing multiple processes with a single service.
# config/deploy.rb
# Use sidekiqswarm instead of sidekiq
set :sidekiq_command, 'sidekiqswarm'
# Configure swarm-specific environment variables
set :sidekiq_service_unit_env_vars, [
'SIDEKIQ_COUNT=10', # Number of processes to spawn
'SIDEKIQ_MAXMEM_MB=512', # Memory limit per process
'SIDEKIQ_PRELOAD_APP=1', # Preload Rails app for memory efficiency
'RAILS_ENV=production'
]
# Optional: Configure shutdown timeout for graceful stops
set :sidekiq_command_args, -> { "-e #{fetch(:sidekiq_env)} -t 30" }server 'worker1.example.com',
roles: [:worker],
sidekiq_service_unit_env_vars: ['SIDEKIQ_COUNT=20', 'SIDEKIQ_MAXMEM_MB=1024']
server 'worker2.example.com',
roles: [:worker],
sidekiq_service_unit_env_vars: ['SIDEKIQ_COUNT=5', 'SIDEKIQ_MAXMEM_MB=512']View Sidekiq service logs using journalctl:
# View last 100 lines of logs
journalctl -u sidekiq_myapp_production -n 100
# Follow logs in real-time
journalctl -u sidekiq_myapp_production -fLog files are configured automatically using the append: functionality in the systemd service file.
For systems with older Systemd versions where append: is not supported:
A complete example application demonstrating the usage of this gem is available at: https://github.com/seuros/capistrano-example-app
git checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)暂无开放 Issues,或尚未同步最近议题。