trendyminds/yii2-scheduling

Yii2框架的调度扩展

安装: 808

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 83

类型:yii2-extension

2.1.3 2024-04-10 21:36 UTC

This package is auto-updated.

Last update: 2024-09-10 22:36:41 UTC


README

本扩展是Laravel的Schedule组件(https://laravel.net.cn/docs/master/scheduling#scheduling-artisan-commands)的移植

安装

安装此扩展的最佳方式是通过 composer

运行以下命令之一:

php composer.phar require trendyminds/yii2-scheduling "*"

或添加以下内容到你的composer.json文件的require部分:

"trendyminds/yii2-scheduling": "*"

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();

默认使用yii\mutex\FileMutex或'mutex'应用程序组件(https://yiiframework.cn/doc-2.0/yii-mutex-mutex.html

在单一服务器上运行任务

为了使用此功能,你必须配置应用程序组件中的mutex,除了FileMutex:yii\mutex\MysqlMutexyii\mutex\PgsqlMutexyii\mutex\OracleMutexyii\redis\Mutex。此外,所有服务器都必须与同一个中心数据库/缓存服务器通信。

以下展示了Redis mutex的示例

'components' => [
    'mutex' => [
        'class' => 'yii\redis\Mutex',
        'redis' => [
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0,
        ]
    ],
],
$schedule->command('report:generate')
                ->fridays()
                ->at('17:00')
                ->onOneServer();

如何在你的应用中使用此扩展?

你应该在@console/config/schedule.php下创建以下文件(注意:你可以创建任何名称、任何扩展名的文件,并放置在服务器上的任何位置,只需调整以下命令中scheduleFile的名称即可)

<?php
/**
 * @var \trendyminds\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中包含对trendyminds\yii2-scheduling的依赖

...
'require': {
    "trendyminds/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 trendyminds\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' => 'trendyminds\scheduling\Schedule',

使用附加功能

如果您想使用Event的thenPing方法,应将以下字符串添加到您的应用程序的composer.json文件中:

"guzzlehttp/guzzle": "~5.0"

关于时区的说明

请注意,这是一个PHP扩展,因此它使用php配置或您的Yii配置文件中定义的时区,所以请正确设置它们。