bschmitt / laravel-amqp

AMQP wrapper for Laravel and Lumen to publish and consume messages
MIT License
268 stars 86 forks source link

What happen if I do not acknowledge? #122

Open realtebo opened 1 week ago

realtebo commented 1 week ago

I am using your project to implement a queue-supported mail sending system


           $amqp->consume('communication-sent', function ($message, $resolver) use ($mailController) {

                $messageDecoded = json_decode($message->body);

                $communication = $messageDecoded->communication;
                $emailAddress = $messageDecoded->email;

                print('Sending mail to ' . $emailAddress . " [communication]\n");
                try {
                    $mailController->send($emailAddress, new CommunicationSent($communication));
                    print('Sending successful\n');
                    sleep(10);
                }catch (\Exception $e){
                    print("Error: ".$e->getMessage());
                }

                $resolver->acknowledge($message);
                $resolver->stopWhenProcessed();
            });

i would like that the mail to send, in the queue, wil NOT BE lost if sending mail crashes for some reason.

Could I simply move $resolver->acknowledge($message); into the try branch? If I do this, and the code skip to catch branch, will the message in the queue be lost or is this available again and again untile someone reading it will also aknowledge?

bschmitt commented 1 week ago

Yes, only acknowledge when e-mail sending was successful. Please also check how your queue is configured.

realtebo commented 1 week ago

Should I move only

$resolver->acknowledge($message);
$resolver->stopWhenProcessed();

or both ?

$resolver->acknowledge($message);
$resolver->stopWhenProcessed();

What's the difference between the 2?