Use DI container to resolve worker listeners and jobs

未关闭
#194 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
php
领域
backend

调研方向

从实例化已配置 listener 的 WorkerCommand 开始,跟踪 container 直到 Processor。然后追踪动态选择的 job 是如何创建的,并找出现有的测试或入口点,这些测试或入口点涵盖 listener 和 job dispatch。当已配置的 listener 和受支持的动态 job 能够使用可用的 container,并在不存在 container 时保持原有行为,即表示完成。

由索引模型根据 Issue 内容生成。

描述

enhancement

Description

The WorkerCommand accepts a ContainerInterface and passes it to the Processor, which suggests that dependency injection is supported and intended for queue workers.

However, the configured worker listener is instantiated directly with new, which means the DI container is bypassed:

$listener = new $listenerClassName();

This makes it impossible to use constructor dependency injection in a custom worker listener.

There is a similar issue when a worker listener needs to instantiate a job dynamically based on the message target. If the listener uses:

$job = new $class();

the job is also instantiated outside the DI container, making constructor injection in jobs impossible.

Environment

  • CakePHP: 5.3.7
  • cakephp/queue: 2.3.1
  • PHP: 8.3

Current behavior

WorkerCommand receives the DI container:

public function __construct(
    protected readonly ?ContainerInterface $container = null,
) {
}

and passes it to the processor:

return new $processorClass($logger, $this->container);

However, when the configured worker listener is created, the container is not used:

$listenerClassName = Configure::read(sprintf('Queue.%s.listener', $config));

/** @var \Cake\Event\EventListenerInterface $listener */
$listener = new $listenerClassName();

if ($processor instanceof Processor) {
    $processor->getEventManager()->on($listener);
}

Consequently, a listener such as:

final class WorkerListener implements EventListenerInterface
{
    public function __construct(
        private SomeService $service,
    ) {
    }
}

cannot be used as the configured worker listener, because it is instantiated without its required dependency.

The same problem occurs for jobs that are dynamically resolved from queue messages.

For example, a custom listener may determine the job class from the message and currently instantiate it with:

$job = new $class();
$job->{$handler}($message);

A job such as:

final class ImportUsersJob
{
    public function __construct(
        private ImportUsersService $importUsersService,
    ) {
    }
}

cannot therefore be instantiated using CakePHP's dependency injection container.

Expected behavior

Since the WorkerCommand already has access to the application DI container, I would expect configured worker listeners to be resolved through that container when one is available.

For example, instead of:

$listener = new $listenerClassName();

something along the lines of:

$listener = $this->container
    ? $this->container->get($listenerClassName)
    : new $listenerClassName();

would allow worker listeners to use constructor injection while preserving the existing behavior when no container is available.

Likewise, there should ideally be a supported way for jobs resolved from queue messages to be obtained from the same DI container, rather than requiring them to be instantiated manually with new.

This would allow applications to use normal CakePHP dependency injection throughout their queue worker infrastructure.

Why this matters

Queue jobs often need application services such as:

  • application/domain services
  • repositories
  • API clients
  • import/export services
  • other infrastructure services

Requiring these dependencies to be manually constructed or passed through the queue message defeats the purpose of having the application's DI container available to the worker.

It also means that a job cannot simply use normal constructor injection:

final class SomeJob
{
    public function __construct(
        private SomeService $service,
    ) {
    }
}

even though the same class could be resolved normally through CakePHP's container.

Possible implementation

I don't want to prescribe the exact implementation, but it seems that the existing $container available in WorkerCommand could be used to resolve configured listeners.

For example:

$listener = $this->container
    ? $this->container->get($listenerClassName)
    : new $listenerClassName();

For jobs, perhaps the queue infrastructure could expose a job resolver/factory backed by the same container, or otherwise make the container available to the component responsible for resolving job classes.

The important part from an application developer's perspective is that dynamically resolved listeners and jobs should be able to participate in CakePHP's normal dependency injection mechanism.

Additional context

The current behavior is particularly noticeable when implementing a custom worker listener that reacts to the queue lifecycle events:

Processor.message.seen
Processor.message.start
Processor.message.success
Processor.message.failure
Processor.message.invalid
Processor.message.reject
Processor.message.exception

A custom listener can currently be registered through the queue configuration, but because it is instantiated directly, it cannot have constructor dependencies.

This seems somewhat inconsistent with the fact that WorkerCommand already accepts and passes around the application's ContainerInterface.

Would it be possible to resolve configured worker listeners through the DI container, and preferably provide a DI-aware mechanism for resolving jobs as well?

主要语言
PHP
星标
36
派生
22
平均合并
20 小时 36 分钟
30 天内合并 PR
2

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

cakephp/queue 的其他 Issue

查看 cakephp/queue 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。