Scheduling a Ruby on Rails Job on Heroku Using Cron To Go

Heroku provides a robust platform for developers to deploy, manage, and scale applications. When working with Ruby on Rails projects, scheduling jobs can be an integral part of your application's architecture. Cron To Go, an Heroku add-on, makes this process seamless, allowing you to schedule tasks in a way similar to cron. In this blog post, we'll walk you through the process of scheduling a Ruby on Rails job on Heroku using Cron To Go.

Step 1: Clone the Demo Repository

First, you'll need a Ruby on Rails application to work with. We'll be using a minimalistic example that you can clone from our demo repository. Here's the link to the repository.

Simply clone the repository to your local machine to get started:

git clone https://github.com/crazyantlabs/crontogo-rails-example.git
cd crontogo-rails-example

Step 2: Install Heroku CLI and Cron To Go Plugin

Before proceeding, make sure that you have the Heroku CLI installed and configured. If you don't have it, follow the link provided here to get it set up.

Once you have the Heroku CLI installed, you will need to install the Cron To Go plugin. This is essential for scheduling the jobs:

heroku plugins:install cron-to-go

Step 3: Deploy the Rails Application to Heroku

Next, deploy your Ruby on Rails application to Heroku by creating an app and pushing the code to it:

heroku create
git push --set-upstream heroku main 

Your application should now be live on Heroku.

Step 4: Attach Cron To Go to Your Heroku App

With your application deployed, it's time to add Cron To Go to your Heroku app. This will allow you to schedule jobs as needed:

heroku addons:create crontogo:free-trial

Step 5: Schedule Jobs with Cron To Go

Your repository contains a manifest.yaml file that defines cron jobs. You can also create jobs using the CLI or the Cron To Go dashboard.

In the manifest file, you can see a job named 'Daily data update' that runs a rake task to update your database daily at midnight UTC.

version: 1
jobs:
  - name: 'Daily data update'
    command: 'rake db:update'
    schedule: '0 0 * * *'
    timezone: 'UTC'
    description: 'This job updates the database daily at midnight UTC.'

To schedule this job, use the Cron To Go CLI plugin:

heroku cron:jobs:import manifest.yaml

Your job is now scheduled! You can verify your scheduled jobs by running:

heroku cron:jobs

Optional: Manually Manage Jobs

Apart from using a YAML file, you can manually add or edit jobs through the Cron To Go dashboard. You can also monitor your jobs, view their run history, and troubleshoot any encountered issues. To open Cron To Go's dashboard from the CLI:

heroku addons:open crontogo

You can even run your job manually through the dashboard by clicking on the job menu button and selecting “Run job now.”

Conclusion

Scheduling Ruby on Rails jobs on Heroku using Cron To Go is a straightforward process that brings automation and efficiency to your applications. By following the steps outlined in this blog post, you can schedule recurring tasks that suit your needs.

If you found this guide helpful, feel free to share it with fellow developers and leave your thoughts in the comments section. For more tutorials and insights, consider subscribing to our channel or following our blog. Happy coding!

Run and Monitor Scheduled Tasks on your Favorite Apps
Cron To Go simplifies the monitoring, alerting, and management of your cron jobs' performance, uptime, and status - ensuring seamless operation.
Try Cron To Go for free!