Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sidekiq config per host #81

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ server 'example-small.com', roles: [:sidekiq_small]
server 'example-big.com', roles: [:sidekiq_big]
```

## Different sidekiq config per host

You can configure which config file you want to use on each host next way:

```ruby
set :sidekiq_role, [:sidekiq_small, :sidekiq_big]
set :sidekiq_small_config, 'config/sidekiq-small.yml'
set :sidekiq_big_config, 'config/sidekiq-big.yml'
server 'example-small.com', roles: [:sidekiq_small]
server 'example-big.com', roles: [:sidekiq_big]
```

## Customizing the monit sidekiq templates

If you need change some config in redactor, you can
Expand Down
64 changes: 33 additions & 31 deletions lib/capistrano/tasks/sidekiq.cap
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,31 @@ end

namespace :sidekiq do
def for_each_process(reverse = false, &block)
pids = processes_pids
pids.reverse! if reverse
pids.each_with_index do |pid_file, idx|
configs = processes_configs
configs.reverse! if reverse
configs.each_with_index do |config, idx|
within release_path do
yield(pid_file, idx)
yield(config, idx)
end
end
end

def processes_pids
pids = []
def processes_configs
configs = []
sidekiq_roles = Array(fetch(:sidekiq_role))
sidekiq_roles.each do |role|
next unless host.roles.include?(role)
config = fetch(:"#{ role }_config") || fetch(:sidekiq_config)
processes = fetch(:"#{ role }_processes") || fetch(:sidekiq_processes)
processes.times do |idx|
pids.push (idx.zero? && processes <= 1) ?
pid = (idx.zero? && processes <= 1) ?
fetch(:sidekiq_pid) :
fetch(:sidekiq_pid).gsub(/\.pid$/, "-#{idx}.pid")
configs.push pid_file: pid, config: config, idx: idx
end
end

pids
configs
end

def pid_process_exists?(pid_file)
Expand Down Expand Up @@ -87,21 +89,21 @@ namespace :sidekiq do
end
end

def start_sidekiq(pid_file, idx = 0)
def start_sidekiq(config = {})
args = []
args.push "--index #{idx}"
args.push "--pidfile #{pid_file}"
args.push "--index #{config[:idx]}"
args.push "--pidfile #{config[:pid_file]}"
args.push "--environment #{fetch(:sidekiq_env)}"
args.push "--logfile #{fetch(:sidekiq_log)}" if fetch(:sidekiq_log)
args.push "--require #{fetch(:sidekiq_require)}" if fetch(:sidekiq_require)
args.push "--tag #{fetch(:sidekiq_tag)}" if fetch(:sidekiq_tag)
Array(fetch(:sidekiq_queue)).each do |queue|
args.push "--queue #{queue}"
end
args.push "--config #{fetch(:sidekiq_config)}" if fetch(:sidekiq_config)
args.push "--config #{config[:config]}"
args.push "--concurrency #{fetch(:sidekiq_concurrency)}" if fetch(:sidekiq_concurrency)
if process_options = fetch(:sidekiq_options_per_process)
args.push process_options[idx]
args.push process_options[config[:idx]]
end
# use sidekiq_options for special options
args.push fetch(:sidekiq_options) if fetch(:sidekiq_options)
Expand Down Expand Up @@ -131,9 +133,9 @@ namespace :sidekiq do
task :quiet do
on roles fetch(:sidekiq_role) do
if test("[ -d #{release_path} ]") # fixes #11
for_each_process(true) do |pid_file, idx|
if pid_process_exists?(pid_file)
quiet_sidekiq(pid_file)
for_each_process(true) do |config|
if pid_process_exists?(config[:pid_file])
quiet_sidekiq(config[:pid_file])
end
end
end
Expand All @@ -144,9 +146,9 @@ namespace :sidekiq do
task :stop do
on roles fetch(:sidekiq_role) do
if test("[ -d #{release_path} ]")
for_each_process(true) do |pid_file, idx|
if pid_process_exists?(pid_file)
stop_sidekiq(pid_file)
for_each_process(true) do |config|
if pid_process_exists?(config[:pid_file])
stop_sidekiq(config[:pid_file])
end
end
end
Expand All @@ -156,8 +158,8 @@ namespace :sidekiq do
desc 'Start sidekiq'
task :start do
on roles fetch(:sidekiq_role) do
for_each_process do |pid_file, idx|
start_sidekiq(pid_file, idx) unless pid_process_exists?(pid_file)
for_each_process do |config|
start_sidekiq(config) unless pid_process_exists?(config[:pid_file])
end
end
end
Expand All @@ -171,21 +173,21 @@ namespace :sidekiq do
desc 'Rolling-restart sidekiq'
task :rolling_restart do
on roles fetch(:sidekiq_role) do
for_each_process(true) do |pid_file, idx|
if pid_process_exists?(pid_file)
stop_sidekiq(pid_file)
for_each_process(true) do |config|
if pid_process_exists?(config[:pid_file])
stop_sidekiq(config[:pid_file])
end
start_sidekiq(pid_file, idx)
start_sidekiq(config)
end
end
end

# Delete any pid file not in use
task :cleanup do
on roles fetch(:sidekiq_role) do
for_each_process do |pid_file, idx|
if pid_file_exists?(pid_file)
execute "rm #{pid_file}" unless pid_process_exists?(pid_file)
for_each_process do |config|
if pid_file_exists?(config[:pid_file])
execute "rm #{config[:pid_file]}" unless pid_process_exists?(config[:pid_file])
end
end
end
Expand All @@ -196,9 +198,9 @@ namespace :sidekiq do
task :respawn do
invoke 'sidekiq:cleanup'
on roles fetch(:sidekiq_role) do
for_each_process do |pid_file, idx|
unless pid_file_exists?(pid_file)
start_sidekiq(pid_file, idx)
for_each_process do |config|
unless pid_file_exists?(config[:pid_file])
start_sidekiq(config)
end
end
end
Expand Down