Laravel ShouldQueue How It Works

I know how to use ShouldQueue , my question is why it works the way it does.

I need to edit how my new Job is stored in the database, and so I dig the insides of Laravel.

The job I want to change starts from the following event listener:

 <?php namespace App\Listeners; use App\Events\NewMail; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use App\Jobs\SendEmail; use Carbon\Carbon; class NewMailListener implements ShouldQueue { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Handle the event. * * @param NewMail $event * @return void */ public function handle(NewMail $event) { $addressee = $event->user->name; $address = $event->user->email; $type = "NewMail"; $job = (new SendEmail($type,$addressee,$address))->delay(Carbon::now()->addMinutes(10)); dispatch($job); } } 

I don’t understand how ShouldQueue magic ShouldQueue , because it does nothing in the source code .

 <?php namespace Illuminate\Contracts\Queue; interface ShouldQueue { // } 

I understand that this is a contract , but it does not define anything ... so what does it do exactly? Is there any kind of automatic loading from the namespace?

I was not sure what the interface was, so I looked at it: PHP Docs: Interfaces and left with the impression that even if it is intended for decoupling, and the interface should define something that I do not see in ShouldQueue .

The top comment on this page of PHP docs says the following:

INTERFACE is provided so that you can describe the set of functions and then hide the final implementation of these functions in the implementing class. This allows you to change the IMPLEMENTATION of these functions without changing how you use it.

But where is this function description here?

PS - I know that this interface / contract is used for the queue of the event listener itself, and not for the job that I want to change. But I hope that understanding how the queue interacts with the event listener will better tell me how it works for tasks.

+5
source share
1 answer

Internally, Laravel checks to see if it supports Job, Mailable or Notification, etc. interface ShouldQueue . For instance:

 if ($job instanceof ShouldQueue) { 

https://github.com/laravel/framework/blob/5.5/src/Illuminate/Console/Scheduling/Schedule.php#L86

+4
source

Source: https://habr.com/ru/post/1274852/


All Articles