vildanbina / laravel-scheduler
用于在Laravel应用程序中调度和管理cron任务的服务
dev-master
2021-11-17 21:19 UTC
This package is auto-updated.
Last update: 2024-09-18 03:17:24 UTC
README
安装
第一步
为Laravel < 5.5添加提供者
vildanbina\Scheduler\SchedulerServiceProvider::class,
发布配置和CronTasksList类文件
php artisan vendor:publish
并在需要时选择"提供者:vildanbina\Scheduler\SchedulerServiceProvider"。
必须发布的文件
config/scheduler.php
app/Console/CronTasksList.php
创建数据库表
php artisan migrate
让我们完成设置
将您的命令从App\Console\Kernel
schedule()函数移动到新文件:CronTasksList.php
特质。
在命令列表中添加下一行代替
<?php namespace App\Console; use vildanbina\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
中自行配置它。
创建操作后,您将拥有计划任务列表,并且它将准备就绪但使用调度器您有更多强大的功能。
- 您可以为相同的命令创建具有不同参数的不同任务并单独运行。
在下一张截图中,您可以看到相同的计划任务,对于第一个任务,用户等于1,选项为--client=2,对于下一个任务,用户等于3,选项为--client=4。
许可证
Laravel调度器是开源软件,许可证为MIT许可证。