Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

capistrano-sidekiq

> 后端框架
Open source

Sidekiq integration for Capistrano

562 stars0 likes3 views
WebsiteGitHub

About

Sidekiq integration for Capistrano

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.

Example Application

For a complete working example of this gem in action, see the capistrano-example-app which demonstrates:

  • Rails 8.0 deployment with Capistrano
  • Sidekiq 7.0 with Redis 7+
  • Systemd service management
  • rbenv integration
  • Complete deployment guide

Installation

Add to your Gemfile:

ruby
gem 'capistrano-sidekiq', group: :development

Then execute:

bash
$ bundle install

Setup

Add to your Capfile:

ruby
# 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.

Configuration Options

Basic Settings

ruby
# 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']

Shared Configuration with Other Gems

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 variables

Each gem can override these with prefixed versions (e.g., :sidekiq_systemctl_user).

Advanced Configuration

…

Per-Server Configuration

You can configure Sidekiq differently for specific servers, allowing you to run different Sidekiq processes with different configurations on different servers.

Basic Per-Server Setup

ruby
# 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']

Advanced Per-Server Configuration

ruby
# 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 vars

How It Works

  1. Configuration Files: Each server can have its own set of sidekiq_config_files
  2. Service Creation: A separate systemd service is created for each config file
  3. Service Naming: Services are named as _sidekiq_ for the default sidekiq.yml, or _sidekiq_. for additional configs
  4. Independent Control: Each service can be started, stopped, and restarted independently

Example Configurations

config/sidekiq_high_priority.yml:

yaml
:concurrency: 10
:queues:
  - [critical, 2]
  - [high, 1]

config/sidekiq_low_priority.yml:

yaml
:concurrency: 5
:queues:
  - [low, 1]
  - [default, 1]

Deployment Commands

When using per-server configurations, Capistrano will:

  • Install the appropriate services on each server during cap production sidekiq:install
  • Start only the configured services on each server during cap production sidekiq:start
  • Manage each server's services independently

You can also target specific servers:

bash
cap production sidekiq:restart --hosts=worker1.example.com

Multiple Processes

There are two ways to run multiple Sidekiq processes:

Option 1: Multiple Config Files (Recommended)

Create separate config files for different workloads:

yaml
# config/sidekiq_critical.yml
:concurrency: 5
:queues:
  - [critical, 2]

# config/sidekiq_default.yml  
:concurrency: 10
:queues:
  - [default, 1]
  - [low, 1]

Configure in deploy.rb:

ruby
set :sidekiq_config_files, ['sidekiq_critical.yml', 'sidekiq_default.yml']

This creates separate services:

  • myapp_sidekiq_production.sidekiq_critical
  • myapp_sidekiq_production.sidekiq_default

Benefits:

  • Clear purpose for each process
  • Independent scaling and management
  • Different configurations per process
  • Meaningful service names

Option 2: Sidekiq Enterprise (Sidekiqswarm)

For simple scaling with identical processes:

ruby
set :sidekiq_command, 'sidekiqswarm'
set :sidekiq_service_unit_env_vars, ['SIDEKIQ_COUNT=10']

This runs 10 identical processes with a single service.

Quick Setup for Multiple Processes

Use the helper task to generate config files:

bash
# 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:

ruby
set :sidekiq_config_files, ['sidekiq.yml', 'sidekiq_1.yml', 'sidekiq_2.yml', 'sidekiq_3.yml', 'sidekiq_4.yml']

Available Tasks

…

Systemd Integration

For detailed information about systemd integration, see Systemd Integration Guide.

Deployment Tracking (Sidekiq 7+)

Sidekiq 7 introduced metrics with deployment tracking. Enable it to see deployment markers in your Sidekiq metrics graphs:

Configuration

ruby
# 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)}"

How It Works

When enabled, the gem will:

  1. Automatically mark deployments after successful deploys
  2. Show vertical red lines in Sidekiq's metrics graphs
  3. Display the deploy label when hovering over the marker

This helps correlate performance changes with deployments.

Sidekiq Enterprise (Swarm) Support

This gem supports Sidekiq Enterprise's sidekiqswarm command for managing multiple processes with a single service.

Configuration

ruby
# 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" }

Benefits of Sidekiqswarm

  • Single Service: Manage N processes with one systemd service
  • Memory Efficiency: Preload app once, fork processes
  • Auto-restart: Processes that exceed memory limits are automatically restarted
  • Simplified Management: No need for multiple service files

Per-Server Swarm Configuration

ruby
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']

Working with Systemd Logs

View Sidekiq service logs using journalctl:

bash
# View last 100 lines of logs
journalctl -u sidekiq_myapp_production -n 100

# Follow logs in real-time
journalctl -u sidekiq_myapp_production -f

Log File Configuration

Modern Systemd (v240+, e.g., Ubuntu 20.04+)

Log files are configured automatically using the append: functionality in the systemd service file.

Legacy Systemd Systems

For systems with older Systemd versions where append: is not supported:

  1. Sidekiq messages are sent to syslog by default
  2. Configure system logger to filter Sidekiq messages

Example Application

A complete example application demonstrating the usage of this gem is available at: https://github.com/seuros/capistrano-example-app

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyautomationbackground-jobscapistranocapistrano-plugin

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架