rumd3x/php-scheduler

PHP中调度任务的简单解决方案。

1.1.2 2018-12-15 20:46 UTC

This package is auto-updated.

Last update: 2024-09-16 10:10:21 UTC


README

PHP中调度任务的简单解决方案。

安装

通过composer安装,只需运行

  composer require rumd3x/php-scheduler

使用方法

此实用程序在采取额外措施防止任务重复运行的情况下,每分钟只能调用一次。所有其他锁和多个PHP实例处理已内置。

任务运行时,会打印

[d/m/Y H:i:s]: Job Default Started
[d/m/Y H:i:s]: Job Default Finished

基本用法

use Rumd3x\Scheduler\Schedule;

Schedule::action(function() {
    // Your code here
})->cron('* * * * *')->run();

您可以向闭包添加一个参数以使用当前的Cron表达式对象。

use Cron\CronExpression;
use Rumd3x\Scheduler\Schedule;

Schedule::action(function(CronExpression $cron) {
    // Your code here
})->everyMinute()->run();

您还可以为任务提供一个名称,这样它会打印任务名称而不是“默认”。

use Cron\CronExpression;
use Rumd3x\Scheduler\Schedule;

Schedule::action(function(CronExpression $cron) {
    // Your code here
})->setName('Test')->daily()->at('11:00')->run();

/* Prints:
    [d/m/Y H:i:s]: Job Test Started
    [d/m/Y H:i:s]: Job Test Finished
*/

还有更美观的内置方法,可以帮助调度任务而无需制作cron表达式。

->cron('* * * * *');	      // Run the task on a custom Cron schedule
->monthly();	              // Run the task every month
->monthly(13);	            // Run the task every 13th day of the month
->weekly()                  // Run the task every week
->weekly(1);	              // Run the task every Monday
->daily();                  // Run the task every Day
->hourly();                 // Run the task every Hour
->hourly(15);               // Run the task every Hour at minute 15
->everyThirtyMinutes();     // Run the task every Thirty Minutes
->everyFifteenMinutes();    // Run the task every Fifteen Minutes
->everyTenMinutes();        // Run the task every Ten Minutes
->everyFiveMinutes();       // Run the task every Five Minutes
->everyMinute();            // Run the task every Minute

还有“at”方法,允许您指定任务运行的特定时间,对于大于或等于一天的调度间隔。

->monthly()->at('12:00');	      // Run the task every month at 12:00
->weekly()->at('8:00')          // Run the task every week at 8:00
->daily()->at('9:00');          // Run the task every Day at 9:00