easyconn/yii2-scheduling

此软件包最新版本(1.0.8.3)没有可用的许可信息。

Yii2框架的调度扩展

维护者

详细信息

github.com/zz/yii2-scheduling

源代码

安装: 10

依赖: 0

建议者: 0

安全: 0

星级: 0

关注者: 2

分支: 83

类型:yii2-extension

1.0.8.3 2018-04-10 06:06 UTC

This package is auto-updated.

Last update: 2024-08-29 04:43:09 UTC


README

此扩展是Laravel的Schedule组件(https://laravel.net.cn/docs/master/artisan#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');

如何在您的应用程序中使用此扩展?

您应该在 @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 组件

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

'schedule' => 'omnilight\scheduling\Schedule',

使用附加功能

如果您想使用Event的thenPing方法,则应将以下字符串添加到您的应用的composer.json

"guzzlehttp/guzzle": "~5.0"

关于时区说明

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