Search code examples
ruby-on-railsemailherokurufus-scheduler

Make rufus-scheduler work on Heroku


I would like to use Rufus-Scheduler in order to send a mail daily. I strictly followed the instructions given on GitHub here (the snippet, using Thin server, etc.); but nothnig happened (no mail sent) and I couldn't figured out the reason based on Heroku logs

my code

# config/initializers/scheduler.rb

require 'rufus-scheduler'


s = Rufus::Scheduler.singleton


# Here goes my mailing code (already tested and works well)

s.every '1d' do

Rails.logger.info "hello, it's #{Time.now}"
Rails.logger.flush
end

Is there some other points not mentionned on GitHub in order to make rufus-schedul work ? Many thanks


Solution

  • You can do this, but you have to understand how hobby dynos work. each free account is allocated hours like this.

    Accounts are given a base of 550 hours each month in which your Free dynos can run. In addition to these base hours, accounts which verify with a credit card will receive an additional 450 hours of Free dyno quota.

    Because rufus-scheduler runs as a thread in your ruby app process (this is why it starts running when you run rails console), as long as you have your server (I use puma) running, rufus scheduler will run just fine.

    The downside is that if you run two processes in your server, say you run puma with 3-4 workers, you're going to have 3-4 of your schedulers running at the same time making it execute your scheduled events in triplicate/quadruplicate, so keep that in mind as well.

    So the steps are simple - make sure you have enough hours to run your dyno continuously all month - use a service like pingdom, to ping your app every couple of minutes to keep the dyno active so that Heroku doesn't spin it down after 30 minutes of inactivity (it does that to free dynos) - that should be all you need to do

    Just remember that to run a dyno for a month you're going to need about 745 hours which your primary allocation covers (when you add a credit card). If for some reason you run out of hours (say you run two different apps on the account and use the method I describe below) then this could happen to you

    A second notification will be sent when you reach 100% of your account quota, at which point your application’s dynos will be put to sleep for the remainder of that month. As a result, any apps using free dynos will not be accessible for the remainder of the month.

    Seems like a lot of trouble to go to when you can just use the heroku scheduler to schedule rake tasks like everyone else does.