Heim  >  Artikel  >  Backend-Entwicklung  >  Laravel 队列服务

Laravel 队列服务

高洛峰
高洛峰Original
2016-11-15 14:23:301049Durchsuche

QueueServiceProvider

Laravel 各种服务的注册大多都是通过各种 ServiceProvider 进行绑定的,队列服务也不例外,打开 namespace IlluminateQueueQueueServiceProvider 文件定位到 register 方法,

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, 实现了接口 SelfHandling 和 ShouldQueue , 或许你会问这两个接口啥规范都没规定啥用啊(先略过), 重点在两个 trait 内,对队列任务实现了各种操作,删除,重试,延迟等。
在分配任务的时候我们使用了辅助函数 dispatch ,其实是 IlluminateBus 下 Dispatcher 类的 dispatch方法

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 下的

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']; // 默认队列连接
        });
    });
}

下面看 dispatchToQueue

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(),夜很深了,下面自己看吧!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn