jameybash

a personal blog for Jamey Alea

Gem of the Week: Whenever

July 25, 2016 Jamey Alea 0 Comments

One of the major benefits of Ruby development is the use of gems – powerful self-contained libraries of code that can easily add new functionality to your Ruby or Rails app. Gems are great tools for so many reasons (and not just because they make your roommate think that your programming job includes dragon-slaying, because that might be a benefit for only me). Gems are easy to install, prevent code duplication and, because of Ruby’s open-source foundation, not hard to make – which means that there are lots of community-developed gems designed for specific issues or functionality requirements, so that you don’t have to reinvent the wheel to get rolling. Whatever the problem you’re trying to solve, chances are pretty good that there’s already a gem that deals with your issue. All you’ve gotta do is slap in your gemfile and… voila! Now you’re thinking with portals! Now you’re working with gems!

That said, not all gems are created equal. With so much content out there, how do you know which gems are right for you and your project? Well, by talking to other developers and seeing which gems they’ve had success with. And that’s where I come in, presenting to you my Gem of the Week, which highlights a gem I’ve used that has made my life easier.

This week’s gem is… Whenever!

Now there are gems that make your life moderately more convenient and there are gems that you can’t live without. Whenever is a gem that I cannot live without.

Have you ever written a function that needs to run automatically at a certain interval, like every hour or every day? That’s the purpose of Cron, a very handy time-based job scheduler. But let’s talk for a minute about straight Cron syntax. Because when I first started working with Cron as a junior developer, I saw */15 */6 1,15,31 * 1-5 * and was about ready to find a new career to pursue.

15 minutes after installing Whenever, Cron jobs were scheduling and running my tasks – and I was getting on with my day and not worrying about it. It really makes it incredibly easy, even for beginners, because Whenever literally turns this…

0,45 0-23/3 * * *
30 1-23/3 * * *
15 2-23/3 * * *

…into this:

every 45.minutes do

Whenever also has pretty great documentation about how to integrate it into your existing app and exactly what your schedule.rb file should look like on their Github page so you’re ready to get scheduling!

Update: A couple readers have asked me for examples of when you might want to use Whenever in your applications. Whenever can be used for anything that you would normally run a Cron job for and the uses for Cron are practically unlimited. Think about anything that you’d like to run automatically in your app. You can write a method for Whenever to run that does just about anything you’d normally do in code.

Want to run a backup of your website every night and store it in dropbox? Set a daily job for 3am. Want to trigger a reminder for users who have registered for your site but not completed the validation process? Write a method that looks just for unverified users and send them an email. Want your app to automatically post tweets or other social media over the course of the day? Write a method that selects the most popular post on your site that’s been created in the last two hours and send it to Twitter. You can even do complex scheduling like, “every two hours, but only on weekdays and between the hours of 8am and 6pm.”

Setting up the method to run is easy too. Just make sure you have whatever functionality you want to run inside a class method for one of your models. The syntax for Whenever looks like this:

every 1.day, :at => ‘3:30 am’ do
    runner ‘MyModel.my_class_method’
end

Check out the Whenever documentation for all the types of scheduling that’s supported and get your tasks running exactly when you want them to!

(This article was originally posted on Uptime.)

#gems#programming#ruby on rails#scheduling#uptime

Next Post

Leave a Reply

Your email address will not be published / Required fields are marked *