Using Moment for delaying jobs in a Rails 3 app

by Andrew Gertig on February 5, 2011

I recently needed to schedule delayed tasks in a web app that utilizes the Twilio SMS API, this is a discussion of how I handled that situation. My plan was to host my app on Heroku as I normally would until I realized that using the delayed_job gem on Heroku was where you had to start paying for the service because this requires workers. I think this is completely okay of them to charge for this and I assume that I will become a paying customer of Heroku very shortly one way or another. However, for this project I was just testing out a feature that includes the scheduling of text messages and as such did not wish to have to pay just to try it.

This is where Moment came to the rescue. It is currently in Beta and is free while they figure out a good charge structure. Moment allows you to make a RESTful call to its API to schedule a call back to a URL on your site at a time that you determine. Moment is quite elegant in its relative simplicity.

One of the few things that I wished that Moment’s site had was a quick demo of how to make a call to their API using Ruby on Rails (which isn’t really that RoR self-centered as the Moment app itself is built with RoR). It is pretty much the same way you would access any other RESTful API but I feel as though it is always good to try to overcome the “curse of knowledge” and pretend you are a beginner.

So here is how I implemented the Moment API to schedule a delayed call to a custom action that then sent a text message to all the people in my People table via the Twilio API (the Twilio piece is left out for simplicity).

To interacte with a RESTful API the easiest way to do so in Ruby on Rails is with the HTTParty gem.

For the simplicity of this example I created a Custom controller with an index and mycustomaction methods. This way any time you refresh the page at http://localhost:3000/custom it will cause the Person.create_schedule method to be called.

My Person model looks like this:

Within my Scheduler model is where the POST to the Moment API occurs:

And that is how you use the Moment API from a Ruby on Rails app using the HTTParty gem. Here is what my routes.rb file looks like just for reference.

After using the Moment API I realized how easy it was to use and how many simple ways it could be leveraged and definitely plan to use it in production soon.

  • http://topsy.com/andrewgertig.com/2011/02/using-moment-for-delaying-jobs-in-a-rails-3-app/?utm_source=pingback&utm_campaign=L2 Tweets that mention Using Moment for delaying jobs in a Rails 3 app — Topsy.com

    [...] This post was mentioned on Twitter by Andrew Gertig, Andrew Gertig. Andrew Gertig said: Using Moment for delaying jobs in a Rails 3 app http://bit.ly/gSDbnQ [...]

  • http://beforeitwasround.com/ Steve Agalloco

    Interesting idea. This scares me a little bit because you are now tying your apps availability to the processing of delayed jobs. I also wonder what visibility Moment gives you as to your jobs, their status, and capabilities to reschedule on failure, etc. For simple non-critical tasks, this is probably a suitable replacement for a self-managed worker system but probably not something i’d feel comfortable with for everything.

Previous post:

Next post: