php - 将任务安排在每月的第一个工作日

标签 php laravel laravel-5.3

我目前有一项任务,从我的网站收集使用统计信息,并设置为自动通过电子邮件将它们发送给客户。

问题是这个月的 1 号可能是非工作日,我想这不是灾难,但看起来有点不专业。

这就是我目前的安排:

$schedule
    ->command("report", [ "--email" => "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="781d00191508141d381d00191508141d561b1715" rel="noreferrer noopener nofollow">[email protected]</a>" ]) //My command which accepts the email as a parameter
    ->monthly();

我正在考虑执行以下操作:

$schedule
    ->command("report", [ "--email" => "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="701508111d001c15301508111d001c155e131f1d" rel="noreferrer noopener nofollow">[email protected]</a>" ]) //My command which accepts the email as a parameter
    ->monthlyOn(1)
    ->when(function () {
         if (in_array(Carbon::now()->dayOfWeek,[Carbon::SATURDAY,Carbon::SUNDAY])) { 
            return false;  
        }
        return true;
    });

$schedule
    ->command("report", [ "--email" => "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="096c71686479656c496c71686479656c276a6664" rel="noreferrer noopener nofollow">[email protected]</a>" ]) //My command which accepts the email as a parameter
    ->monthlyOn(2)
    ->when(function () {
         if (Carbon::now()->dayOfWeek == Carbon::MONDAY) { 
            return true; //1st was in the weekend
        }
        return false;
    });    

$schedule
    ->command("report", [ "--email" => "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6b766f637e626b4e6b766f637e626b206d6163" rel="noreferrer noopener nofollow">[email protected]</a>" ]) //My command which accepts the email as a parameter
    ->monthlyOn(3)
    ->when(function () {
         if (Carbon::now()->dayOfWeek == Carbon::MONDAY) { 
            return true; //1st and 2nd was in the weekend
        }
        return false;
    });     

然而,对于这么简单的事情来说,这看起来是一件非常奇怪的事情。

所以我的问题:

  1. 如果 when 条件失败,是否会再次尝试该任务直至成功? (假设这是一个否定但不确定)
  2. 是否有更简单的方法在该月的第一个工作日运行任务?

最佳答案

我会将其作为社区 wiki 答案发布,供其他人在将来需要时使用:

You can put the condition into the actual crontab command:

[ "$(date '+%a')" = "Mon" ] && echo "It's Monday"

Now, if this condition is true on one of the first seven days in a month, you have its first Monday. Note that in the crontab, the percent-syntax needs to be escaped though:

0   12  1-7 *   *   [ "$(date '+\%a')" = "Mon" ] && echo "It's Monday"

以上引用自:https://superuser.com/questions/428807/run-a-cron-job-on-the-first-monday-of-every-month

这样它就被设置为一个 cronjob,每月仅在星期一运行一次。我相信这将是您实现目标的最有效方法。

关于php - 将任务安排在每月的第一个工作日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46560043/

相关文章:

带有 json 收集数据的 PHP 页面在 ubuntu 上不起作用,在 Windows 服务器上也能同样工作

php - 循环显示第一行为标题的MySQL PHP表

PHP获取用户ip,真的靠谱吗?

php - 在 MySQL 查询中使用 now() 函数

php - 测试 Laravel 服务提供者

php - Laravel 5.4 PHP 要求(5.6.30 与 5.6.40)

php - Laravel 5.8 中的 laravelcollective/html 包安装问题

laravel - 访问AppServiceProvider中的用户?

php - 基于 Eloquent 模型创建后代类

jquery - XHR 中的缓存不起作用