Skip to content

Commit c9af389

Browse files
authored
Merge pull request #283 from php-enqueue/enchence-processor-doc
[doc][skip ci] Add processor examples, notes on exception and more.
2 parents fb934bb + 157feae commit c9af389

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

Diff for: docs/consumption/message_processor.md

+44-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Message processor
22

3+
* [Basics](#basics)
4+
* [Reply result](#reply-result)
5+
* [On exceptions](#on-exceptions)
6+
* [Examples](#examples)
7+
8+
9+
## Basics
10+
311
The message processor is an object that actually process the message and must return a result status.
412
Here's example:
513

@@ -20,9 +28,15 @@ class SendMailProcessor implements PsrProcessor
2028
}
2129
```
2230

23-
Usually there is no need to catch exceptions.
24-
The message broker can detect consumer has failed and redeliver the message.
25-
Sometimes you have to reject messages explicitly.
31+
By returning `self::ACK` a processor tells a broker that the message has been processed correctly.
32+
33+
There are other statuses:
34+
35+
* `self::ACK` - Use this constant when the message is processed successfully and the message could be removed from the queue.
36+
* `self::REJECT` - Use this constant when the message is not valid or could not be processed. The message is removed from the queue.
37+
* `self::REQUEUE` - Use this constant when the message is not valid or could not be processed right now but we can try again later
38+
39+
Look at the next example that shows the message validation before sending a mail. If the message is not valid a processor rejects it.
2640

2741
```php
2842
<?php
@@ -95,7 +109,11 @@ class SendMailProcessor implements PsrProcessor
95109
}
96110
```
97111

98-
The consumption component provide some useful extensions, for example there is an extension that makes RPC processing simplier.
112+
## Reply result
113+
114+
The consumption component provide some useful extensions, for example there is an extension that makes RPC processing simpler.
115+
The producer might wait for a reply from a consumer and in order to send it a processor has to return a reply result.
116+
Dont forget to add `ReplyExtension`.
99117

100118
```php
101119
<?php
@@ -130,4 +148,26 @@ $queueConsumer->bind('foo', new SendMailProcessor());
130148
$queueConsumer->consume();
131149
```
132150

151+
152+
## On exceptions
153+
154+
It is advised to not catch exceptions and [fail fast](https://en.wikipedia.org/wiki/Fail-fast).
155+
Also consider using [supervisord](supervisord.org) or similar process manager to restart exited consumers.
156+
157+
Despite advising to fail there are some cases where you might want to catch exceptions.
158+
159+
* A message validator throws an exception on invalid message. It is better to catch it and return `REJECT`.
160+
* Some transports ([Doctrine DBAL](../transport/dbal.md), [Filesystem](../transport/filesystem.md), [Redis](../transport/redis.md)) does notice an error,
161+
and therefor won't be able to redeliver the message. The message is completely lost. You might want to catch an exception to properly redelivery\requeue the message.
162+
163+
# Examples
164+
165+
Feel free to contribute your own.
166+
167+
* [LiipImagineBundle. ResolveCacheProcessor](https://github.com/liip/LiipImagineBundle/blob/713e36f5df353d7c5345daed5a2eefc23c103849/Async/ResolveCacheProcessor.php#L1)
168+
* [EnqueueElasticaBundle. ElasticaPopulateProcessor](https://github.com/php-enqueue/enqueue-elastica-bundle/blob/7c05c55b1667f9cae98325257ba24fc101f87f97/Async/ElasticaPopulateProcessor.php#L1)
169+
* [formapro/pvm. HandleAsyncTransitionProcessor](https://github.com/formapro/pvm/blob/d5e989a77eb1540a93e69abacc446b3d7937292d/src/Enqueue/HandleAsyncTransitionProcessor.php#L1)
170+
* [php-quartz. EnqueueRemoteTransportProcessor](https://github.com/php-quartz/quartz-dev/blob/91690aa535b0322510b4555dab59d6ae9d7044e5/pkg/bridge/Enqueue/EnqueueRemoteTransportProcessor.php#L1)
171+
* [php-comrade. CreateJobProcessor](https://github.com/php-comrade/comrade-dev/blob/43c0662b74340aae318bceb15d8564670325dcee/apps/jm/src/Queue/CreateJobProcessor.php#L1)
172+
133173
[back to index](../index.md)

0 commit comments

Comments
 (0)