lexeo / yii2-scheduling

此包的最新版本(dev-master)没有提供许可证信息。

Yii2框架的调度扩展

安装: 207

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 83

类型:yii2-extension

dev-master 2020-05-24 23:51 UTC

This package is auto-updated.

Last update: 2024-09-25 10:12:30 UTC


README

本扩展受Laravel的Console\Scheduling组件启发,基于omnilight/yii2-scheduling,旨在修复发现的一些错误,并提供真正可行的解决方案。

安装

安装此扩展的首选方式是通过composer

运行

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

或在您的composer.json的require部分添加

"lexeo/yii2-scheduling": "*"

描述

该项目受Laravel的Scheduling组件启发,试图将它的简洁性带给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 Scheduling的大多数功能,但不包括环境和维护模式。

调度闭包

$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'应用程序组件。

在单个服务器上运行任务

要利用此功能,您必须在应用程序组件中配置mutex,除了FileMutex:yii\mutex\MysqlMutexyii\mutex\PgsqlMutexyii\mutex\OracleMutexyii\redis\Mutex。此外,所有服务器都必须与同一中央db/cache服务器通信。

以下显示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 \lexeo\yii2scheduling\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() {
    // 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中包含对lexeo\yii2-scheduling的依赖关系

...
'require': {
    "lexeo/yii2-schedule": "*"
}
...

接下来,您应该创建您扩展的引导类,如文档中所述

在您的引导方法中放置以下代码

public function bootstrap(Application $app)
{
    if ($app instanceof \yii\console\Application) {
        if ($app->has('schedule')) {
            /** @var lexeo\yii2scheduling\Schedule $schedule */
            $schedule = $app->get('schedule');
            // Place all your schedule command below
            $schedule->command('my-extension-command')->dailyAt('12:00');
        }
    }
}

在您的扩展的README中添加信息,以便用户可以为应用程序注册schedule组件,并将schedule/run命令添加到crontab,如上所述。

使用schedule组件

如果您仅在使用计划(并且不希望扩展能够注册它们自己的cron作业)的应用程序中使用计划,则不需要直接使用schedule组件或在应用程序中定义它。但如果您想允许扩展注册cron作业,则应在应用程序配置中定义schedule组件

'schedule' => 'lexeo\yii2scheduling\Schedule',

关于时区注意

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