Commit e788a49
committed
feature #39622 [Messenger] Be able to get raw data when a message in not decodable by the PHP Serializer (lyrixx)
This PR was merged into the 6.2 branch.
Discussion
----------
[Messenger] Be able to get raw data when a message in not decodable by the PHP Serializer
| Q | A
| ------------- | ---
| Branch? | 6.2
| Bug fix? | yes
| New feature? | yes
| Deprecations? | no
| Tickets |
| License | MIT
| Doc PR |
---
It's easier to review the pr with [`?w=1`](https://github.com/symfony/symfony/pull/39622/files?w=1)
---
## Why?
This PR aimed to handle properly messaged that are already failed and also **not decodable** anymore.
This use case occurs when a Message class is renamed / moved to another namespace
## Reproducer
1. Publish many message of type `App\Message\Foobar`
1. Consume them all, but throw an Exception exception in the handler
1. Renamed `Foobar` to `Foo`
## Before / After
### command `messenger:failed:show`
#### Before
##### Without an ID
```
…abs/symfony/messenger-class-not-found(new) bin/console messenger:failed:show
There are 94 messages pending in the failure transport.
In PhpSerializer.php line 84:
Message class "App\Message\Foobar" not found during decoding.
messenger:failed:show [--max MAX] [--transport [TRANSPORT]] [--stats] [--class-filter CLASS-FILTER] [--] [<id>]
```
And this **message is lost**
##### With an ID
```
>…abs/symfony/messenger-class-not-found(new) bin/console messenger:failed:show 68
There are 93 messages pending in the failure transport.
In PhpSerializer.php line 84:
Message class "App\Message\Foobar" not found during decoding.
messenger:failed:show [--max MAX] [--transport [TRANSPORT]] [--stats] [--class-filter CLASS-FILTER] [--] [<id>]
```
**Message lost too**
#### After
##### Without an ID
```
>…goire/dev/labs/symfony/symfony-5.2(remaned) bin/console messenger:failed:show
There are 94 messages pending in the failure transport.
----- ------------------------ --------------------- ---------------------------------------
Id Class Failed at Error
----- ------------------------ --------------------- ---------------------------------------
62 __PHP_Incomplete_Class 2022-08-12 16:26:06 FoobarHandler is not implemented yet.
63 __PHP_Incomplete_Class 2022-08-12 16:26:06 FoobarHandler is not implemented yet.
64 __PHP_Incomplete_Class 2022-08-12 16:26:06 FoobarHandler is not implemented yet.
// Showing first 50 messages.
// Run messenger:failed:show {id} --transport=failed -vv to see message details.
```
**No messages are lost**
##### With an ID
```
>…abs/symfony/messenger-class-not-found(new) bin/console messenger:failed:show 69
There are 92 messages pending in the failure transport.
Failed Message Details
======================
------------- ---------------------------------------
Class __PHP_Incomplete_Class
Message Id 69
Failed at 2022-08-12 16:26:06
Error FoobarHandler is not implemented yet.
Error Code 0
Error Class Exception
Transport async
------------- ---------------------------------------
Message history:
* Message failed at 2022-08-12 16:25:58 and was redelivered
* Message failed at 2022-08-12 16:25:59 and was redelivered
* Message failed at 2022-08-12 16:26:02 and was redelivered
* Message failed at 2022-08-12 16:26:06 and was redelivered
[ERROR] The message could not be decoded.
Re-run command with -vv to see more message & error details.
Run messenger:failed:retry 69 --transport=failed to retry this message.
Run messenger:failed:remove 69 --transport=failed to delete it.
```
**messages not lost**
##### With an ID an -vv
```
>…abs/symfony/messenger-class-not-found(new) bin/console messenger:failed:show 69 -vv
There are 92 messages pending in the failure transport.
Failed Message Details
======================
------------- ---------------------------------------
Class __PHP_Incomplete_Class
Message Id 69
Failed at 2022-08-12 16:26:06
Error FoobarHandler is not implemented yet.
Error Code 0
Error Class Exception
Transport async
------------- ---------------------------------------
Message history:
* Message failed at 2022-08-12 16:25:58 and was redelivered
* Message failed at 2022-08-12 16:25:59 and was redelivered
* Message failed at 2022-08-12 16:26:02 and was redelivered
* Message failed at 2022-08-12 16:26:06 and was redelivered
Message:
========
[ERROR] The message could not be decoded. See below an APPROXIMATIVE representation of the class.
__PHP_Incomplete_Class(App\Message\Foobar) {}
Exception:
==========
Exception^ {
message: "FoobarHandler is not implemented yet."
code: 0
file: "/home/gregoire/dev/labs/symfony/messenger-class-not-found/src/MessageHandler/FoobarHandler.php"
line: 12
trace: {
/home/gregoire/dev/labs/symfony/messenger-class-not-found/src/MessageHandler/FoobarHandler.php:12
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Messenger/Middleware/HandleMessageMiddleware.php:95 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Messenger/Middleware/SendMessageMiddleware.php:71 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Messenger/Middleware/FailedMessageProcessingMiddleware.php:34 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Messenger/Middleware/DispatchAfterCurrentBusMiddleware.php:68 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Messenger/Middleware/RejectRedeliveredMessageMiddleware.php:41 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Messenger/Middleware/AddBusNameStampMiddleware.php:37 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Messenger/Middleware/TraceableMiddleware.php:43 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Messenger/MessageBus.php:73 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Messenger/TraceableMessageBus.php:41 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Messenger/RoutableMessageBus.php:54 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Messenger/Worker.php:156 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Messenger/Worker.php:105 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Messenger/Command/ConsumeMessagesCommand.php:223 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Console/Command/Command.php:309 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Console/Application.php:1019 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:94 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Console/Application.php:300 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:80 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Console/Application.php:172 { …}
/home/gregoire/dev/github.com/lyrixx/symfony/src/Symfony/Component/Runtime/Runner/Symfony/ConsoleApplicationRunner.php:54 { …}
./vendor/autoload_runtime.php:29 { …}
./bin/console:11 { …}
}
}
Run messenger:failed:retry 69 --transport=failed to retry this message.
Run messenger:failed:remove 69 --transport=failed to delete it.
```
<details>
<summary>screenshot with colors</summary>

