matviib / scheduler
laravel 应用中调度和管理 cron 任务的服务
v1.0.4
2019-04-22 07:08 UTC
This package is auto-updated.
Last update: 2024-09-22 19:17:37 UTC
README
安装
第一步
为 Laravel < 5.5 添加提供者
MatviiB\Scheduler\SchedulerServiceProvider::class,
发布配置和 CronTasksList 类文件
php artisan vendor:publish
如果需要,请选择 "提供者: MatviiB\Scheduler\SchedulerServiceProvider"。
必须发布的文件
config/scheduler.php
app/Console/CronTasksList.php
创建数据库表
php artisan migrate
让我们完成设置
将您的命令从 App\Console\Kernel
中的 schedule() 函数移动到新的文件:CronTasksList.php
特性。
将下一行添加到 schedule() 函数中,而不是命令列表
<?php namespace App\Console; use MatviiB\Scheduler\Console\Kernel as SchedulerKernel; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ .. .. ]; /** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { // make changes just here // cut your commands from here // and write next line with(new SchedulerKernel())->schedule($schedule); }
将您的命令粘贴到 app/Console/CronTasksList.php
特性中
<?php namespace App\Console; use Illuminate\Console\Scheduling\Schedule; /** * Trait CronTasksList * * To use: uncomment all lines and copy your commands list * from app/Console/Kernel.php schedule() to tasks() function. * * @package App\Console */ trait CronTasksList { public function tasks(Schedule $schedule) { // paste your commands here $schedule->command('example:command')->yearly()->withoutOverlapping(); } }
如果到目前为止一切都已完成,您可以运行以下命令,它将显示您的当前命令列表
php artisan scheduler:show
您将看到类似以下内容
Scheduler is disabled.
You see standard tasks list.
+-----------------+------------------------------+-----------+-------------+-----+----------+
| command | description | is_active | expression | w_o | interval |
+-----------------+------------------------------+-----------+-------------+-----+----------+
| command:name | Description for command:name | 1 | 0 * * * * * | 1 | 1 hour |
| example:command | Command description | 1 | * * * * * * | 1 | 1 minute |
+-----------------+------------------------------+-----------+-------------+-----+----------+
要使用调度器,您需要将命令复制到调度器表中。
注意:每次执行 scheduler:create
都会软删除旧任务并创建新的命令数据。
php artisan scheduler:create
要使用调度器,您需要在您的 .env
中添加以下行来启用它
SCHEDULER_ENABLED=true
让我们检查状态和计划的任务
php artisan scheduler:show
您将看到类似以下内容
Scheduler is enabled.
You see scheduled tasks list configured with Scheduler.
+-----------------+------------------------------+-----------+-------------+-----+----------+
| command | description | is_active | expression | w_o | interval |
+-----------------+------------------------------+-----------+-------------+-----+----------+
| command:name | Description for command:name | 1 | 0 * * * * * | 1 | 1 hour |
| example:command | Command description | 1 | * * * * * * | 1 | 1 minute |
+-----------------+------------------------------+-----------+-------------+-----+----------+
用法
默认情况下,您可以在 /scheduler
页面上管理您的计划任务。
您还可以在 config/scheduler.php
中自行配置。
创建操作后,您将获得计划任务列表,并且它将准备就绪,但使用调度器您还有一些更强大的功能。
- 您可以为同一命令创建不同的任务,使用不同的参数并分别运行。
在下一张截图上,您可以看到相同的计划任务,对于第一个任务,参数 user 等于 1,选项 --client 等于 2;对于下一个任务,参数 user 等于 3,选项 --client 等于 4。
- 下一个强大的功能 - 您可以从 UI 立即用不同的参数和选项运行任务。
许可
Laravel 调度器是开源软件,许可协议为 MIT 许可。