anonymous-php/yii-scheduling

Yii 1.1.* 框架的调度扩展

安装: 126

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 1

开放问题: 0

类型:yii-extension

1.0.0 2019-05-07 13:50 UTC

This package is auto-updated.

Last update: 2024-09-08 01:46:01 UTC


README

此扩展是 omnilight/yii2-scheduling (https://github.com/omnilight/yii2-scheduling) 的移植

安装

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

运行以下命令:

php composer.phar require anonymous-php/yii-scheduling "*"

或者

"anonymous-php/yii-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.

安装后,您需要创建一个扩展 Anonymous\Scheduling\ScheduleCommand 类的控制台命令,并在其中实现包含您作业的 schedule() 方法

<?php

class ScheduleCommand extends \Anonymous\Scheduling\ScheduleCommand
{
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('foo')->monthly();
    }
}

设置完成后,只需在 crontab 中添加一行即可

* * * * * php /path/to/yii yii schedule run 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;
});

通过电子邮件发送调度作业的输出

$schedule->command('foo')->sendOutputTo($filePath)->processOutput(function ($textBody, $app) 
{
    if (trim($textBody) === '' ) {
        return;
    }

    $app->mailer
        ->compose()
        ->setTextBody($textBody)
        ->setSubject('Cronjob notification')
        ->setTo('admin@example.com')
        ->send();
});

防止任务冲突

$schedule->command('foo')->withoutOverlapping();

默认使用 FileMutex 或 'mutex' 应用程序组件 (https://github.com/yiisoft/mutex)

在单个服务器上运行任务

为了利用此功能,您必须在应用程序组件中配置互斥锁,除了 FileMutex: Yiisoft\Mutex\MysqlMutexYiisoft\Mutex\PgsqlMutexYiisoft\Mutex\OracleMutexYiisoft\Mutex\RedisMutex。此外,所有服务器都必须与同一中心数据库/缓存服务器通信。

以下显示了 redis mutex 示例

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

使用附加功能

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

"guzzlehttp/guzzle": "~5.0"

关于时区的说明

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