sharkom/yii2-cron

从浏览器创建定时任务,并查看运行日志

维护者

详细信息

github.com/SharKom/yii2-cron

源代码

安装次数: 1,859

依赖项: 0

建议者: 0

安全性: 0

星标: 3

关注者: 0

分支: 8

类型:yii2-extension

v1.1.12 2023-10-10 09:57 UTC

This package is auto-updated.

Last update: 2024-09-09 09:32:54 UTC


README

从浏览器创建定时任务,并查看运行日志

从vasadibt/yii2-cron分支,进行了一些修改

  1. 现在可以运行任何类型的脚本,而不仅仅是Yii2控制台命令
  2. 增加了除了数据库日志之外,还可以有日志文件的能力
  3. 移除了runquick功能

变更日志 2023年2月27日

  1. 在定时任务页面添加了手动运行按钮
  2. 添加了自动清理日志(使用模块参数purge_log_interval | 默认3个月)
  3. 修复了日志页面的一些图形问题

变更日志 2023年5月5日

  1. 添加了执行错误邮件通知
  2. 添加了自动解锁邮件通知
  3. 添加了模块参数sendNotifications

安装

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

运行以下命令

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

或者添加

"sharkom/yii2-cron": "*"

到你的composer.json文件的require部分。

迁移

在终端运行以下命令进行数据库迁移

yii migrate/up --migrationPath=@sharkom/cron/migrations

或者使用命名空间迁移(需要至少Yii 2.0.10)

// Add namespace to console config:
'controllerMap' => [
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationPath' => [
            '@sharkom/cron/migrations',
        ],
    ],
],

然后运行

yii migrate/up

Web应用程序配置

在Web应用程序中启用Cron作业管理模块

简单示例

'modules' => [
    'cron' => [
        'class' => 'sharkom\cron\Module',
        'params'=>[
            'sendNotifications'=>true,
        ]
    ],
],

控制台应用程序配置

在控制台应用程序中启用Cron作业管理模块

简单示例

'modules' => [
    'cron' => [
        'class' => 'sharkom\cron\Module',
        'params'=>[
            'sendNotifications'=>true,
        ]
    ],
],

调度配置

设置服务器调度以运行以下命令

在Linux上

使用crontab -e命令或编辑/etc/crontab文件,将任务添加到你想运行脚本的用户的crontab中(可能是非root用户)

* * * * * <your-application-folder>/yii cron/cron/run 2>&1

在Windows上

打开任务计划程序,创建一个新的任务

###邮件通知

为了接收执行错误的邮件通知,你需要配置

  1. 将模块参数sendNotifications设置为true(请检查本README中的Web应用程序和控制台应用程序配置)
  2. 在common/config/params.php中设置以下参数
    1. senderEmail
    2. NotificationsEmail
  3. 在common/config/main-local.php中配置邮件发送器
return [
    'senderEmail'=>'notifications@yourapp.net',
    'NotificationsEmail'=>'notifications@yourapp.net',
];
'mailer' => [
   'class' => 'yii\swiftmailer\Mailer',
   // send all mails to a file by default. You have to set
   // 'useFileTransport' to false and configure a transport
   // for the mailer to send real emails.
   'useFileTransport' => false,
   'transport' => [
      'class' => 'Swift_SmtpTransport',
      'encryption' => 'tls',
      'host' => 'your smtp relay',
      'port' => '25',
      'username' => 'your user',
      'password' => 'your password',
   ],
],

.