Better Programming

Advice for programmers.

Follow publication

Schedule Jobs in Ruby on Rails with Whenever gem

Get hourly reminders to drink water on slack.

Florence Njeri
Better Programming
Published in
4 min readMar 2, 2022
‘Image credit: Author’
Image credit: author

Have you been forgetting to take water? Let’s create a water reminder app that will notify you per hour using to take a glass of water using slack messages.

By the end of this tutorial, you’ll learn how to:

  1. Create incoming webhooks on Slack to send Slack messages
  2. Create a Ruby on Rails app and add the webhook URL to .env file
  3. Use HTTParty to invoke the API requests
  4. Use whenever to schedule cron jobs

Getting started

Start by downloading VSCode. If you already have VSCode, jump to the next section.

Post-Slack Messages Using Incoming Webhooks

Navigate to Incoming Webhooks and create an incoming webhook.

  1. Click Create Your Slack App
Create a Slack App

2. Create an app from scratch

Create an app from scratch

3. Edit an app name and select a workspace from the dropdown then click on create an app

Add app name, pick workspace and click create an app

4. Then follow steps 2 and 3 here to create an incoming webhook.

5. Copy the webhook URL you’ll get like shown in the screenshot below.

Copy the Webhook URL

You can test the webhook URL using Postman and confirm that you get a Slack notification

Create a Ruby on Rails app

Open a terminal instance, and create a new rails app without the view and model:

bundle gem slackbot

Add Webhook URL to Rails

We don’t want to expose our slack credentials to the public when we push the project to Github therefore we will use environment variables.

Step 1. Create a .env file on your root directory

Step 2. Add the webhook URL

WEBHOOK_URL = "your_webhook_url"

Step 3: Add dotenv gem to the Gemfile then run bundle install

gem 'dotenv-rails', groups: [:development, :test]
bundle install
Step 1 and Step 2 above

Schedule Cron jobs using Whenever

What is a cron job?

Cron jobs are used to schedule tasks that run repeatedly at a particular time every day or week e.g schedule reminders to drink water every hour or a fire your alarm every Wednesday at 4 AM

Step 1: Add whenever to the Gemfile

gem 'whenever', require: false

Step 2: Run

bundle install
Step 1 and Step 2 above

Step 3: Run wheneverize which will generate config/schedule.rb

bundle exec wheneverize .
Step 3

Step 4. Generate a rake file to define the tasks to be run by the cron job.

rails g task slack run_notifications

The command above will generate slack.rake under lib/tasks folder then add the code below to your rake file:

require 'httparty'require 'dotenv/tasks'namespace :slack dodesc "Send hourly remonders to drink water in Slack"task :run_notifications => :environment doHTTParty.post(ENV["WEBHOOK_URL"], :body => {:channel  => '#general',:username => 'Slack Bot',:text     => 'Time to take a glass of water."'}.to_json)end

To install HTTParty, add the gem below to your Gemfile and run bundle install

gem 'httparty'bundle install

Learn more about rake here. Learn more about HTTParty here

Step 5. Navigate to schedule.rb file. First, you’ll need to specify where cron jobs will log when the job completes

set :output, "log/cron.log"

Add the rake task:

every 1.hour dorake "slack:run_notifications"end

Step 6: Update the crontab file.

Navigate to your terminal and run the command below:

whenever — update-crontab — set environment=’development’

Step 7: Test your rake task by running the command below in your terminal

rake "slack:run_notifications"

You should get a slack notification

To learn more about whenever click here. The project on GitHub can be found here.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Florence Njeri
Florence Njeri

Written by Florence Njeri

Software Engineering || Career || Life

No responses yet

Write a response