看完文档 后总想知道是怎么样一个开始,又是怎样的一个结束!图片来源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
, 实现了接口 SelfHandling
和 ShouldQueue
, 或许你会问这两个接口啥规范都没规定啥用啊(先略过), 重点在两个 trait
内,对队列任务实现了各类操做,删除,重试,延迟等。
在分配任务的时候咱们使用了辅助函数 dispatch
,实际上是 IlluminateBus
下 Dispatcher
类的 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']; // 默认队列链接 }); }); }
下面看 dispatchToQueue
app
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