Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"require": {
"php": ">=8.2",
"cakephp/cakephp": "^5.4.0",
"enqueue/simple-client": "^0.10",
"psr/log": "^3.0",
"ramsey/uuid": "^4.7.0"
},
Expand Down Expand Up @@ -67,7 +66,7 @@
"phpstan": "tools/phpstan analyse",
"stan-baseline": "tools/phpstan --generate-baseline",
"stan-setup": "phive install",
"rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"~2.5.0\" && mv composer.backup composer.json",
"rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"~2.8.0\" && mv composer.backup composer.json",
"rector-check": "vendor/bin/rector process --dry-run",
"rector-fix": "vendor/bin/rector process",
"test": "phpunit",
Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/toc_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"items": [
{ "text": "Queueing Mail", "link": "/mail" },
{ "text": "Custom Processors", "link": "/processors" },
{ "text": "Custom Clients", "link": "/clients" },
{ "text": "Running Workers", "link": "/workers" }
]
},
Expand Down
70 changes: 70 additions & 0 deletions docs/en/clients.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Custom Clients

Queue connections use `Cake\Queue\Client\EnqueueClient` by default. A connection can use a custom client class by setting the `clientClass` option in the queue configuration. The class must extend `Cake\Queue\Client\EnqueueClient`.

```php
'Queue' => [
'default' => [
'url' => 'null:',
'queue' => 'default',
],
'audited' => [
'url' => 'redis://localhost:6379',
'queue' => 'audited',
'clientClass' => \App\Queue\AuditedClient::class,
],
],
```

If no client class is configured, the plugin uses `Cake\Queue\Client\EnqueueClient`.

## Configuration

`EnqueueClient` is built from the `url` value without any Symfony configuration processing. The `url` value can be a DSN string or an array with `transport`, `client` and `extensions` sections. Extra transport keys are passed to the connection factory. Use `connection_factory_class` inside the `transport` section to provide a custom `Interop\Queue\ConnectionFactory`.

## Produce Events

The client dispatches two events around every produce operation. Subscribe to these events on the client event manager to observe publishing.

- `Queue.produce.success` is dispatched after the transport accepts the message. Payload keys: `topic` or `command`, `messageId`, `transportDestination`. For commands the payload also includes `promise`.
- `Queue.produce.failure` is dispatched when the transport throws. Payload keys: `topic` or `command`, `exception`. The original exception is rethrown after dispatch.

Produce events carry identifiers and routing metadata only, never message bodies. The sender already holds the message object, while events are broadcast to every listener, so bodies are deliberately excluded. Event payloads are safe to log.

The `messageId` value is assigned by the transport during `sendEvent` or `sendCommand`. The client only observes it through the post send extension and never generates it. `QueueManager::push()` returns this value on success and `null` when a unique push is skipped as duplicate.

Direct calls on `$client->getProducer()->sendEvent()` or `sendCommand()` bypass these events. Only `EnqueueClient::sendEvent()`, `EnqueueClient::sendCommand()` and `QueueManager::push()` dispatch produce events.

When binding processors, always pass an explicit `$processorName`. Names generated with `uniqid()` are a fallback for tests and prototypes, not a naming scheme.

Custom client classes with a non-standard producer must assign a message id on the passed message before returning, otherwise `QueueManager::push()` reports `null` on success, indistinguishable from a push skipped as duplicate.

## Example

```php
<?php
declare(strict_types=1);

namespace App\Queue;

use Cake\Queue\Client\EnqueueClient;

class AuditedClient extends EnqueueClient
{
protected function createConnection(array $transport): \Interop\Queue\ConnectionFactory
{
return parent::createConnection($transport);
}
}
```

Attach a listener in application code:

```php
$engine = \Cake\Queue\QueueManager::engine('audited');
$engine->getEventManager()->on('Queue.produce.success', function ($event) {
$messageId = $event->getData('messageId');
});
```

The worker side keeps its own event set. See Worker Events for processing events.
7 changes: 7 additions & 0 deletions docs/en/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ Workers dispatch events during normal message processing. If your queue config d
- `Processor.message.failure`: dispatched when a message finishes with `Processor::REQUEUE`. Arguments: `message`.
- `Processor.message.seen`: dispatched when a message is first observed by the worker. Arguments: `message`.
- `Processor.message.start`: dispatched immediately before processing begins. Arguments: `message`.

## Produce Events

Producing is separate from processing. The queue client dispatches these events on its own event manager. See Custom Clients for setup.

- `Queue.produce.success`: dispatched after the transport accepts the message. Arguments: `topic` or `command`, `message`, `messageId`, `transportDestination`, `transportMessage`, and `promise` for commands.
- `Queue.produce.failure`: dispatched when the transport throws. Arguments: `topic` or `command`, `message`, `exception`.
1 change: 1 addition & 0 deletions docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Queue jobs are regular PHP classes. They can receive constructor dependencies fr
- [Running Workers](/workers) documents the worker command and its options.
- [Failed Jobs](/failed-jobs) covers storing, requeueing, and purging failed jobs.
- [Worker Events](/events) lists the events emitted while jobs are processed.
- [Custom Clients](/clients) explains how to replace the default queue client and observe produce events.
3 changes: 3 additions & 0 deletions docs/en/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ Define one or more queue connections in `config/app.php`:
// Optional processor class. Defaults to Cake\Queue\Queue\Processor.
'processor' => \App\Queue\CustomProcessor::class,

// Optional client class. Defaults to Cake\Queue\Client\EnqueueClient.
'clientClass' => \App\Queue\AuditedClient::class,

// Sleep time in milliseconds when no messages are available.
'receiveTimeout' => 10000,

Expand Down
Loading
Loading