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(),夜很深了,下面自己看吧!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号