lordsimal/cakephp-scheduler

CakePHP 调度插件

安装: 70

依赖: 0

建议者: 0

安全: 0

星标: 6

关注者: 1

分支: 2

开放问题: 1

类型:cakephp-plugin

1.1.0 2024-09-15 07:09 UTC

This package is auto-updated.

Last update: 2024-09-15 07:10:33 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require codecov

这个插件能做什么?

此工具允许您将所有cron作业从通过crontab配置的服务器迁移到您的CakePHP应用程序(和插件)中的应用控制。

要求

  • PHP 8.1+
  • CakePHP 5.0+

安装

composer require lordsimal/cakephp-scheduler

加载插件

在 Application.php 中

public function bootstrap()
{
    parent::bootstrap();

    $this->addPlugin(\CakeScheduler\CakeSchedulerPlugin::class);
}

或者使用 cake CLI。

bin/cake plugin load CakeScheduler

使用方法

定义一个计划

您的应用程序或插件需要实现 CakeSchedulerInterface,这将添加 schedule(Scheduler &$scheduler) 方法。

<?php
 
namespace App;

use App\Command\MyAppCommand;
use App\Command\OtherAppCommand;
use Cake\Http\BaseApplication;
use CakeScheduler\CakeSchedulerInterface;
use CakeScheduler\Scheduler\Scheduler;

class Application extends BaseApplication implements CakeSchedulerInterface
{
    public function schedule(Scheduler &$scheduler): void
    {
        $scheduler->execute(MyAppCommand::class)->daily();
        $scheduler->execute(OtherAppCommand::class, ['somearg', '--myoption=someoption'])->daily();
    }
}

使用 ->execute() 方法定义应该执行哪个命令。

每个 ->execute() 方法将返回一个 \CakeScheduler\Scheduler\Event 实例,用于告诉调度程序何时执行命令。

可用频率

有关所有可用选项,请参阅 EventTest

列出所有计划中的事件

bin/cake schedule:view

运行调度程序

您仍然需要在您的 crontab 中有如下条目

* * * * * cd /path-to-your-project && bin/cake schedule:run >> /dev/null 2>&1

致谢

此插件深受 Laravel 任务调度功能 启发。

许可证

该插件作为开源软件,根据 MIT 许可证 的条款提供。