yii2mod / yii2-scheduling
Yii2框架的调度扩展
Requires
- php: >=5.6
- guzzlehttp/guzzle: ^6.3
- league/climate: ^3.2
- mtdowling/cron-expression: ~1.0
- nesbot/carbon: ~1.21
- symfony/process: ~3.2
- yiisoft/yii2: 2.0.*
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.0
- phpunit/phpunit: ~6.0
This package is not auto-updated.
Last update: 2024-09-14 19:04:11 UTC
README
Yii2 调度扩展
此扩展是Laravel的调度组件(https://laravel.net.cn/docs/master/artisan#scheduling-artisan-commands)的移植
安装
安装此扩展的首选方式是通过composer。
运行以下命令
php composer.phar require yii2mod/yii2-scheduling "*"
或者
"yii2mod/yii2-scheduling": "*"
将以下内容添加到你的composer.json文件的require部分。
描述
本项目受到Laravel的调度组件的启发,试图将其简洁性带到Yii框架中。引用自Laravel文档
In the past, developers have generated a Cron entry for each console command they wished to schedule.
However, this is a headache. Your console schedule is no longer in source control,
and you must SSH into your server to add the Cron entries. Let's make our lives easier.
安装后,你只需将单行命令放入crontab
* * * * * php /path/to/yii yii schedule/run --scheduleFile=@path/to/schedule.php 1>> /dev/null 2>&1
你可以将你的调度放入schedule.php
文件,或者在你的扩展或应用的启动过程中添加
调度示例
此扩展支持Laravel调度所有功能,除了环境和维护模式。
调度闭包
$schedule->call(function() { // Do some task... })->hourly();
调度终端命令
$schedule->exec('composer self-update')->daily();
运行应用命令
$schedule->command('migrate')->cron('* * * * *');
频繁作业
$schedule->command('foo')->everyFiveMinutes(); $schedule->command('foo')->everyTenMinutes(); $schedule->command('foo')->everyThirtyMinutes();
每日作业
$schedule->command('foo')->daily();
每日特定时间(24小时制)作业
$schedule->command('foo')->dailyAt('15:00');
每日两次作业
$schedule->command('foo')->twiceDaily();
每周工作日作业
$schedule->command('foo')->weekdays();
每周作业
$schedule->command('foo')->weekly(); // Schedule weekly job for specific day (0-6) and time... $schedule->command('foo')->weeklyOn(1, '8:00');
每月作业
$schedule->command('foo')->monthly();
特定日期作业
$schedule->command('foo')->mondays(); $schedule->command('foo')->tuesdays(); $schedule->command('foo')->wednesdays(); $schedule->command('foo')->thursdays(); $schedule->command('foo')->fridays(); $schedule->command('foo')->saturdays(); $schedule->command('foo')->sundays();
仅当回调为真时允许作业运行
$schedule->command('foo')->monthly()->when(function() { return true; });
仅当skip
回调为假时允许作业运行
$schedule->command('foo')->monthly()->skip(function() { return false; });
为计划作业设置自定义描述
$schedule->command('foo')->monthly()->description('command description');
将计划作业的输出电子邮件化
$schedule->command('foo')->sendOutputTo($filePath)->emailOutputTo('foo@example.com');
要查看已注册任务的列表,你可以使用以下命令schedule/list
php yii schedule/list +---+-------------------------------------+-------------+-----------------------------------------------------+ | # | Task | Expression | Command to Run | +---+-------------------------------------+-------------+-----------------------------------------------------+ | 1 | Delete the logs older than week | 0 0 * * 0 * | /usr/bin/php5 yii app/delete-logs-older-then-week | +---+-------------------------------------+-------------+-----------------------------------------------------+
如何在你的应用程序中使用此扩展?
你应该在@console/config/schedule.php
下创建以下文件(注意:你可以创建任何名称和扩展名以及服务器上的任何位置,只需简单调整以下命令中的scheduleFile名称即可)
<?php /** * @var \yii2mod\scheduling\Schedule $schedule */ // Place here all of your cron jobs // This command will execute ls command every five minutes $schedule->exec('ls')->everyFiveMinutes(); // This command will execute migration command of your application every hour $schedule->command('migrate')->hourly(); // This command will call callback function every day at 10:00 $schedule->call(function(\yii\console\Application $app) { // Some code here... })->dailyAt('10:00');
接下来,你应该将以下命令添加到你的crontab中
* * * * * php /path/to/yii yii schedule/run --scheduleFile=@console/config/schedule.php 1>> /dev/null 2>&1
就这样!现在所有你的cron作业都将按照你的schedule.php文件中的配置运行。
如何在你的扩展中使用此扩展?
首先,你应该在你的composer.json中将yii2mod\yii2-scheduling
依赖项包括进去
'require': {
"yii2mod/yii2-scheduling": "*"
}
接下来,你应该创建你扩展的启动类,如https://yiiframework.cn/doc-2.0/guide-structure-extensions.html#bootstrapping-classes中所述
在你的启动方法中放入以下代码
public function bootstrap(Application $app) { if ($app instanceof \yii\console\Application) { if ($app->has('schedule')) { /** @var omnilight\scheduling\Schedule $schedule */ $schedule = $app->get('schedule'); // Place all your shedule command below $schedule->command('my-extension-command')->dailyAt('12:00'); } } }
在你的扩展的README中添加用户注册schedule
组件和添加schedule/run
命令到crontab的信息,如上所述。
使用schedule
组件
如果你只在应用程序中使用调度(并且不希望扩展注册它们自己的cron作业),你不需要直接使用schedule
组件或将其定义在你的应用程序中。但是,如果你想允许扩展注册cron作业,你应该在应用程序配置中定义schedule
组件
'schedule' => 'yii2mod\scheduling\Schedule',
使用附加功能
如果你要使用Event的thenPing
方法,你应该将以下字符串添加到你的app的composer.json
中
"guzzlehttp/guzzle": "~5.3|~6.0"
关于时区说明
请注意,这是一个PHP扩展,因此它使用在php配置或您的Yii配置文件中定义的时区,所以请正确设置它们。