Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku scheduler weekly?

How can I get the heroku scheduler to run weekly?

Is this even possible, from what I can see I can only schedule hourly, every 10 mins, or daily tasks with no option for a weekly.

If not, what are other Heroku Add-ons that might allow me to run jobs (i.e. cron job) tasks on a weekly bassis in production.

Thanks!

Update 2:

  require 'date'
task :weeklydelete do
  if Date.today.wday.zero?
    runner "Event.clear_expired"
    runner "Activity.clear_expired"
  end
end

Update 2.5:

$heroku run bundle exec rake weeklydelete -a friendiosenew
Running `bundle exec rake weeklydelete` attached to terminal... up, run.6194
rake aborted!
undefined local variable or method `path' for main:Object
/app/lib/tasks/weeklydelete.rake:2:in `block in <top (required)>'
Tasks: TOP => weeklydelete
(See full trace by running task with --trace)
like image 322
user3399101 Avatar asked Mar 10 '14 23:03

user3399101


3 Answers

I found a great answer here. Just use a bash script in the Heroku Scheduler that checks the day of the week before running your command:

if [ "$(date +%u)" = 1 ]; then MY_COMMAND; fi   # only run on Mondays
like image 191
lubar Avatar answered Oct 26 '22 19:10

lubar


Set up a daily job, and in the job check if the day of week is Sunday (or whichever day). If it is that day, run the job. If it isn't that day, do nothing and exit.

edit: I was thinking more like

require 'date'
task :weeklydelete do
  if Date.today.wday.zero?
    runner "Event.clear_expired"
    runner "Activity.clear_expired"
  end
end

The logging to a file stuff wont work on heroku, and I'm not sure what you're using for the time stuff, but I fear if scheduler runs it not exactly at 3, that stuff might not work.

like image 38
Will Avatar answered Oct 26 '22 19:10

Will


the Date class has some really great helper methods. So, Date.today.wday.zero? could be shortened to Date.today.sunday?

like image 33
apowell Avatar answered Oct 26 '22 20:10

apowell