Technical · DevOps

Capistrano Deployment. Restart systemd and NGINX Services.

October 26, 2020

For the last few months, our team at 3Metas has been building a few SaaS solutions. Our engineering team delivers our APIs multiple times every day using Capistrano for our deployments, however, finding the right approach to restart the puma services registered with systemd (on ubuntu) and our web server (nginx) after each deployment took some time.

We ended doing this:

On our deploy.rb file we have:

after 'deploy:finishing', 'restart_apiservice'
after 'deploy:finishing', 'restart_nginx'

task :restart_apiservice do
on roles(:app), in: :sequence, wait: 5 do
execute 'sudo /bin/systemctl restart apiservice.service'
sleep(30)
end
end

task :restart_nginx do
on roles(:app), in: :sequence, wait: 5 do
execute 'sudo /usr/sbin/service nginx restart'
end
end

On each server where we are running our API, we need to set up the sudoer file to allow restart services commands without a password, if you are using GitLab, Jenkins, or any other CD/CI tool you will need that.

We created a sudoer file for our deploymentUser,

sudo visudo -f /etc/sudoers.d/deploymentUser

(Ubuntu users will need to create that on the /etc/sudoers.d directory. Only root accessible if you are using AWS, switch to root with sudo -i)

That file contains only the commands that we need to restart services after deployment and without a password, in our case, restart the puma services registered with systemd and our nginx web server. Our file is something similar to this.

deploymentUser ALL=NOPASSWD: /bin/systemctl restart apiservice.service, /usr/sbin/service nginx restart

with those pieces together, we can automate the deployment process of new API versions and restart our services in a cluster of servers running on AWS instances.

Integration with slack for deployment notifications and having a health check page for each service, plus external monitor tools is also a must to have for continuous deployment on fast pace environments that are moving even faster than agile.

Originally published on Medium in 3Metas.

Related reading

More in the Digest — a weekly roundup of AI and agents news — or start with AI Strategy.