laravel定時任務用法及原理詳解

本篇文章給大家帶來了關于laravel的相關知識,其中主要介紹了關于定時任務的用法以及原理的相關內容,根據應用場景講解一下定時任務的相關問題,下面一起來看一下,希望對大家有幫助。

laravel定時任務用法及原理詳解

【相關推薦:laravel

應用場景

一個網站系統往往會有很多定時任務要執行。例如推送訂閱消息,統計相關數據等,Linux一般采用crontab對定時任務進行設置和管理,但是隨著任務的增多,管理定時任務就比較麻煩,容易管理混亂。laravel 對此的解決方案是只設置一條定時任務,業務中所有的定時任務在這條定時任務進行處理和判斷,實現了在代碼層面對定時任務的管理。

基本用法

首先配置crontab:

* * * * * php artisan schedule:run >> /dev/null 2>&1

上面的意思是設置定時任務每分鐘執行一次,具體的業務配置,放在了AppConsoleKernel 的 schedule 方法中:

class Kernel extends ConsoleKernel{     Protected function schedule(Schedule $schedule)     {                 //綜合數據統計         $schedule->command('complex_data_log')         ->everyMinute() //每分鐘執行一次(除此之外還有,每五、十、十五、三十...,不同方法設置的默認時間不同)         ->withoutOverlapping() //防止重復執行         ->onOneServer() //在單臺服務器上跑         ->runInBackground() //任務后臺運行         //->appendOutputTo('log_path')//日志輸出,默認追加         ->sendOutputTo('log_path'); //日志輸出,默認覆蓋先前日志     }}

原理解析:

基本原理:
schedule:run 這個指定是在vendorilluminateconsoleSchedulingScheduleRunCommand 類里面進行定義的,定義的形式和一般的定時任務相同:

/**  * The console command name.  *  * @var string  */protected $name = 'schedule:run';

laravel 解析命令的時候,ScheduleRunCommand 這個類與 Kernel 類里面的 commands 數組進行了合并:

	/**      * Get the commands to add to the application.      *      * @return array      */     protected function getCommands()     {         return array_merge($this->commands, [             'IlluminateConsoleSchedulingScheduleRunCommand',         ]);     }

所以 php artisan schedule:run 命令就是框架內置的一個命令。
在命令啟動的時候,會默認找類中的handle 方法進行執行:

/** vendorilluminateconsoleCommand.php  * Execute the console command.  *   * @param  SymfonyComponentConsoleInputInputInterface  $input  * @param  SymfonyComponentConsoleOutputOutputInterface  $output  * @return mixed  */protected function execute(InputInterface $input, OutputInterface $output){     return $this->laravel->call([$this, 'handle']);}

php artisan schedule:run 指令會每分鐘掃描Kernel::schedule里面注冊的所有指令,并判斷該指令是否已經到達執行周期,如果到達,就推入待執行隊列:

    /**      * Schedule the event to run every minute.      * 代碼每分鐘執行一次      * @return $this      */     public function everyMinute()     {         return $this->spliceIntoPosition(1, '*');     }          /**      * Splice the given value into the given position of the expression.      * 拼接定時任務表達式      * @param  int  $position      * @param  string  $value      * @return $this      */     protected function spliceIntoPosition($position, $value)     {         $segments = explode(' ', $this->expression);          $segments[$position - 1] = $value;          return $this->cron(implode(' ', $segments));     }

ScheduleRunCommand::handle函數:

/**      * Execute the console command.      *       * @return void      */     public function handle()     {         foreach ($this->schedule->dueEvents($this->laravel) as $event) {             if (! $event->filtersPass($this->laravel)) {                 continue;             }             $this->runEvent($event);         }     }

避免任務重疊:
有時候單個定時任務執行時間過長,到了下一個執行時間后,上一次的執行任務還沒有跑完,這個時候,我們可以采用withoutOverlapping()方法,避免任務重疊。在 withoutOverlapping方法中,給對應的任務加鎖(onOneServer 方法同理):

public function create(Event $event){     return $this->cache->store($this->store)->add(         $event->mutexName(), true, $event->expiresAt     );}

只有拿到對應的任務鎖,才能執行任務:

/**      * Run the given event.      * 運行任務      * @param  IlluminateContractsContainerContainer  $container      * @return void      */     public function run(Container $container)     {         if ($this->withoutOverlapping &&             ! $this->mutex->create($this)) {             return;         }                  //判斷是否是后臺運行         $this->runInBackground                     ? $this->runCommandInBackground($container)                     : $this->runCommandInForeground($container);     }

任務后臺運行:
由于定時任務是依次執行的,上一個任務執行時間過長,會影響下一個任務的執行時間,所以我們可以采用runInBackground方法,將任務放到后臺執行,有點類似于shell 中 & 的作用:

/**      * Build the command for running the event in the background.      * 構建定時任務后臺運行語句      * @param  IlluminateConsoleSchedulingEvent  $event      * @return string      */     protected function buildBackgroundCommand(Event $event)     {         $output = ProcessUtils::escapeArgument($event->output);          $redirect = $event->shouldAppendOutput ? ' >> ' : ' > ';          $finished = Application::formatCommandString('schedule:finish').' "'.$event->mutexName().'"';          return $this->ensureCorrectUser($event,             '('.$event->command.$redirect.$output.' 2>&1 '.(windows_os() ? '&' : ';').' '.$finished.') > '             .ProcessUtils::escapeArgument($event->getDefaultOutput()).' 2>&1 &'         );     }

其他用法:

除了上面的方法,我們還可以用laravel 的定時任務去調用Shell 命令:

$schedule->exec('node /home/forge/script.js')->daily();

也可以使用閉包進行調度:

$schedule->call(function () {     DB::table('recent_users')->delete();})->daily();

想了解更多使用方法的話,可以查看laravel 的文檔:

https://laravelacademy.org/post/19517.html

【相關推薦:laravel

以上就是

? 版權聲明
THE END
喜歡就支持一下吧
點贊12 分享