yii2mod/yii2-cron-log

用于记录定时任务组件

安装次数: 22,092

依赖项: 1

建议者: 0

安全性: 0

星标: 23

关注者: 7

分支: 8

开放问题: 0

类型:yii2-extension

1.6 2018-11-06 13:49 UTC

This package is auto-updated.

Last update: 2024-09-07 06:59:02 UTC


README

Yii2 Cron Log 扩展


用于记录定时任务的组件。

Latest Stable Version Total Downloads License Build Status

安装

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

运行以下命令:

php composer.phar require --prefer-dist yii2mod/yii2-cron-log "*"

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

"yii2mod/yii2-cron-log": "*"

配置

数据库迁移

在开始使用此扩展之前,我们还需要准备数据库。

php yii migrate/up --migrationPath=@yii2mod/cron/migrations

错误处理器和文件互斥设置

错误处理器必须在控制台配置中定义,它将用于将异常记录到数据库中。

FileMutex 通过本地文件系统文件实现了互斥 "lock" 机制。

将以下代码添加到您的控制台应用程序配置中

'components' => [
    'errorHandler' => [
        'class' => 'yii2mod\cron\components\ErrorHandler',
    ],
    'mutex' => [
        'class' => 'yii\mutex\FileMutex'
    ],
],

用法

  1. 要访问已执行命令列表,您需要在任何控制器中定义 CronLogAction(例如 /modules/admin/SettingsController.php)
    public function actions()
    {
        return [
            'cron' => 'yii2mod\cron\actions\CronLogAction',
            // Also you can override some action properties in following way:
            'cron' => [
                'class' => 'yii2mod\cron\actions\CronLogAction',
                'searchClass' => [
                    'class' => 'yii2mod\cron\models\search\CronScheduleSearch',
                    'pageSize' => 10
                ],
                'view' => 'custom name of the view, which should be rendered.'
            ]
        ];
    }

此操作用于查看已执行命令列表: http://project.com/admin/settings/cron

  1. 要记录定时任务操作,应将行为添加到所有需要记录的命令中。在以下示例中,CronLoggerBehavior 将记录 index 操作。
namespace app\commands;

use yii\console\Controller;

/**
 * This command echoes the first argument that you have entered.
 *
 * This command is provided as an example for you to learn how to create console commands.
 *
 */
class HelloController extends Controller
{
    public function behaviors()
    {
        return [
            'cronLogger' => [
                'class' => 'yii2mod\cron\behaviors\CronLoggerBehavior',
                'actions' => ['index']
            ],
            // Example of usage the `MutexConsoleCommandBehavior`
            'mutexBehavior' => [
                'class' => 'yii2mod\cron\behaviors\MutexConsoleCommandBehavior',
                'mutexActions' => ['index'],
                'timeout' => 3600, //default 0
            ]
        ];
    }

    /**
     * This command echoes what you have entered as the message.
     * @param string $message the message to be echoed.
     */
    public function actionIndex($message = 'hello world')
    {
        echo $message . "\n";
    }
}

国际化

在此扩展中引入的所有文本和信息都可在 'yii2mod-cron-log' 类别下进行翻译。您可以使用以下应用程序配置使用此扩展提供的翻译:

return [
    'components' => [
        'i18n' => [
            'translations' => [
                'yii2mod-cron-log' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@yii2mod/cron/messages',
                ],
                // ...
            ],
        ],
        // ...
    ],
    // ...
];

支持我们

您的业务是否依赖于我们的贡献?通过 Patreon 联系我们并支持我们。所有承诺都将用于分配人员以维护和开发新功能。