webtoolsnz/yii2-scheduler
Composer 安装命令:
composer require webtoolsnz/yii2-scheduler
包简介
A scheduled task runner for Yii2 applications
关键字:
README 文档
README
A scheduled task manager for yii2
Installation
The preferred way to install this extension is through composer.
Install using the following command.
$ composer require webtoolsnz/yii2-scheduler
Now that the package has been installed you need to configure the module in your application
The config/console.php file should be updated to reflect the changes below
'bootstrap' => ['log', 'scheduler'], 'modules' => [ 'scheduler' => ['class' => 'webtoolsnz\scheduler\Module'], ], 'components' => [ 'errorHandler' => [ 'class' => 'webtoolsnz\scheduler\ErrorHandler' ], 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log\EmailTarget', 'mailer' =>'mailer', 'levels' => ['error', 'warning'], 'message' => [ 'to' => ['admin@example.com'], 'from' => [$params['adminEmail']], 'subject' => 'Scheduler Error - ####SERVERNAME####' ], 'except' => [ ], ], ], ], ]
also add this to the top of your config/console.php file
\yii\base\Event::on( \webtoolsnz\scheduler\console\SchedulerController::className(), \webtoolsnz\scheduler\events\SchedulerEvent::EVENT_AFTER_RUN, function ($event) { if (!$event->success) { foreach($event->exceptions as $exception) { throw $exception; } } } );
To implement the GUI for scheduler also add the following to your config/web.php
'bootstrap' => ['log', 'scheduler'], 'modules' => [ 'scheduler' => ['class' => 'webtoolsnz\scheduler\Module'], ],
After the configuration files have been updated, a tasks directory will need to be created in the root of your project.
Run the database migrations, which will create the necessary tables for scheduler
php yii migrate up --migrationPath=vendor/webtoolsnz/yii2-scheduler/src/migrations
Add a controller
<?php namespace app\modules\admin\controllers; use yii\web\Controller; /** * Class SchedulerController * @package app\modules\admin\controllers */ class SchedulerController extends Controller { public function actions() { return [ 'index' => [ 'class' => 'webtoolsnz\scheduler\actions\IndexAction', 'view' => '@scheduler/views/index', ], 'update' => [ 'class' => 'webtoolsnz\scheduler\actions\UpdateAction', 'view' => '@scheduler/views/update', ], 'view-log' => [ 'class' => 'webtoolsnz\scheduler\actions\ViewLogAction', 'view' => '@scheduler/views/view-log', ], ]; } }
Example Task
You can now create your first task using scheduler, create the file AlphabetTask.php inside the tasks directory in your project root.
Paste the below code into your task:
<?php namespace app\tasks; /** * Class AlphabetTask * @package app\tasks */ class AlphabetTask extends \webtoolsnz\scheduler\Task { public $description = 'Prints the alphabet'; public $schedule = '0 * * * *'; public function run() { foreach (range('A', 'Z') as $letter) { echo $letter; } } }
The above code defines a simple task that runs at the start of every hour, and prints the alphabet.
The $schedule property of this class defines how often the task will run, these are simply Cron Expression
Running the tasks
Scheduler provides an intuitive CLI for executing tasks, below are some examples
# list all tasks and their status $ php yii scheduler # run the task if due $ php yii scheduler/run --taskName=AlphabetTask # force the task to run regardless of schedule $ php yii scheduler/run --taskName=AlphabetTask --force # run all tasks $ php yii scheduler/run-all # force all tasks to run $ php yii scheduler/run-all --force
In order to have your tasks run automatically simply setup a crontab like so
*/5 * * * * admin php /path/to/my/app/yii scheduler/run-all > /dev/null &
Events & Errors
Events are thrown before and running individual tasks as well as at a global level for multiple tasks
Task Level
Event::on(AlphabetTask::className(), AlphabetTask::EVENT_BEFORE_RUN, function ($event) { Yii::trace($event->task->className . ' is about to run'); }); Event::on(AlphabetTask::className(), AlphabetTask::EVENT_AFTER_RUN, function ($event) { Yii::trace($event->task->className . ' just ran '.($event->success ? 'successfully' : 'and failed')); });
or at the global level, to throw errors in /yii
$application->on(\webtoolsnz\scheduler\events\SchedulerEvent::EVENT_AFTER_RUN, function ($event) { if (!$event->success) { foreach($event->exceptions as $exception) { throw $exception; } } });
You could throw the exceptions at the task level, however this will prevent further tasks from running.
License
The MIT License (MIT). Please see LICENSE for more information.
webtoolsnz/yii2-scheduler 适用场景与选型建议
webtoolsnz/yii2-scheduler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 83.72k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2016 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cron」 「task」 「scheduler」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webtoolsnz/yii2-scheduler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webtoolsnz/yii2-scheduler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webtoolsnz/yii2-scheduler 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony bundle to monitor and execute commands
Swoole server process management
Expose the DDev executable commands to the Robo task runner.
Scheduling extension for Yii2 framework
Reset scheduler tasks
A lightweight task queue on top of rybakit/phive-queue
统计信息
- 总下载量: 83.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 18
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2016-03-17