美文网首页PHP
laravel 任务调度的使用

laravel 任务调度的使用

作者: 爱吃秋葵的莫冲 | 来源:发表于2018-02-14 11:07 被阅读630次

中文说明
上面中文说明其实已经很清楚了,把我在做的过程中的坑记录下来。

  • 创建 Command
    参考
    要创建一个新的命令,可以使用 make:command 命令。这个命令会创建一个命令类并存放在 app/Console/Commands 目录。 不必担心不存在这个目录,运行 make:command 命令时会首先创建这个目录。生成的命令将会包括所有默认存在的属性和方法:
php artisan make:command SendEmails

demo:

<?php

/*
 * This file is part of PHP CS Fixer.
 * (c) Fabien Potencier <fabien@symfony.com>
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace App\Console\Commands;

use App\Repositories\FeedRepository;
use Illuminate\Console\Command;

class CacheFeeds extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'feed:cache';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'feeds 缓存';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        set_time_limit(60 * 30);

        $feed = new FeedRepository();
        $feed->cacheFeeds();
    }
}

新增成功后,执行 php artisan list 可以见到刚刚创建的command:feed:cache

  • 将新增的command 添加到schedule
    修改./app/Console/Kernel.php,添加$commands,在 schedule function 中定义任务调度的执行频率和输出log等配置。需要注意的是 command('feed:cache') 输入的是在command中定义的 $signature
<?php

/*
 * This file is part of PHP CS Fixer.
 * (c) Fabien Potencier <fabien@symfony.com>
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace App\Console;

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 = [
        \App\Console\Commands\CacheFeeds::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param \Illuminate\Console\Scheduling\Schedule $schedule
     */
    protected function schedule(Schedule $schedule)
    {
        $filePath = storage_path().'/logs/cache.log';
        $schedule->command('feed:cache')
                 ->appendOutputTo($filePath)
                 ->cron('*/15 * * * *') 
                 ->after(function () {
                     echo datetime().' feed cache completed! '.PHP_EOL;
                 })
                 ;
    }

    /**
     * Register the Closure based commands for the application.
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}
  • 在cronjob中添加
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

/path/to/artisan 就是项目地址,例如 /home/laravel-project/artisan

添加后重启crontab,service cron restart,到执行时间会在输出的log中见到。
注意点:

  • crontab 的命令建议要手打,不要复制粘贴,>> /可能会被转义
  • 可以手动执行 php artisan feed:cache 看下单个命令有无问题
  • 手动执行 php artisan schedule:run 会出no command ready to run 的提示,因为schedule的执行机制是每分钟执行一次,当系统当前时间除以schedule的循环时间是整数时才会执行
  • schedule:run 不能放入supervisor执行,因为每次执行后会自动退出,导致supervisor Failed 退出

相关文章

  • laravel 任务调度的使用

    中文说明上面中文说明其实已经很清楚了,把我在做的过程中的坑记录下来。 创建 Command参考要创建一个新的命令,...

  • Laravel 任务调度问题

    换地方了 Laravel 任务调度问题

  • Laravel任务调度的简单使用

    创建任务 在命令行执行下面的命令生产自定义任务文件,比如说创建一个定时取消订单的任务OrderCancel 命令执...

  • laravel 任务调度

    1.在代码里面写调度任务,在Console文件夹下面Kernel.php写调取任务命令 2.在Commands文件...

  • laravel 任务调度

    创建定时任务文件 编辑文件内容 修改Kernel 文件 添加定时任务

  • laravel任务调度

    crontab -e #添加代码 * * * * * /usr/bin/php7.0 /var/www/html/...

  • laravel 调度任务

    larave 一直是国内外备受欢迎的框架,功能强大、结构优雅,其中 artisan 命令 也一直备受青睐。接下来就...

  • laravel任务调度

    1.创建定时任务 编写以下cron语句: /usr/bin/php为PHP路径,/www/wwwroot/guxi...

  • 30. 配置 Cron 调度器

    Laravel 提供了便利的方式来调度 Cron 任务,通过Artisan 命令 schedule:run ,调度...

  • Laravel 任务调度 ( Console )

    在以前, 开发者需要为每一个需要调度的任务编写一个 Cron 条目, 这是很让人头疼的事. 你的任务调度不在源码控...

网友评论

    本文标题:laravel 任务调度的使用

    本文链接:https://www.haomeiwen.com/subject/jmfafxtx.html