You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+71-36Lines changed: 71 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,76 +42,111 @@ See the [adapter list](docs/guide/en/adapter-list.md) and follow the adapter-spe
42
42
> In this mode messages are processed immediately in the same process, so it won't provide true
43
43
> async execution, but the code stays the same when you switch to a real adapter.
44
44
45
-
### 2. Configure the queue
45
+
### 2. Prepare a message and handler
46
46
47
-
#### Configuration with [yiisoft/config](https://github.com/yiisoft/config)
48
-
49
-
**If you use [yiisoft/app](https://github.com/yiisoft/app) or [yiisoft/app-api](https://github.com/yiisoft/app-api)**
50
-
51
-
Add queue configuration to your application `$params` config. In [yiisoft/app](https://github.com/yiisoft/app)/[yiisoft/app-api](https://github.com/yiisoft/app-api) templates it's typically the `config/params.php` file.
52
-
_If your project structure differs, put it into any params config file that is loaded by [yiisoft/config](https://github.com/yiisoft/config)._
53
-
54
-
Minimal configuration example:
47
+
Define a message class for the work to be done — a simple value object with typed properties:
55
48
56
49
```php
57
-
return [
58
-
'yiisoft/queue' => [
59
-
'handlers' => [
60
-
'message-type' => [FooHandler::class, 'handle'],
61
-
],
62
-
],
63
-
];
64
-
```
50
+
use Yiisoft\Queue\Message\Message;
65
51
66
-
[Advanced configuration with `yiisoft/config`](docs/guide/en/configuration-with-config.md)
52
+
final class DownloadFileMessage extends Message
53
+
{
54
+
public const TYPE = 'download-file';
67
55
68
-
#### Manual configuration
56
+
public function __construct(
57
+
public readonly string $url,
58
+
public readonly string $destinationPath,
59
+
) {}
69
60
70
-
For setting up all classes manually, see the [Manual configuration](docs/guide/en/configuration-manual.md) guide.
61
+
public static function fromData(string $type, mixed $data): static
62
+
{
63
+
if ($type !== self::TYPE) {
64
+
throw new \InvalidArgumentException("Expected type \"" . self::TYPE . "\", got \"$type\".");
65
+
}
66
+
if (!is_array($data)
67
+
|| !is_string($data['url'] ?? null)
68
+
|| !is_string($data['destinationPath'] ?? null)
69
+
) {
70
+
throw new \InvalidArgumentException('Invalid data for ' . self::class . '.');
71
+
}
72
+
return new self($data['url'], $data['destinationPath']);
73
+
}
71
74
72
-
### 3. Prepare a handler
75
+
public function getType(): string
76
+
{
77
+
return self::TYPE;
78
+
}
73
79
74
-
You need to create a handler class that will process the queue messages. The most simple way is to implement the `MessageHandlerInterface`. Let's create an example for remote file processing:
#### Configuration with [yiisoft/config](https://github.com/yiisoft/config)
112
+
113
+
**If you use [yiisoft/app](https://github.com/yiisoft/app) or [yiisoft/app-api](https://github.com/yiisoft/app-api)**
114
+
115
+
Add queue configuration to your application `$params` config. In [yiisoft/app](https://github.com/yiisoft/app)/[yiisoft/app-api](https://github.com/yiisoft/app-api) templates it's typically the `config/params.php` file.
116
+
_If your project structure differs, put it into any params config file that is loaded by [yiisoft/config](https://github.com/yiisoft/config)._
99
117
100
-
To send a message to the queue, you need to get the queue instance and call the `push()` method. Typically, with Yii Framework you'll get a `Queue` instance as a dependency of a service.
Copy file name to clipboardExpand all lines: docs/guide/en/message-handler-advanced.md
+42-2Lines changed: 42 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,12 +14,52 @@ Handler definitions are configured in:
14
14
15
15
### Handlers mapped by short message type
16
16
17
-
Use a short stable message type when pushing a `Message` instead of a PHP class name:
17
+
Use a short stable message type instead of a PHP class name. That decoupling would allow you to refactor the code and handle the message with external handler.
18
+
Define a dedicated message class where `getType()` returns that type:
18
19
19
20
```php
20
21
use Yiisoft\Queue\Message\Message;
21
22
22
-
new Message('send-email', ['data' => '...']); // "send-email" is the message type here
23
+
final class SendEmailMessage extends Message
24
+
{
25
+
public const TYPE = 'send-email';
26
+
27
+
public function __construct(
28
+
public readonly string $to,
29
+
public readonly string $subject,
30
+
public readonly string $body,
31
+
) {}
32
+
33
+
public static function fromData(string $type, mixed $data): static
34
+
{
35
+
if ($type !== self::TYPE) {
36
+
throw new \InvalidArgumentException("Expected type \"" . self::TYPE . "\", got \"$type\".");
37
+
}
38
+
if (!is_array($data)
39
+
|| !is_string($data['to'] ?? null)
40
+
|| !is_string($data['subject'] ?? null)
41
+
|| !is_string($data['body'] ?? null)
42
+
) {
43
+
throw new \InvalidArgumentException('Invalid data for ' . self::class . '.');
44
+
}
45
+
return new self($data['to'], $data['subject'], $data['body']);
The producer only needs to know the message type and its data. It does not need to know anything about how the message will be processed, or even in which application.
@@ -30,21 +37,62 @@ This means the producer and consumer can be:
30
37
31
38
## Message: payload only
32
39
33
-
A message carries just enough data to perform the work:
40
+
A message carries just enough data to perform the work. Usually data has some parameters but not the full context to process. Getting full context is better to be moved to the handler unless processing is done in another application that doesn't have access to data storage.
41
+
Defining a dedicated class for each message type makes your code
42
+
self-documenting and type-safe:
43
+
44
+
```php
45
+
use Yiisoft\Queue\Message\Message;
46
+
47
+
final class SendEmailMessage extends Message
48
+
{
49
+
public const TYPE = 'send-email';
50
+
51
+
public function __construct(
52
+
public readonly string $to,
53
+
public readonly string $subject,
54
+
public readonly string $body,
55
+
) {}
56
+
57
+
public static function fromData(string $type, mixed $data): static
58
+
{
59
+
if ($type !== self::TYPE) {
60
+
throw new \InvalidArgumentException("Expected type \"" . self::TYPE . "\", got \"$type\".");
61
+
}
62
+
if (!is_array($data)
63
+
|| !is_string($data['to'] ?? null)
64
+
|| !is_string($data['subject'] ?? null)
65
+
|| !is_string($data['body'] ?? null)
66
+
) {
67
+
throw new \InvalidArgumentException('Invalid data for ' . self::class . '.');
68
+
}
69
+
return new self($data['to'], $data['subject'], $data['body']);
0 commit comments