Execute Rake task every fourth sunday using whenever gem

1k views Asked by At

I have the following task defined in my schedule.rb that should be run every 1st of the month a 2am.

every "0 2 1 * *" do
  rake ":task_1"
end

What I really want to do though is run that task every fourth sunday instead. All I found in the whenever gem-documentation was something like every :sunday, :at => '2am'. How would this look if I wanted to run the task every fourth sunday at 2am?

2

There are 2 answers

1
Simone Carletti On BEST ANSWER

It's not possible to achieve such result with the crontab syntax. There are some workarounds.

The most common is to configure it to run every day for the first 7 days of the month, and at the top of the script exist if it's not Sunday.

Quoting Run every 2nd and 4th Saturday of the month

The first Saturday of the month falls on one (and only one) of the dates from the first to the seventh inclusive. Likewise, the third Saturday falls on one date between the fifteenth and the twenty-first inclusive. So, simply set up your cron job to run on every one of those days and immediately exit if it's not Saturday.

You can easily accomplish it with whenever. Configure the cron as indicated, then in the rake task check and exit from the task if the day is not Sunday

Date.today.sunday?
1
Freddy Wetson On

I dont think you can represent that in crontab form. All wheneverize syntax does is change it to crontab syntax