Laravel 隊列服務

Laravel

看完文檔 後總想知道是怎麼樣一個開始,又是怎樣的一個結束!圖片來源php

QueueServiceProvider

Laravel 各類服務的註冊大多都是經過各類 ServiceProvider 進行綁定的,隊列服務也不例外,打開 namespace IlluminateQueueQueueServiceProvider 文件定位到 register 方法,html

public function register()
{
   // 註冊隊列管理器 一旦實例化,爲隊列鏈接器註冊各類解析器,這些鏈接器負責建立接受隊列配置和實例化各類不一樣隊列處理的類。
   // 按照配置文件註冊一個默認鏈接方式 在此使用 redis
    $this->registerManager();
   // 註冊隊列各類命令 隊列鏈接 重啓等。
    $this->registerWorker();
   // 註冊隊列監聽命令
    $this->registerListener();
   // 5.1後棄用
    $this->registerSubscriber();
   // 註冊隊列失敗處理
    $this->registerFailedJobServices();
   // Register the Illuminate queued closure job. 什麼用,後面再看。
    $this->registerQueueClosure();
}

任務建立與分配

php artisan make:job SendReminderEmail

按照文檔的方式生成了一個隊列任務類,該類繼承了 namespaceAppJobsJob, 實現了接口 SelfHandlingShouldQueue , 或許你會問這兩個接口啥規範都沒規定啥用啊(先略過), 重點在兩個 trait 內,對隊列任務實現了各類操做,刪除,重試,延遲等。
在分配任務的時候咱們使用了輔助函數 dispatch ,實際上是 IlluminateBusDispatcher 類的 dispatch方法laravel

public function dispatch($command, Closure $afterResolving = null)
{    

    if ($this->queueResolver && $this->commandShouldBeQueued($command)) {
        // 隊列執行
        return $this->dispatchToQueue($command);
    } else {
        // 當即執行
        return $this->dispatchNow($command, $afterResolving);
    }
}
protected function commandShouldBeQueued($command)
{
    if ($command instanceof ShouldQueue) {  // 就這用。。
        return true;
    }

    return (new ReflectionClass($this->getHandlerClass($command)))->implementsInterface(
        'Illuminate\Contracts\Queue\ShouldQueue'
    );
}

在此,咱們先看下 namespace IlluminateBusBusServiceProvider 下的redis

public function register()
{
    $this->app->singleton('Illuminate\Bus\Dispatcher', function ($app) {
        return new Dispatcher($app, function () use ($app) {

        // 'queue.connection' => 'Illuminate\Contracts\Queue\Queue', 再回看 QueueServiceProvider 的 registerManager 方法,就很清晰了。

            return $app['Illuminate\Contracts\Queue\Queue']; // 默認隊列鏈接
        });
    });
}

下面看 dispatchToQueueapp

public function dispatchToQueue($command)
{
    $queue = call_user_func($this->queueResolver); // 在此爲設置的默認值 將實例化 RedisQueue
    
    // 異常則拋出!
    if (! $queue instanceof Queue) {
        throw new RuntimeException('Queue resolver did not return a Queue implementation.');
    }
    if (method_exists($command, 'queue')) {
        // 能夠自定義
        return $command->queue($queue, $command);
    } else {
        // 在此使用的是進入隊列方式  最終結果相似 $queue->push(); 看 RedisQueue 下的 push 方法。
        return $this->pushCommandToQueue($queue, $command);
    }
}

上面任務進入隊列的整個流程就明白了。那任務出隊列呢?在文檔中咱們能夠看到,咱們經過執行 php artisan queue:work 這條語句進行隊列的監聽,那在此就看下 namespace IlluminateQueueConsoleWorkCommand::fire(),夜很深了,下面本身看吧!ide

相關文章
相關標籤/搜索