phplaravelnotificationslaravel-11

Laravel bootcamp send notification fails


I am following the Laravel Bootcamp tutorial, but I am stuck at notifications & events, as the notification is not sent.

I have kept the default configuration in .env while, which is as follows:

MAIL_MAILER=log
MAIL_HOST=localhost
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"

If I understand correctly, this should just write the e-mail at storage/logs/laravel.log.

However, if I start the queue processing with php artisan queue:work, and I send a new chirp, I see the event getting triggered, but it fails after a minute and in the log I see

[2024-05-12 10:01:39] local.ERROR: App\Listeners\SendChirpCreatedNotifications has been attempted too many times. {"exception":"[object] (Illuminate\\Queue\\MaxAttemptsExceededException(code: 0): App\\Listeners\\SendChirpCreatedNotifications has been attempted too many times. at /root/chirper/vendor/laravel/framework/src/Illuminate/Queue/MaxAttemptsExceededException.php:24)
[stacktrace]
#0 /root/chirper/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(785): Illuminate\\Queue\\MaxAttemptsExceededException::forJob()
#1 /root/chirper/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(519): Illuminate\\Queue\\Worker->maxAttemptsExceededException()
#2 /root/chirper/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(428): Illuminate\\Queue\\Worker->markJobAsFailedIfAlreadyExceedsMaxAttempts()
#3 /root/chirper/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(389): Illuminate\\Queue\\Worker->process()
#4 /root/chirper/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(176): Illuminate\\Queue\\Worker->runJob()
#5 /root/chirper/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(139): Illuminate\\Queue\\Worker->daemon()
#6 /root/chirper/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(122): Illuminate\\Queue\\Console\\WorkCommand->runWorker()
#7 /root/chirper/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\\Queue\\Console\\WorkCommand->handle()
#8 /root/chirper/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#9 /root/chirper/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure()
#10 /root/chirper/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod()
#11 /root/chirper/vendor/laravel/framework/src/Illuminate/Container/Container.php(662): Illuminate\\Container\\BoundMethod::call()
#12 /root/chirper/vendor/laravel/framework/src/Illuminate/Console/Command.php(212): Illuminate\\Container\\Container->call()
#13 /root/chirper/vendor/symfony/console/Command/Command.php(279): Illuminate\\Console\\Command->execute()
#14 /root/chirper/vendor/laravel/framework/src/Illuminate/Console/Command.php(181): Symfony\\Component\\Console\\Command\\Command->run()
#15 /root/chirper/vendor/symfony/console/Application.php(1049): Illuminate\\Console\\Command->run()
#16 /root/chirper/vendor/symfony/console/Application.php(318): Symfony\\Component\\Console\\Application->doRunCommand()
#17 /root/chirper/vendor/symfony/console/Application.php(169): Symfony\\Component\\Console\\Application->doRun()
#18 /root/chirper/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(196): Symfony\\Component\\Console\\Application->run()
#19 /root/chirper/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1187): Illuminate\\Foundation\\Console\\Kernel->handle()
#20 /root/chirper/artisan(13): Illuminate\\Foundation\\Application->handleCommand()
#21 {main}
"}

This is what I see in the console:

$ php artisan queue:work

   INFO  Processing jobs from the [default] queue.

  2024-05-12 14:33:07 App\Listeners\SendChirpCreatedNotifications ................................................................................. RUNNING
  2024-05-12 14:34:10 App\Listeners\SendChirpCreatedNotifications .............................................................................. 1m 3s FAIL
Killed

I also tried other options, like mailpit or mailtrap, but still couldn't get it work; also, sometimes no error message would be logged in the laravel.log file.

The code was run from WSL using PHP 8.3


Solution

  • In the end I have been able to solve the problem by setting a higher timeout, by running

    php artisan queue:work --timeout=120
    

    By doing this, it started working properly, and indeed the first time required much more than a minute, which if I am not wrong is the default timeout

    INFO  Processing jobs from the [default] queue.
    
    2024-05-17 19:12:08 App\Listeners\SendChirpCreatedNotifications ................................................................................. RUNNING
    2024-05-17 19:13:54 App\Listeners\SendChirpCreatedNotifications ............................................................................. 1m 45s DONE
    2024-05-17 19:25:51 App\Listeners\SendChirpCreatedNotifications ................................................................................. RUNNING
    2024-05-17 19:26:22 App\Listeners\SendChirpCreatedNotifications ................................................................................ 31s DONE
    2024-05-17 19:27:16 App\Listeners\SendChirpCreatedNotifications ................................................................................. RUNNING
    2024-05-17 19:27:38 App\Listeners\SendChirpCreatedNotifications ................................................................................ 21s DONE
    2024-05-17 19:30:08 App\Listeners\SendChirpCreatedNotifications ................................................................................. RUNNING
    2024-05-17 19:30:32 App\Listeners\SendChirpCreatedNotifications ................................................................................ 23s DONE
    

    subsequent notifications required much less time to run, and even restarting the queue work didn't require so much time to handle the first notification.