queencitycodefactory/cakephp-scheduler
Composer 安装命令:
composer require queencitycodefactory/cakephp-scheduler
包简介
Makes scheduling tasks in CakePHP much simpler.
关键字:
README 文档
README
Makes scheduling tasks in CakePHP much simpler.
Author
Trent Richardson [http://trentrichardson.com]
License
Copyright 2015 Trent Richardson
You may use this project under MIT license. http://trentrichardson.com/Impromptu/MIT-LICENSE.txt
How It Works
SchedulerShell works by scheduling one cron (SchedulerShell) for your project. Then in bootstrap.php you can create intervals for all your tasks. Deploying new scheduled tasks are now much easier; one crontab entry executes all your tasks.
Install
This Shell was developed for CakePHP 3.
Composer Installation (recommended)
In your project's composer.json file add this to your require:
"trentrichardson/cakephp-scheduler": "~3.0"
Manual Installation
Copy the Scheduler plugin into your App/Plugin folder and rename the folder to Scheduler.
Load the Plugin
In your bootstrap.php file add either:
Plugin::loadAll();
or
Plugin::load('Scheduler',['autoload'=>true]);
Schedule a single system cron by the shortest interval you need for SchedulerShell.php. For example, if you have 5 tasks and the most often run is every 5 minutes, then schedule this cron to run at least every 5 minutes. For more help see Shells as Cron Jobs.
Example cron job:
*/5 * * * * cd /path/to/app && bin/cake Scheduler.Scheduler
This would run the SchedulerShell every 5 minutes.
Now once this shell is scheduled we are able to add our entries to bootstrap.php. Lets say we want to schedule a CleanUp task daily at 5am and a NewsletterTask for every 15 minutes.
Configure::write('SchedulerShell.jobs', [ 'CleanUp' => ['interval' => 'next day 5:00', 'task' => 'CleanUp'],// tomorrow at 5am 'Newsletters' => ['interval' => 'PT15M', 'task' => 'Newsletter'] //every 15 minutes ]);
The key to each entry will be used to store the previous run. These must be unique!
interval is set one of two ways.
-
For set times of day we use PHP's relative time formats: "next day 5:00".
-
To use an interval to achieve "every 15 minutes" we use DateInterval string format: "PT15M".
task is simply the name of the Task.
There are a couple optional arguments you may pass: "action" and "pass".
action defaults to "execute", which is the method name to call in the task you specify.
pass defaults to [], which is the array of arguments to pass to your "action".
Configure::write('SchedulerShell.jobs', [ 'CleanUp' => ['interval' => 'next day 5:00', 'task' => 'CleanUp', 'action' => 'execute', 'pass' => [] ], 'Newsletters' => ['interval' => 'PT15M', 'task' => 'Newsletter', 'action' => 'execute', 'pass' => [] ] ));
Storage of Results
SchedulerShell keeps track of each run in a json file. By default this is stored in TMP and is named "cron_scheduler.json".
If you need to change either of these you may use:
// change the file name Configure::write('SchedulerShell.storeFile', "scheduler_results.json"); // change the path (note the ending /) Configure::write('SchedulerShell.storePath', "/path/to/save/");
Preventing Simultaneous SchedulerShells Running Same Tasks
By default, the SchedulerShell will exit if it is already running and has been for less than 10 minutes. You can adjust this by setting:
// change the number of seconds to wait before running a parallel SchedulerShell; 0 = do not exit Configure::write('SchedulerShell.processTimeout', 5*60);
This works by creating a flag file while the process is running, and deleting this file once it is complete. If the file exists, but is older than the timeout specified the scheduler will continue. Although you shouldn't need to, you can change the name of this file:
// change the name of the processing flag file Configure::write('SchedulerShell.processingFlagFile', '.cron_scheduler_processing_flag');
Other Notes/Known Issues
- The optional pass arguments have not been thoroughly tested
- PHP prior to version 5.3.6 only used relative datetime for the DateTime::modify() function. This could result in an interval of "next day 5:00" not running if the previous
lastRuntime was 05:02. Therefore this plugin should only be run on PHP >= 5.3.6.
Contributing
Contributions are much appreciated. Before you burn up too much time on a fix, check the posted issues, pull requests, and branches to see if someone has addressed the issue.
When making changes keep in mind the master branch is always the latest stable version. I do not apply changes directly to this branch, but rather to the cakephp-v* branches. When the latest cakephp version's branch is stable I tag it and merge it with master.
The quick overview:
- Fork/Checkout the cakephp-v* branch of the cake version you're applying changes for
- From a terminal
cdinto the directory and runcomposer install - Write any tests and changes you would like to make
- Run
vendor/bin/phpunitto verify all tests pass and your code works - Commit, Push, and send a pull request back to the cakephp-v* branch
queencitycodefactory/cakephp-scheduler 适用场景与选型建议
queencitycodefactory/cakephp-scheduler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 12 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cakephp」 「task」 「cron job」 「scheduler」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 queencitycodefactory/cakephp-scheduler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 queencitycodefactory/cakephp-scheduler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 queencitycodefactory/cakephp-scheduler 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Symfony bundle to monitor and execute commands
Expose the DDev executable commands to the Robo task runner.
Scheduling extension for Yii2 framework
Monitoring for scheduled jobs
Email Toolkit Plugin for CakePHP
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-12-05