crabstudio/scheduler

让 CakePHP 中的任务调度变得更加简单。

安装: 2,375

依赖: 0

建议者: 0

安全: 0

星星: 0

关注者: 3

分支: 17

类型:cakephp-plugin

3.0.6 2016-05-22 09:54 UTC

This package is auto-updated.

Last update: 2024-08-29 04:36:57 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

CakePHP 3: 调度插件

========================

让 CakePHP 中的任务调度变得更加简单。

作者

Trent Richardson [http://trentrichardson.com]

贡献者

Anh Tuan Nguyen [anhtuank7c@hotmail.com]

许可协议

版权所有 2015 Trent Richardson

您可以在 MIT 许可证下使用此项目。 http://trentrichardson.com/Impromptu/MIT-LICENSE.txt

工作原理

SchedulerShell 通过为您的项目调度一个 cron (SchedulerShell) 来工作。然后在 bootstrap.php 中,您可以为您所有的任务创建间隔。部署新的计划任务现在变得更加简单;一个 crontab 条目执行所有任务。

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 包的推荐方法是

composer require crabstudio/scheduler

或者将以下行添加到应用程序的 composer.json

"require": {
    "crabstudio/scheduler": "^1.0"
}

然后执行以下命令

composer update

加载插件

打开 终端/命令行,然后输入以下命令

bin/cake plugin load Scheduler

或者将此行添加到 Your_project\config\bootstrap.php

Plugin::load('Scheduler');

通过最短的间隔为 SchedulerShell.php 调度单个系统 cron。例如,如果您有 5 个任务,最频繁运行的是每 5 分钟一次,那么请至少每 5 分钟调度此 cron。有关更多信息,请参阅 Shells as Cron Jobs

示例 cron 任务

*/5 * * * * cd /path/to/app && bin/cake Scheduler.Scheduler

这将每 5 分钟运行一次 SchedulerShell。

现在一旦调度了这个 shell,我们就能将条目添加到 bootstrap.php 中。假设我们想要每天早上 5 点调度 CleanUp 任务,每 15 分钟调度一次 NewsletterTask。

Configure::write('SchedulerShell.jobs', array(
	'task_01' => array('interval' => 'next day 5:00', 'task' => 'EmailQueue'),// tomorrow at 5am
	'task_02' => array('interval' => 'PT15M', 'task' => 'Newsletter') //every 15 minutes
));

DateInterval

`PT1S` task will run each 1 Second
`PT1M` task will run each 1 Minute
`PT1H` task will run each 1 Hour
`P1D` task will run each 1 Day
`P1W` task will run each 1 Week
`P1M` task will run each 1 Month
`P1Y` task will run each 1 Year

datetime.formats.relative

`next day 05:00` task will on tomorrow at 05:00
`sunday 05:00` task will on sunday at 05:00
`weekday 05:00` task will on from Mon-Friday at 05:00
`saturday 05:00` task will on Saturday at 05:00
`sun 05:00` task will on Sun at 05:00
`sun 05:00 next month` task will on Sun at 05:00 in next month
`Monday next week 2020-01-01` task will on Monday in next week 2020-01-01

每个条目的键将用于存储上次运行。 这些必须是唯一的

interval 以两种方式之一设置。

  1. 对于一天中的设定时间,我们使用 PHP 的 相对时间格式:"下一天 5:00"。

  2. 为了使用间隔实现"每 15 分钟",我们使用 DateInterval 字符串格式:"PT15M"。

task 简单地是任务名称。

您可以传递一些可选参数:"action" 和 "pass"。

action 默认为 "execute",这是您指定任务中要调用的方法名称。

pass 默认为 array(),这是传递给您的 "action" 的参数数组。

Configure::write('SchedulerShell.jobs', array(
	'CleanUp' => array('interval' => 'next day 5:00', 'task' => 'CleanUp', 'action' => 'execute', 'pass' => array()),
	'Newsletters' => array('interval' => 'PT15M', 'task' => 'Newsletter', 'action' => 'execute', 'pass' => array())
));

结果存储

SchedulerShell 通过在 json 文件中跟踪每次运行。默认情况下,它存储在 TMP 中,命名为 "cron_scheduler.json"。

如果您需要更改其中任何一个,可以使用

// change the file name
Configure::write('SchedulerShell.storeFile', "scheduler_results.json");
// change the path (note the ending /)
Configure::write('SchedulerShell.storePath', "/path/to/save/");

防止同时运行相同的任务

默认情况下,如果 SchedulerShell 已经运行少于 10 分钟,则将退出。您可以调整此设置

// change the number of seconds to wait before running a parallel SchedulerShell; 0 = do not exit
Configure::write('SchedulerShell.processTimeout', 5*60);

其他注意事项/已知问题

  • 可选的 pass 参数尚未经过彻底测试
  • PHP版本5.3.6之前仅使用了相对日期时间对DateTime::modify()函数进行处理。这可能导致“下一天5:00”的时间间隔未能执行,如果之前的lastRun时间设置为05:02。因此,此插件只应在PHP >= 5.3.6版本的PHP上运行。