- Read Tutorial
- Watch Guide Video
Now that we have our tests in place, it's time to decide how we are going to send SMS messages. It's not possible for a person to be available round-the-clock to send SMS messages at a particular time, and this is why we have background tasks. In this video, we're going to create a rake task that'll handle sending out these messages.
To start off, run rake -T
on your console, and this will bring up all the rake tasks available for you.
It's a good idea to go through this list to get an idea of the options available for you.
In this program though, we are going to create a custom rake task that'll go along with all these.
To start, let's run a task generator command:
rails g task notification sms
For this generator, our namespace is notification
and our task is sms
.
This will create a file called notification.rake
in our lib/tasks
directory, and it'll look like this:
# lib/tasks/notification.rake namespace :notification do desc "Sends SMS notification to employees asking them to log if they had overtime or not" task sms: :environment do puts "I'm in a rake task" end end
In this code we've changed the desc
to say "Sends SMS notifications to employees asking them to log if they had overtime or not." We'll also added a simple puts
statement to verify that the task is working properly.
Now, if you go to console and run rake -T
, you can see notification:sms
in the list.
Next, let's run this task with the command:
rake notification:sms
And as you'll see, that's working perfectly!