Rails whenever gem: each month on the 20th
Asked Answered
S

4

19

I searched all over the internet for this, and the documentation isn't really talking about monthly jobs in specific. So I was hoping that someone here could tell me how to do this.

I've installed the whenever gem and all I need to know is the right syntax for this:

every :month, :on => '20th', :at => '02:00' do
  runner "Mailer.send_friend_sheet"
end

Hope someone can point me in the right direction..

Thanks!

Synchronic answered 23/6, 2011 at 11:58 Comment(0)
J
11

Whenever doesn't support an :on option as far as I am aware, but you should be able to do

every '0 2 20 * *' do
  runner "Mailer.send_friend_sheet"
end

The '0 2 20 * *' is simply the relevant cron syntax - see http://www.manpagez.com/man/5/crontab/

Jonette answered 23/6, 2011 at 12:34 Comment(1)
Too bad there isn't a readable solution, but it'll do. Thanks!Synchronic
C
40

You can use raw cron syntax as well if you cant figure out how to use with ruby syntax.

What you want will look like:

every '0 2 20 * *' do
  command "echo 'you can use raw cron syntax too'"
end

Here is a quick cheatsheet for how to use cron syntax

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

Shamelessly stolen from: http://adminschoice.com/crontab-quick-reference

Canoewood answered 23/6, 2011 at 12:34 Comment(0)
K
21

If you want it more readable you can also just parse a date in text.

every 1.month, :at => 'January 20th 2:00am' do
  runner "Mailer.send_friend_sheet"
end

This will generate 0 2 20 * * as well.

Kairouan answered 20/5, 2013 at 10:14 Comment(0)
U
12

Even better:

every 1.month, at: 'start of the month at 2am' do
  runner "Mailer.send_friend_sheet"
end
Uzial answered 14/1, 2016 at 10:5 Comment(2)
Would be nice if I needed the first day of the month, I needed the 20th, nice syntax though.Synchronic
every 1.month, at: 'end of the month at 2am' do runner "Mailer.send_friend_sheet" end Is something like this valid?Tallyman
J
11

Whenever doesn't support an :on option as far as I am aware, but you should be able to do

every '0 2 20 * *' do
  runner "Mailer.send_friend_sheet"
end

The '0 2 20 * *' is simply the relevant cron syntax - see http://www.manpagez.com/man/5/crontab/

Jonette answered 23/6, 2011 at 12:34 Comment(1)
Too bad there isn't a readable solution, but it'll do. Thanks!Synchronic

© 2022 - 2024 — McMap. All rights reserved.