【问题标题】:Running multiple schedules in laravel(octobercms)在 laravel(octobercms) 中运行多个计划
【发布时间】:2019-10-09 23:52:14
【问题描述】:

我有这些计划要执行

public function registerSchedule($schedule)
{
  $schedule->call('Corymillz\Adverts\Component\PaidBoost@onPaidBoostMsb')
           ->everyMinute();
  $schedule->call('Corymillz\Adverts\Component\PaidBoost@onPaidBoostMbb')
           ->cron('0 */12 * * *');
  $schedule->call('Corymillz\Adverts\Component\PaidBoost@onPaidBoostMeb')
           ->cron('0 */6 * * *');
  $schedule->call('Corymillz\Adverts\Component\PaidBoost@onPaidBoostBmsb')
           ->daily();
  $schedule->call('Corymillz\Adverts\Component\PaidBoost@onPaidBoostBmbb')
           ->cron('0 */12 * * *');
  $schedule->call('Corymillz\Adverts\Component\PaidBoost@onPaidBoostBmeb')
           ->cron('0 */6 * * *');
}

当我跑步时

php artisan schedule:run

似乎只执行了第一个计划,因为它显示了

运行预定命令:Corymillz\Adverts\Component\PaidBoost@onPaidBoostMsb

我在本地主机上,这是它在本地主机上运行的方式吗?还是我的计划任务组织错误?

【问题讨论】:

  • 您的 cron 表达式无效
  • @Saly3301 它看起来有效,如果它无效,我建议请指出问题所在,以便他至少知道自己做错了什么。

标签: php laravel octobercms


【解决方案1】:

您可以运行多个计划。它从不限于一个。

当您确实运行 php artisan schedule:run 时,不满足运行其他计划的条件。

cron 任务 php artisan schedule:run 应该按照 Laravel 的建议每分钟运行一次。

【讨论】:

    【解决方案2】:

    首先我们需要了解它是如何工作的。

    如果我们间隔 1 分钟让调度程序 cron 运行 [这里我们手动运行它,仍然会根据 laravel/[cron-expression] 设计使用 1 分钟]

    It checks interval base on minutes for => minute based crons
    It checks interval base on day's first hour's first minute for => day based crons
    and so on ...
    

    例如: 如果您的 cron 设置为每 2 分钟运行一次。 0 */2 * * *

    我们假设当前时间是:02:00PM

    0 */2 * * * 将仅在此时间间隔内运行

    02:00PM to 02:01PM
    02:02PM to 02:03PM
    02:04PM to 02:05PM
    02:06PM to 02:08PM
    and so on ...
    

    当前时间是:02:00PM -> 现在如果你运行php artisan schedule:run 您的 cron 将在当前时间在 02:00PM 到 02:01PM 之间运行

    下一个执行时间将在下午 02:02 到下午 02:03

    现在,如果你在下午 02:01:30 运行 php artisan schedule:run,什么都不会 发生在它不属于任何运行时间间隔

    现在你的调度任务


    1. everyMinute => every minute
    2. '0 */6 * * *' => every 6th minute
    3. '0 */12 * * *' => evry 12th minute
    4. daily => each day
    
    1. 每分钟

    无论你什么时候开火,它都会一直执行 php artisan schedule:run

    1. 0 */6 * * * 每 6 分钟一次

    它有 6 分钟的间隔,所以执行模式将是。 [假设它的当前时间是下午 2:00]

    -> it can execute between this time
    02:00PM to 02:01PM 
    02:06PM to 02:07PM 
    02:12PM to 02:13PM 
    02:18PM to 02:19PM 
    and so on ...
    

    所以,执行你的

    $schedule->call('Corymillz\Adverts\Component\PaidBoost@onPaidBoostMeb')
               ->cron('0 */6 * * *');
    

    时间应该是正确的。

    首先确保您正确设置了时区。表示如果您打印

    dd(\Carbon\Carbon::now());
    

    它应该在网页上显示/打印与您的计算机/机器时间完全相同的时间

    现在运行 php artisan schedule:run -> 按照我上面定义的特定时间间隔运行

    例如: 如果您的时钟时间是:下午 5:13

    那么你需要等待得到它6 minute * 3 => 18 所以下一个区间是5:18PM to 5:19PM

    5:18PM to 5:19PM之间运行php artisan schedule:run命令

    您可以看到您的 cron 确实在执行。对于其他 cron 时间,您可以根据上述逻辑计算自己的时间间隔,并在该间隔内运行调度程序以执行其他 cron。

    如有任何疑问,请发表评论。

    【讨论】:

      猜你喜欢
      • 2016-06-24
      • 2020-02-07
      • 1970-01-01
      • 2021-02-26
      • 2020-08-28
      • 2019-05-29
      • 1970-01-01
      • 2017-10-16
      • 2016-08-04
      相关资源
      最近更新 更多