Laravel 的任務(wù)調(diào)度(計(jì)劃任務(wù))功能

參考文檔 http://d.laravel-china.org/docs/5.4/scheduling

解決的問題:不用頻繁在服務(wù)器操作crontab.

自定義的 Artisan 命令

makde:command order

打開文件:

laravel\app\Console\Commands\order.php

如下修改:
修改為自己的命令和要處理的內(nèi)容

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '命令描述';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        Log::info('test');
    }
}

然后修改: laravel\app\Console\Kernel.php 文件

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
        Commands\order::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
         $schedule->command('inspire')
                  ->hourly();
         
         $schedule->command('order:make')->everyMinute();
    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

啟用計(jì)劃任務(wù):在服務(wù)器中加入到計(jì)劃任務(wù) crontab -e

* * * * * php /home/vagrant/Code/laravel/artisan schedule:run >> /dev/null 2>&1

打開日志文件查看效果

laravel\storage\logs\laravel.log
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容