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-setup and deploy-rails-app scripts.
  • 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.

  1. Open the server-setup script: In your CloudRay project, navigate to “Scripts” and edit the server-setup script.
  2. 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.

  1. Open the deploy-rails-app script: In your CloudRay project, navigate to “Scripts” and edit the deploy-rails-app script.
  2. Add the following line after the systemctl restart {{app_name}}-puma.service line:
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:

  1. Run the server-setup script: Create a new Runlog for the server-setup script, just like you did in the main Rails deployment guide. This will create the systemd service for Delayed Job.
  2. Run the deploy-rails-app script: Once the server setup is complete, create a new Runlog for the deploy-rails-app script. 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.