</details>
**messages not lost**
### command `messenger:failed:retry`
#### Before
```
>…abs/symfony/messenger-class-not-found(new) bin/console messenger:failed:retry 69
// Quit this command with CONTROL-C.
// Re-run the command with a -vv option to see logs about consumed messages.
There are 92 messages pending in the failure transport.
To retry all the messages, run messenger:consume failed
In PhpSerializer.php line 84:
Message class "App\Message\Foobar" not found during decoding.
messenger:failed:retry [--force] [--transport [TRANSPORT]] [--] [<id>...]
```
**And the first message is lost**
Same if I use an ID
#### After
##### Without ID
```
>…abs/symfony/messenger-class-not-found(new) bin/console messenger:failed:retry
// Quit this command with CONTROL-C.
// Re-run the command with a -vv option to see logs about consumed messages.
There are 91 messages pending in the failure transport.
To retry all the messages, run messenger:consume failed
Failed Message Details
======================
------------- ---------------------------------------
Class __PHP_Incomplete_Class
Message Id 63
Failed at 2022-08-12 16:26:06
Error FoobarHandler is not implemented yet.
Error Code 0
Error Class Exception
Transport async
------------- ---------------------------------------
Message history:
* Message failed at 2022-08-12 16:25:58 and was redelivered
* Message failed at 2022-08-12 16:25:59 and was redelivered
* Message failed at 2022-08-12 16:26:02 and was redelivered
* Message failed at 2022-08-12 16:26:06 and was redelivered
[ERROR] The message could not be decoded.
Re-run command with -vv to see more message & error details.
In FailedMessagesRetryCommand.php line 176:
The message with id "63" could not decoded, it can only be shown or removed.
messenger:failed:retry [--force] [--transport [TRANSPORT]] [--] [<id>...]
```
**And the message is not lost**
##### With an ID
```
>…abs/symfony/messenger-class-not-found(new) bin/console messenger:failed:retry 63
// Quit this command with CONTROL-C.
// Re-run the command with a -vv option to see logs about consumed messages.
There are 91 messages pending in the failure transport.
To retry all the messages, run messenger:consume failed
Failed Message Details
======================
------------- ---------------------------------------
Class __PHP_Incomplete_Class
Message Id 63
Failed at 2022-08-12 16:26:06
Error FoobarHandler is not implemented yet.
Error Code 0
Error Class Exception
Transport async
------------- ---------------------------------------
Message history:
* Message failed at 2022-08-12 16:25:58 and was redelivered
* Message failed at 2022-08-12 16:25:59 and was redelivered
* Message failed at 2022-08-12 16:26:02 and was redelivered
* Message failed at 2022-08-12 16:26:06 and was redelivered
[ERROR] The message could not be decoded.
Re-run command with -vv to see more message & error details.
In FailedMessagesRetryCommand.php line 176:
The message with id "63" could not decoded, it can only be shown or removed.
messenger:failed:retry [--force] [--transport [TRANSPORT]] [--] [<id>...]
```
### command `messenger:failed:remove`
#### Before
```
>…abs/symfony/messenger-class-not-found(new) bin/console messenger:failed:remove 63
In PhpSerializer.php line 84:
Message class "App\Message\Foobar" not found during decoding.
messenger:failed:remove [--force] [--transport [TRANSPORT]] [--show-messages] [--] <id>...
```
The message is lost, but it's not an issue I guess :p
#### After
```
>…abs/symfony/messenger-class-not-found(new) bin/console messenger:failed:remove 69
[ERROR] The message with id "69" was not found.
>…abs/symfony/messenger-class-not-found(new) bin/console messenger:failed:remove 79
Failed Message Details
======================
------------- ---------------------------------------
Class __PHP_Incomplete_Class
Message Id 79
Failed at 2022-08-12 16:26:06
Error FoobarHandler is not implemented yet.
Error Code 0
Error Class Exception
Transport async
------------- ---------------------------------------
Message history:
* Message failed at 2022-08-12 16:25:58 and was redelivered
* Message failed at 2022-08-12 16:25:59 and was redelivered
* Message failed at 2022-08-12 16:26:02 and was redelivered
* Message failed at 2022-08-12 16:26:06 and was redelivered
[ERROR] The message could not be decoded.
Re-run command with -vv to see more message & error details.
Do you want to permanently remove this message? (yes/no) [no]:
> yes
[OK] Message with id 79 removed.
```
---
## Extra
### `messenger:consume` command
This command is not fixed on purpose: We keep the default behavior to keep a BC.
**BUT** I would like to use the same stamp system I introduce to avoid loosing a message.
Because ATM, when a DecodeException is thrown, the message is lost. Not cool!
```
>…goire/dev/labs/symfony/symfony-5.2(remaned) bin/console messenger:consume failed
[OK] Consuming messages from transports "failed".
// The worker will automatically exit once it has received a stop signal via the messenger:stop-workers command.
// Quit the worker with CONTROL-C.
// Re-run the command with a -vv option to see logs about consumed messages.
In PhpSerializer.php line 92:
Message class "App\Message\Foobar" not found during decoding.
```
Commits
-------
34229af [Messenger] Be able to get raw data when a message in not decodable by the PHP SerializerFile tree
9 files changed
+225
-56
lines changed- src/Symfony
- Bundle/FrameworkBundle/Resources/config
- Component/Messenger
- Command
- Stamp
- Tests/Transport/Serialization
- Transport/Serialization
9 files changed
+225
-56
lines changedLines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
191 | 191 | | |
192 | 192 | | |
193 | 193 | | |
| 194 | + | |
194 | 195 | | |
195 | 196 | | |
196 | 197 | | |
197 | 198 | | |
198 | 199 | | |
199 | 200 | | |
200 | 201 | | |
| 202 | + | |
201 | 203 | | |
202 | 204 | | |
203 | 205 | | |
204 | 206 | | |
205 | 207 | | |
206 | 208 | | |
207 | 209 | | |
| 210 | + | |
208 | 211 | | |
209 | 212 | | |
210 | 213 | | |
| |||
Lines changed: 13 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| 31 | + | |
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
| |||
44 | 46 | | |
45 | 47 | | |
46 | 48 | | |
| 49 | + | |
47 | 50 | | |
48 | 51 | | |
49 | 52 | | |
50 | | - | |
| 53 | + | |
51 | 54 | | |
52 | 55 | | |
53 | 56 | | |
| 57 | + | |
54 | 58 | | |
55 | 59 | | |
56 | 60 | | |
| |||
78 | 82 | | |
79 | 83 | | |
80 | 84 | | |
| 85 | + | |
| 86 | + | |
81 | 87 | | |
82 | 88 | | |
83 | 89 | | |
| |||
126 | 132 | | |
127 | 133 | | |
128 | 134 | | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
129 | 138 | | |
130 | 139 | | |
131 | 140 | | |
132 | 141 | | |
133 | 142 | | |
134 | 143 | | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
135 | 147 | | |
136 | 148 | | |
137 | 149 | | |
| |||
Lines changed: 7 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
78 | 84 | | |
79 | 85 | | |
80 | 86 | | |
| |||
Lines changed: 34 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
27 | 26 | | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
44 | | - | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
50 | | - | |
| 51 | + | |
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
| |||
133 | 134 | | |
134 | 135 | | |
135 | 136 | | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
143 | 143 | | |
144 | | - | |
| 144 | + | |
| 145 | + | |
145 | 146 | | |
146 | 147 | | |
147 | 148 | | |
148 | | - | |
| 149 | + | |
149 | 150 | | |
150 | 151 | | |
151 | 152 | | |
152 | | - | |
| 153 | + | |
153 | 154 | | |
154 | 155 | | |
155 | 156 | | |
| |||
171 | 172 | | |
172 | 173 | | |
173 | 174 | | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
174 | 179 | | |
175 | 180 | | |
176 | 181 | | |
| |||
207 | 212 | | |
208 | 213 | | |
209 | 214 | | |
210 | | - | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
211 | 221 | | |
212 | 222 | | |
213 | 223 | | |
| |||
216 | 226 | | |
217 | 227 | | |
218 | 228 | | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
219 | 239 | | |
Lines changed: 39 additions & 29 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
114 | 119 | | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
| 120 | + | |
| 121 | + | |
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
| |||
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
151 | | - | |
152 | | - | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
153 | 155 | | |
154 | | - | |
155 | | - | |
156 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
157 | 159 | | |
158 | | - | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
159 | 164 | | |
160 | 165 | | |
161 | 166 | | |
| |||
171 | 176 | | |
172 | 177 | | |
173 | 178 | | |
174 | | - | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
175 | 185 | | |
176 | 186 | | |
177 | 187 | | |
| |||
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
Lines changed: 12 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
| 61 | + | |
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
| 82 | + | |
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
| 94 | + | |
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
102 | 107 | | |
103 | 108 | | |
104 | 109 | | |
| |||
0 commit comments