您好,欢迎访问一九零五行业门户网

Laravel 队列服务

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(),夜很深了,下面自己看吧!
其它类似信息

推荐信息