Deploy Delayed Jobs with Bash scripts
This guide provides a step-by-step approach to deploying Delayed Jobs for your Ruby on Rails application using Bash scripts and CloudRay. It is intended as a supplement to the main Deploy Ruby on Rails app with Bash scripts guide.
Prerequisites
This guide assumes you have followed the main Rails deployment guide and have already:
- Set up a CloudRay account.
- Added your server to CloudRay.
- Created the
server-setupanddeploy-rails-appscripts. - Created a variable group with the necessary variables.
Update the setup script
You’ll need to update the server-setup script to include a systemd service for Delayed Jobs. This service will ensure that Delayed Jobs runs in the background and restarts automatically if it fails.
- Open the
server-setupscript: In your CloudRay project, navigate to “Scripts” and edit theserver-setupscript. - Add the following code at the end of the script:
# Install a systemd service to run Delayed Job
cat <<'EOT' > /etc/systemd/system/{{app_name}}-delayed-job.service
[Unit]
Description={{app_name}}-delayed-job
After=network.target
[Service]
Type=simple
User=deploy
EnvironmentFile=/etc/environment
WorkingDirectory=/srv/{{app_name}}
ExecStart=/bin/bash -c "PATH=/home/deploy/.asdf/bin:/home/deploy/.asdf/shims:$PATH asdf exec bundle exec bin/delayed_job start -p {{app_name}} -i 0 --queues=mailers,default --pool=5"
Restart=always
StandardOutput=file:/srv/{{app_name}}/log/delayed_job-stdout.log
StandardError=file:/srv/{{app_name}}/log/delayed_job-stderr.log
[Install]
WantedBy=multi-user.target
EOT
Modify the deployment script
You’ll also need to modify the deploy-rails-app script to restart the Delayed Job service after each deployment. This ensures that Delayed Job picks up the latest code changes.
- Open the
deploy-rails-appscript: In your CloudRay project, navigate to “Scripts” and edit thedeploy-rails-appscript. - Add the following line after the
systemctl restart {{app_name}}-puma.serviceline:
systemctl restart {{app_name}}-delayed-job.service
Run the updated scripts
Now that you’ve updated the server-setup script, you need to run it again to apply the changes. After that, you can run the deploy-rails-app script to deploy your Rails application and start Delayed Job.
Follow these steps:
- Run the
server-setupscript: Create a new Runlog for theserver-setupscript, just like you did in the main Rails deployment guide. This will create the systemd service for Delayed Job. - Run the
deploy-rails-appscript: Once the server setup is complete, create a new Runlog for thedeploy-rails-appscript. This will deploy your Rails application and start the Delayed Job worker.
That’s it! Your Delayed Jobs worker is now set up and will be automatically deployed with your Rails application.
Written by Olusegun Durojaye
CloudRay Team