Understand Laravel job traits

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

    /**
     * The podcast instance.
     *
     * @var \App\Models\Podcast
     */
    protected $podcast;

    /**
     * Create a new job instance.
     *
     * @param  App\Models\Podcast  $podcast
     * @return void
     */
    public function __construct(Podcast $podcast)
    {
        $this->podcast = $podcast;
    }

    /**
     * Execute the job.
     *
     * @param  App\Services\AudioProcessor  $processor
     * @return void
     */
    public function handle(AudioProcessor $processor)
    {
        // Process uploaded podcast...
    }
}

The ShouldQueue interface

This itself is pretty much self-explanatory. When the class implements this interface, the framework will automatically push the Job to the queue, instead of executing it in the current application lifecycle where it was dispatched.

https://github.com/laravel/framework/blob/8.x/src/Illuminate/Bus/Dispatcher.php#L76
    /**
     * Dispatch a command to its appropriate handler.
     *
     * @param  mixed  $command
     * @return mixed
     */
    public function dispatch($command)
    {
        return $this->queueResolver && $this->commandShouldBeQueued($command)
                        ? $this->dispatchToQueue($command)
                        : $this->dispatchNow($command);
    }
    /**
     * Determine if the given command should be queued.
     *
     * @param  mixed  $command
     * @return bool
     */
    protected function commandShouldBeQueued($command)
    {
        return $command instanceof ShouldQueue;
    }

The “Dispatchable” trait

This trait is just a helper to allow dispatching your Job without needing to instance it.

The “InteractsWithQueue” trait

This one is also optional, but also quite useful. This provides methods to let the job interact with itself inside a worker process.

For example, in the handle() method of the job you can check how many attempts have been done to process itself, release it and run it later, mark itself as failed or even delete itself from the queue.

The Queueable trait

This trait, which is also optional, allows you to set the connection, queue and delayment of the job.

The SerializedModels trait

This one is magic, and probably the main culprit why your jobs fail when you pass a deleted model to the job that will be processed later.

Original link https://darkghosthunter.medium.com/laravel-what-does-every-trait-in-a-job-class-6039707b851b

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Up ↑

%d bloggers like this: