master

laravel/framework

Last updated at: 29/12/2023 09:24

InteractsWithQueue.php

TLDR

The InteractsWithQueue.php file is part of the Illuminate\Queue namespace and contains a trait called InteractsWithQueue. This trait provides methods for interacting with a queue job, such as retrieving the number of attempts, deleting the job, failing the job, releasing the job back into the queue, and setting the base queue job instance.

Methods

attempts

This method returns the number of times the job has been attempted.

delete

This method deletes the job from the queue.

fail

This method fails the job from the queue. It accepts an optional parameter that can be a Throwable object, a string, or null. If a string is provided, it will be converted into a ManuallyFailedException object.

release

This method releases the job back into the queue after a specified delay. The delay can be provided as a DateTimeInterface, DateInterval, or an integer representing the number of seconds.

setJob

This method sets the base queue job instance.

Classes

None

<?php

namespace Illuminate\Queue;

use DateTimeInterface;
use Illuminate\Contracts\Queue\Job as JobContract;
use Illuminate\Support\InteractsWithTime;
use InvalidArgumentException;
use Throwable;

trait InteractsWithQueue
{
    use InteractsWithTime;

    /**
     * The underlying queue job instance.
     *
     * @var \Illuminate\Contracts\Queue\Job|null
     */
    public $job;

    /**
     * Get the number of times the job has been attempted.
     *
     * @return int
     */
    public function attempts()
    {
        return $this->job ? $this->job->attempts() : 1;
    }

    /**
     * Delete the job from the queue.
     *
     * @return void
     */
    public function delete()
    {
        if ($this->job) {
            return $this->job->delete();
        }
    }

    /**
     * Fail the job from the queue.
     *
     * @param  \Throwable|string|null  $exception
     * @return void
     */
    public function fail($exception = null)
    {
        if (is_string($exception)) {
            $exception = new ManuallyFailedException($exception);
        }

        if ($exception instanceof Throwable || is_null($exception)) {
            if ($this->job) {
                return $this->job->fail($exception);
            }
        } else {
            throw new InvalidArgumentException('The fail method requires a string or an instance of Throwable.');
        }
    }

    /**
     * Release the job back into the queue after (n) seconds.
     *
     * @param  \DateTimeInterface|\DateInterval|int  $delay
     * @return void
     */
    public function release($delay = 0)
    {
        $delay = $delay instanceof DateTimeInterface
            ? $this->secondsUntil($delay)
            : $delay;

        if ($this->job) {
            return $this->job->release($delay);
        }
    }

    /**
     * Set the base queue job instance.
     *
     * @param  \Illuminate\Contracts\Queue\Job  $job
     * @return $this
     */
    public function setJob(JobContract $job)
    {
        $this->job = $job;

        return $this;
    }
}