how to stop queue job after finish its execution automatically in Laravel?

Published

I use queue job after (a user) registered, it sent an email for verifying, I set a delay (10 second) to run the job.

but the issue is that:

  • the queue job runs and executes the verifying email fine, but it still running in the background forever, and this consumes the resources:

enter image description here

how to stop it automatically after the execution of the job?

Route:

    Route::group(['middleware'=>'guest:web'], function(){
    Route::get('/register', [registerController::class,'register'])->name('site.register');
    Route::match(['get','post'],'/register-create', [registerController::class,'create'])->name('site.register.create');
});

Controller:

    public function create(RegisterRequest $request)
{
     $user = User::create([
        'firstName' => $request->firstName,
        'middleName' => $request->middleName,
        'lastName' => $request->lastName,
        'email' => $request->email,
        'password' => Hash::make($request->password),
    ]);
    $on = CarbonCarbon::now()->addSecond(10);
     dispatch(new VerifyEmailJob($user))->delay($on);
    return redirect()->route('landingPage')->with(['success'=>'We sent verifying email check it']);
}

queue job:

    <?php

namespace AppJobs;

use AppMailVerifyEmail;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use IlluminateQueueInteractsWithQueue;
use IlluminateQueueSerializesModels;
use IlluminateSupportFacadesMail;

class VerifyEmailJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public $user;
    public function __construct($user)
    {
        $this->user= $user;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Mail::to($this->user->email)->send(new VerifyEmail($this->user));

    }
}

Mail class:

    <?php

namespace AppMail;

use IlluminateBusQueueable;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;

class VerifyEmail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public $user;

    public function __construct($user)
    {
        $this->user = $user;
    }
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $user = $this->user;
        return $this->subject('Mail from Oneme')
            ->view('site.auth.verifyEmail',compact('user'));
    }
}

Any Help please

Source: Laravel

Published
Categorised as laravel, php, queue Tagged , ,

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq