首頁  >  文章  >  php框架  >  laravel定時任務用法及原理詳解

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

WBOY
WBOY轉載
2022-05-30 11:52:235521瀏覽

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

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

【相關推薦:laravel影片教學

應用程式場景

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

基本用法

首先配置crontab:

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

上面的意思是設定定時任務每分鐘執行一次,具體的業務配置,放在了App\Console\Kernel的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 這個指定是在vendor\illuminate\console\Scheduling\ScheduleRunCommand 類別裡面進行定義的,定義的形式和一般的定時任務相同:

/**
 * 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, [
            'Illuminate\Console\Scheduling\ScheduleRunCommand',
        ]);
    }

所以php artisan schedule:run 指令就是框架內建的一個指令。
在指令啟動的時候,會預設找類別中的handle 方法執行:

/** vendor\illuminate\console\Command.php
 * Execute the console command.
 * 
 * @param  \Symfony\Component\Console\Input\InputInterface  $input
 * @param  \Symfony\Component\Console\Output\OutputInterface  $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  \Illuminate\Contracts\Container\Container  $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  \Illuminate\Console\Scheduling\Event  $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影片教學

以上是laravel定時任務用法及原理詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除