demokn / yii2-scheduling
Yii2框架的调度扩展
Requires
- php: >=5.4.0
- dragonmantank/cron-expression: 1.*
- symfony/process: 2.6.* || 3.* || 4.*
- yiisoft/yii2: 2.0.*
Requires (Dev)
- phpunit/phpunit: 4.8.36
Suggests
- guzzlehttp/guzzle: Required to use the thenPing method on schedules (~5.0).
Conflicts
This package is auto-updated.
Last update: 2024-09-20 18:28:51 UTC
README
此扩展是Laravel的Schedule组件(https://laravel.net.cn/docs/master/scheduling#scheduling-artisan-commands)的移植
安装
安装此扩展的最佳方式是通过 composer。
运行以下命令之一
php composer.phar require omnilight/yii2-scheduling "*"
或者
"omnilight/yii2-scheduling": "*"
将其添加到您的 composer.json 文件的 require
部分。
描述
本项目受Laravel的Schedule组件的启发,试图将其简洁性带到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的所有功能,除了环境和维护模式。
调度闭包
$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; });
通过电子邮件发送计划作业的输出
$schedule->command('foo')->sendOutputTo($filePath)->emailOutputTo('foo@example.com');
防止任务冲突
$schedule->command('foo')->withoutOverlapping();
如果需要,您可以指定在“无冲突”锁过期之前必须过去多少分钟。默认情况下,锁将在24小时后过期
$schedule->command('foo')->withoutOverlapping(10);
在单个服务器上运行任务
要使用此功能,您的应用必须使用
memcached
或redis
缓存驱动程序作为默认缓存驱动程序。另外,所有服务器都必须与同一中央缓存服务器通信。
$schedule->command('report:generate') ->fridays() ->at('17:00') ->onOneServer();
后台任务
默认情况下,同时安排的多个命令将顺序执行。如果您有长时间运行的命令,这可能导致后续命令的启动时间比预期晚得多。如果您希望命令在后台运行,以便它们可以同时运行,您可以使用 runInBackground
方法
$schedule->command('report:generate')->daily()->runInBackground($currentScheduleFilePath);
runInBackground
方法只能在通过command
和exec
方法安排任务时使用。
如何在您的应用中使用此扩展?
您应该在 @console/config/schedule.php
之下创建以下文件(注意:您可以在您的服务器上的任何位置创建任何名称、扩展名的文件,只需简单调整下面命令中的 scheduleFile 名称即可)
<?php /** * @var \omnilight\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 中包含对 omnilight\yii2-scheduling
的依赖关系
...
'require': {
"omnilight/yii2-schedule": "*"
}
...
接下来,您应该创建您扩展的启动类,如 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
组件
如果您的应用中只使用计划任务(且不希望扩展能够注册它们自己的定时任务),则无需直接使用 schedule
组件或在应用中定义它。但如果您想允许扩展注册定时任务,则应在应用配置中定义 schedule
组件。
'schedule' => 'omnilight\scheduling\Schedule',
使用附加功能
如果您想使用 Event 的 thenPing
方法,您应将以下字符串添加到您的应用的 composer.json
文件中:
"guzzlehttp/guzzle": "~5.0"
时区说明
请注意,这是一个 PHP 扩展,因此它使用 PHP 配置或 Yii 的配置文件中定义的时区,因此请正确设置它们。