Search code examples
phplaravelsupervisordlaravel-queuelaravel-events

Laravel Supervisor - SMTP issue stopping the code even email is sending through queue


I have supervisor installed to run the code using Laravel queue in background

I am using this things to send an email usually and using SMTP to send an email

But whenever there is any issue with SMTP connection, my code get stopped and firing 500 error even email sending is done through Laravel queue & Supervisor

How i call an event

event(new ClientRequestSubmitEvent($param1, $param2, $param3);

How ClientRequestSubmitEvent.php looks like

<?php

namespace App\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ClientRequestSubmitEvent implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithSockets;
    use SerializesModels;

    public $userId;
    public $clientId;
    public $requestData;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($userId, $clientId, $requestData)
    {
        $this->userId    = $userId;
        $this->clientId  = $clientId;
        $this->requestData = $requestData;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

How Listener ClientRequestSubmitListener.php looks like

<?php

namespace App\Listeners;

use App\Events\ClientRequestSubmitEvent;
use App\Mail\ClientRequestSubmitMail;
use App\Models\Client;
use App\Models\ClientHasResource;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;

class ClientRequestSubmitListener implements ShouldQueue
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
    }

    /**
     * Handle the event.
     *
     * @return void
     */
    public function handle(ClientRequestSubmitEvent $event)
    {
        
    Mail::to('[email protected]')->send(new ClientRequestSubmitMail($event));
    }
}

Queue settings in .env is like below

Can anyone help.

Thank You


Solution

  • To make it run on background the Queue connection should be other than sync, eg: database, 'redisetc; in.env` file

    QUEUE_CONNECTION=database