每当宝石使用时,每隔四个星期天执行一次Rake任务

时间:2022-01-25 21:10:44

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

我在schedule.rb中定义了以下任务,该任务应该在每个月的凌晨2点运行。

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?

我真正想要做的是每隔四个星期天运行该任务。我在每当gem文档中找到的就像每一个:sunday,:at =>'2am'。如果我想在凌晨2点每隔四个星期天运行一次任务,那怎么看?

2 个解决方案

#1


0  

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

使用crontab语法无法实现此类结果。有一些解决方法。

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.

最常见的是将其配置为每月的前7天每天运行,如果不是星期日,则在脚本的顶部存在。

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

引用每个月的第2个和第4个星期六运行

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.

本月的第一个星期六是从第一个到第七个日期的一个(也是唯一一个)日期。同样,第三个星期六是在十五和二十一之间的一个日期。因此,只需将您的cron作业设置为每天运行一次,如果不是星期六则立即退出。

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

您可以随时轻松完成它。按照指示配置cron,然后在rake任务检查中配置,如果日期不是星期日,则退出任务

Date.today.sunday?

#2


0  

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

我不认为你可以用crontab形式表示。所有whenize语法都将其更改为crontab语法

#1


0  

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

使用crontab语法无法实现此类结果。有一些解决方法。

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.

最常见的是将其配置为每月的前7天每天运行,如果不是星期日,则在脚本的顶部存在。

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

引用每个月的第2个和第4个星期六运行

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.

本月的第一个星期六是从第一个到第七个日期的一个(也是唯一一个)日期。同样,第三个星期六是在十五和二十一之间的一个日期。因此,只需将您的cron作业设置为每天运行一次,如果不是星期六则立即退出。

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

您可以随时轻松完成它。按照指示配置cron,然后在rake任务检查中配置,如果日期不是星期日,则退出任务

Date.today.sunday?

#2


0  

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

我不认为你可以用crontab形式表示。所有whenize语法都将其更改为crontab语法