boehsermoe / luya-module-scheduler
LUYA 调度模块。
Requires
- psr/log: ~1.0.0
This package is auto-updated.
Last update: 2024-09-14 19:42:26 UTC
README
安装
composer require boehsermoe/luya-module-scheduler:"~1.0.0"
要将模块添加到您的项目中,请进入配置的模块部分
return [ 'modules' => [ // ... 'scheduleradmin' => [ 'class' => 'luya\scheduler\admin\Module', ], 'scheduler' => [ 'class' => 'luya\scheduler\frontend\Module', 'accessToken' => '{token for web access}' // optional for webcron ], // ... ], ];
./vendor/bin/luya migrate ./vendor/bin/luya import
别忘了为调度模块分配用户组权限。
通过 CLI 手动启动作业
手动启动所有已过期作业
./luya scheduler/run
执行指定的作业
./luya scheduler/run/now {id/name of the job}
通过 cron 触发作业
每分钟通过 cron 启动所有已过期作业
* * * * * ./luya scheduler/run
通过其他方式触发作业
通过 Webcron
通过调用路由 https://{Host}/scheduler/run?token={从配置中获取的访问令牌} 来启动已过期作业。您可以使用此 URL 用于 Webcron,如 https://cron-job.org/de/ 或 https://uptimerobot.com/。或任何其他 Webcron 服务。
通过 yii 应用程序事件
[
...
'on afterRequest' => function() {
if (!$this->isCli()) {
Yii::$app->getModule('scheduler')->runExpiredJobsAsync();
}
},
...
]
CommandJob:执行控制台命令
例如,您可以通过添加新的 CommandJob(调度 -> 命令作业 -> 添加)来每小时刷新缓存,并插入 "cache/flush-all" 作为命令。命令是控制台命令的路由。
自定义作业
您还可以在路径 "{appBasePath}/schedulers" 或 "{moduleBasePath}/schedulers" 中编写您自己的作业类。每个作业都必须继承自 BaseJob。
class ExampleJob extends \luya\scheduler\models\BaseJob { public function run() { // Do your job. } }
一个具有文本字段作为选项的作业可能看起来像这样
class ExampleTextJob extends \luya\scheduler\models\BaseJob { public $text; public function rules() { return array_merge(parent::rules(), [ [['text'], 'required'] ]); } public function extraFields() { return [ 'text' ]; } public function ngrestExtraAttributeTypes() { return [ 'text' => 'text', ]; } public function run() { // Do your job // // echo $this->text; } }
更多示例
对于文件备份:https://github.com/boehsermoe/luya-module-backup/blob/master/src/schedules/FileBackupJob.php
对于数据库备份:https://github.com/boehsermoe/luya-module-backup/blob/master/src/schedules/DatanbaseBackupJob.php