sharkom / yii2-cron
从浏览器创建定时任务,并查看运行日志
v1.1.12
2023-10-10 09:57 UTC
Requires
- dragonmantank/cron-expression: ^1.2
- kartik-v/yii2-grid: @dev
- sharkom/yii2-cronjob-widget: ^1
- symfony/process: ^5.0
- yiisoft/yii2: ~2.0.0
README
从浏览器创建定时任务,并查看运行日志
从vasadibt/yii2-cron分支,进行了一些修改
- 现在可以运行任何类型的脚本,而不仅仅是Yii2控制台命令
- 增加了除了数据库日志之外,还可以有日志文件的能力
- 移除了runquick功能
变更日志 2023年2月27日
- 在定时任务页面添加了手动运行按钮
- 添加了自动清理日志(使用模块参数purge_log_interval | 默认3个月)
- 修复了日志页面的一些图形问题
变更日志 2023年5月5日
- 添加了执行错误邮件通知
- 添加了自动解锁邮件通知
- 添加了模块参数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上
打开任务计划程序,创建一个新的任务
###邮件通知
为了接收执行错误的邮件通知,你需要配置
- 将模块参数sendNotifications设置为true(请检查本README中的Web应用程序和控制台应用程序配置)
- 在common/config/params.php中设置以下参数
- senderEmail
- NotificationsEmail
- 在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', ], ],
.