3

Iorder to send mails etc, we use messages in our project. If a message fails (cause of an error in the code of the message handler) it'll be stored in messenger_messages table.

The messenger_messages table contains a column called body. This is a part of the body column (it contains much more but it's really big). This body is encoded by Symfony/PHP (or RabbitMQ bundle, not sure which exactly).

O:36:\"Symfony\\Component\\Messenger\\Envelope\":2:{s:44:\"\0Symfony\\Component\\Messenger\\Envelope\0stamps\";a:4:{s:46:\"Symfony\\Component\\Messenger\\Stamp\\BusNameStamp\";a:1:{i:0;O:46:\"Symfony\\Component\\Messenger\\Stamp\\BusNameStamp\":1:{s:55:\"\0Symfony\\Component\\Messenger\\Stamp\\BusNameStamp\0busName\";s:21:\"messenger.bus.default\";}}s:44:\"Symfony\\Component\\Messenger\\Stamp\\DelayStamp\";a:4:{i:0;O:44:\"Symfony\\Component\\Messenger\\Stamp\\DelayStamp\":1:{s:51:\"\0Symfony\\Component\\Messenger\\Stamp\\DelayStamp\0delay\";i:5000;}i:1;O:44:\"Symfony\\Component\\Messenger\\Stamp\\DelayStamp\":1:{s:51:\"\0Symfony\\Component\\Messenger\\Stamp\\DelayStamp\0delay\";i:10000;}i:2;O:44:\"Symfony\\Component\\Messenger\\Stamp\\DelayStamp\":1:{s:51:\"\0Symfony\\Component\\Messenger\\Stamp\\DelayStamp\0delay\";i:20000;}i:3;O:44:\"Symfony\\Component\\Messenger\\Stamp\\DelayStamp\":1:{s:51:\"\0Symfony\\Component\\Messenger\\Stamp\\DelayStamp\0delay\";i:0;}}s:49:\"Symfony\\Component\\Messenger\\Stamp\\RedeliveryStamp\";a:4:{i:0;O:49:\"Symfony\\Component\\Messenger\\Stamp\\RedeliveryStamp\":4:{s:61:\"\0Symfony\\Component\\Messenger\\Stamp\\RedeliveryStamp\0retryCount\";i:1;s:64:\"\0Symfony\\Component\\Messenger\\Stamp\\RedeliveryStamp\0redeliveredAt\";O:17:\"DateTimeImmutable\":3:{s:4:\"date\";s:26:\"2021-09-16 11:20:58.645398\";s:13:\"timezone_type\";i:3;s:8:\"timezone\";s:16:\"Europe/Amsterdam\";}s:67:\"\0Symfony\\Component\\Messenger\\Stamp\\RedeliveryStamp\0exceptionMessage\";N;s:67:\"\0Symfony\\Component\\Messenger\\Stamp\\RedeliveryStamp\0flattenException\";N;}

How can I decode/deserialize this body? I tried using unserialize: (keep in mind this is just to test, this won't be the final code0

dump(unserialize($conn->executeQuery('SELECT * FROM messenger_messages')->fetchAllAssociative()[0]['body']));

but that doesn't work and gives this error for example:

Notice: unserialize(): Error at offset 0 of 27377 bytes
Joshua Bakker
  • 2,288
  • 3
  • 30
  • 63
  • You are trying to do this on the same project where you you originally dispatched the messages? – yivi Sep 16 '21 at 11:50
  • Yes, it's one Symfony project – Joshua Bakker Sep 16 '21 at 12:05
  • 1
    The same Symfony project? Where are calling `dump()`? A console command? A controller? Why `$conn`? You are not using doctrine? – yivi Sep 16 '21 at 12:07
  • I am using doctrine, however messenger_messages is a table created by RabbitMQ - and not with migrations and make:entity - on fail. Dump is called in a controller just as test. – Joshua Bakker Sep 16 '21 at 12:59
  • `unserialize` should be able to deal with that, if all things are as you say. It just needs the classes that were serialized to _exist_. So it's the same project, it should work. Something else, not in evidence here, must be amiss. [Example](https://3v4l.org/NtRYe#v8.0.10). – yivi Sep 16 '21 at 13:04
  • Oh maybe I should explain further, RabbitMQ encodes the message and puts it in the database (maybe this is what you meant). It's like a bundle that does it. The bundle and the messages are in the same project as I wanna decode in though. – Joshua Bakker Sep 16 '21 at 13:11
  • No, Rabbit does not encode the message nor puts it in the database. RabbitMQ wouldn't know how to do any of those things. That's done by PHP/Symfony. Sorry, can't help you further. As I said, what you have **should** work if everything as explained in the question. – yivi Sep 16 '21 at 13:17
  • You need to look into this tutorial https://symfonycasts.com/screencast/messenger, also you are using incorrectly unserilize https://symfony.com/doc/current/messenger.html#serializing-messages, the next question is about why you need to read that table? or what is the purpose? – Ricardo Jesus Ruiz Sep 17 '21 at 02:58

0 Answers0