"Whenever" gem running cron jobs on Heroku
Asked Answered
L

5

51

I created an app that uses the whenever gem. The gem creates cron jobs. I got it working locally but can't seem to get it working on heroku cedar. What's the command to do this?

running:

heroku run whenever --update-crontab job1

doesn't work

Leesa answered 23/12, 2011 at 19:6 Comment(0)
S
79

Short answer: use the scheduler add-on: http://addons.heroku.com/scheduler

Long answer: When you do heroku run, we

  1. spin up a dyno
  2. put your code on it
  3. execute your command, wait for it to finish
  4. throw the dyno away

Any changes you made to crontab would be immediately thrown away. Everything is ephemeral, you cannot edit files on heroku, just push new code.

Syncope answered 23/12, 2011 at 19:16 Comment(8)
what should you do for long running tasks? (takes hours to execute)Frankfurter
this is fine, you'll get 24 hours to complete itSyncope
@Will, but the app needs to generate some output at least once an hour, right? ("Connections to one-off dynos will be closed after one hour of inactivity (in both input and output).)Yordan
That is for web dynos. A web dyno ideling out has no barring on the scheduler add-on firing off a one-off run on your app. It is no different than doing heroku run <your job>. It uses the same api.Syncope
Do you fully use scheduler (meaning different code for local than production) or do you just use whenever and have the scheduler add-on installed?Leech
Scheduler docs state its 'best effort service' meaning there's no guarantee your task will run. If you need something more reliable - like I do for my payment processing - try clock processes.Apology
So does the Heroku scheduler essentially makes Whenever Gem unnecessary when deploying a rails app on Heroku? Especially when you have simple cron jobs? I would think that using the scheduler effectively renders the Whenever gem useless or am I missing something?Alexandriaalexandrian
@Alexandriaalexandrian have you confirmed this? In my experience, it is an accurate statement that the Whenever gem is useless in production if using Heroku Scheduler. However, it still may be useful for managing in lower, local environments (dev).Flocculent
K
14

You need to add Heroku Scheduler addon.

You can add it directly from your dashboard or using following commands:

  1. install the add-on:

    heroku addons:create scheduler:standard
    
  2. Create a rake task in lib/tasks

    # lib/tasks/scheduler.rake
    task :send_reminders => :environment do
      User.send_reminders
    end
    
  3. Schedule job

    • Visit Heroku Dashboard
    • Open your app
    • Select Scheduler from add-ons list
    • Click Add Job, enter a task and select frequency.

      e.g. Add rake send_reminders, select "Daily" and "00:00" to send reminders every day at midnight.

Heroku add cron task

Kila answered 28/9, 2016 at 9:44 Comment(0)
C
4

The other answers specify you should use the Heroku Scheduler add-on, and it is able to run a background tasks indeed, but it doesn't support the flexibility of cron.

There's another add-on, called Cron To Go, that is able to run your jobs on one-off dynos with cron's flexibility. You can also specify a timezone for your job and get notifications (email or webhook) when job fail, succeed or start.

(Full disclosure - I work for the company that created and operates Cron To Go)

Causeway answered 10/5, 2021 at 6:34 Comment(1)
Agree because the Heroku scheduler is not fit for any serious purpose.Sleeve
A
2

If you want to:

  • Use Heroku Scheduler
  • Run tasks every minute (not 10 min)
  • Don't care about dyno hours

This was my solution hack to run jobs every minute - assuming the task completes in under 60 seconds.

task start_my_service: :environment do
  1.upto(9) do |iteration|
    start_time = DateTime.now
    Services::MyService.call
    end_time = DateTime.now
    wait_time = 60 - ((end_time - start_time) * 24 * 60 * 60).to_i
    sleep wait_time if wait_time > 0
  end
end
Acrylic answered 7/10, 2020 at 18:10 Comment(0)
C
2

Heroku doesn't support cron jobs. And there are two drawbacks to the Heroku Scheduler :

  • you cannot choose an arbitrary interval or time at which to run jobs (it's either every 10 mins, 1 hour or daily).
  • your jobs are not defined in code, hence not in your versioning system and not easy to keep track of or modify.

Heroku does provide an alternative : custom clock processes. But the clock process requires its own dyno, and "Since dynos are restarted at least once a day some logic will need to exist on startup of the clock process to ensure that a job interval wasn’t skipped during the dyno restart".

Simple scheduler is a gem made specifically made for scheduling on Heroku, but seems a bit hackish.

I ended up using sidekiq-cron. Only drawback : if sidekiq is down right when a job is scheduled to run, the job won't run.

Conformance answered 24/8, 2022 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.