Skip to content

Instantly share code, notes, and snippets.

@Paprikas
Last active March 1, 2023 00:40
Show Gist options
  • Save Paprikas/11fc850f81b687d9cbb7a8efb5ead208 to your computer and use it in GitHub Desktop.
Save Paprikas/11fc850f81b687d9cbb7a8efb5ead208 to your computer and use it in GitHub Desktop.
capistrano-sidekiq systemd rbenv solution. Default configs did not work for me, also there is no proper documentation, Here is what I did:
Copy sidekiq.service.capistrano.erb template from "capistrano-sidekiq" gem to "app/config/deploy/templates"
Rbenv default bundler path differs from default '/usr/local/bin/bundler'.
So you have to set "bundler_path" option in "app/config/deploy.rb".
Here is mine:
set :bundler_path, "/usr/local/rbenv/bin/rbenv exec bundle"
And don't forget to add option to switch to systemd
set :init_system, :systemd
There is two ENV options that needs to be set in sidekiq.service.capistrano.erb
Environment=RBENV_VERSION=<%= fetch(:rbenv_ruby) %>
Environment=RBENV_ROOT=/usr/local/rbenv
You can set RBENV_ROOT with fetch. It's up to you.
Final version:
#############################sidekiq.service.capistrano.erb##############################
[Unit]
Description=sidekiq for <%= "#{fetch(:application)} (#{fetch(:stage)})" %>
After=syslog.target network.target
[Service]
Type=simple
Environment=RAILS_ENV=<%= fetch(:rails_env) %>
Environment=RBENV_VERSION=<%= fetch(:rbenv_ruby) %>
Environment=RBENV_ROOT=/usr/local/rbenv
WorkingDirectory=<%= fetch(:deploy_to) %>/current
ExecStart=<%= fetch(:bundler_path, '/usr/local/bin/bundler') %> exec sidekiq -e <%= fetch(:rails_env) %>
ExecReload=/bin/kill -TSTP $MAINPID
ExecStop=/bin/kill -TERM $MAINPID
RestartSec=1
Restart=on-failure
SyslogIdentifier=sidekiq
[Install]
WantedBy=default.target
#############################/sidekiq.service.capistrano.erb##############################
To make capistrano use my config file, I've created rake file in lib/capistrano/tasks/sidekiq.rake
with following content:
#############################sidekiq.rake##############################
namespace :sidekiq do
task :install do
on roles fetch(:sidekiq_roles) do |role|
switch_user(role) do
case fetch(:init_system)
when :systemd
create_systemd_template
execute :systemctl, "--user", "enable", fetch(:service_unit_name)
end
end
end
end
def fetch_systemd_unit_path
home_dir = capture :pwd
File.join(home_dir, ".config", "systemd", "user")
end
def create_systemd_template
search_paths = [
File.expand_path(
File.join(*%w[.. .. .. .. config deploy templates sidekiq.service.capistrano.erb]),
__FILE__
),
]
template_path = search_paths.detect {|path| File.file?(path)}
template = File.read(template_path)
systemd_path = fetch(:service_unit_path, fetch_systemd_unit_path)
execute :mkdir, "-p", systemd_path
upload!(
StringIO.new(ERB.new(template).result(binding)),
"#{systemd_path}/#{fetch :service_unit_name}"
)
execute :systemctl, "--user", "daemon-reload"
end
end
#############################/sidekiq.rake##############################
There is only one difference to original deploy config: Path to custom sidekiq.service.capistrano.erb file.
Now sidekiq working like a charm)
Don't know why there is no working solution for capistrano-sidekiq-systemd-rbenv out of the box.
Feel free to use or create pull request with config generator to original capistrano-sidekiq repo.
Good luck!
@duleorlovic
Copy link

Thanks for the tips, but I success to deploy buy simply setting bundler_path in config/deploy.rb (I do not need to override default template and to add env variables RBENV_VERSION and RBENV_ROOT)

set :bundler_path, '/home/orlovic/.rbenv/shims/bundle' 

@sagar-ranglani
Copy link

Thanks for the tips, but I success to deploy buy simply setting bundler_path in config/deploy.rb (I do not need to override default template and to add env variables RBENV_VERSION and RBENV_ROOT)

set :bundler_path, '/home/orlovic/.rbenv/shims/bundle' 

I'm sure if this works to not! Didn't work for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment