Does constructor values of a job recreate or reused during retries? – Laravel

Does property values of a queued job that are initialized within the constructor of a job class remained the same during the retries? Short answer is yes.

Constructor values of job class remained same during retries because the job instance is serialized with those values and then deserialized with the same values during each retry. This ensures consistency in the job’s state throughout its retry lifecycle in the queue.

Example

Task

Create a Laravel web application to demonstrate that constructor values remain the same across retries of a queued job. Create a job that throws exceptions in the handle method to retry the same job.

Example

First setup a Laravel project. Then create a sample job class called ActivateSubscription using below artisan command.

php artisan make:job ActivateSubscription

Open created job class ActivateSubscription

  1. We are going to use current date time in order to check whether constructer values are regenerated or reused.
  2. Create a variable to hold current date time.
  3. Log created time and time in the handle method.
  4. Throws an exception to retry the same job again.
namespace App\Jobs;

use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Log;

class ActivateSubscription implements ShouldQueue
{
    use Queueable;
    private $createdTime;

    /**
     * Create a new job instance.
     */
    public function __construct()
    {
        $this->createdTime = now();
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        Log::info("constructor time: $this->createdTime");
        log::info("current time: ". now());
        sleep(5);
        throw new Exception("Exception thrown here");
    }
    
}

Now execute created job class. For that I am using web.php file.

<?php

use App\Jobs\ActivateSubscription;
use App\Jobs\DeactivateSubscription;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    Log::info("Activate subscription request received");
    ActivateSubscription::dispatch();
});

Now test the application. First run this web application using below command.

php artisan serve

Now run the queue worker with retries option.

php artisan queue:work --retries=3

Check Laravel logs

[2024-12-19 09:28:42] local.INFO: Activate subscription request received  
[2024-12-19 09:28:45] local.INFO: constructor time: 2024-12-19 09:28:42  
[2024-12-19 09:28:45] local.INFO: current time: 2024-12-19 09:28:45  
[2024-12-19 09:28:50] local.ERROR: Exception thrown here ...
[2024-12-19 09:28:51] local.INFO: constructor time: 2024-12-19 09:28:42  
[2024-12-19 09:28:51] local.INFO: current time: 2024-12-19 09:28:51  
[2024-12-19 09:28:56] local.ERROR: Exception thrown here ...
[2024-12-19 09:28:56] local.INFO: constructor time: 2024-12-19 09:28:42  
[2024-12-19 09:28:56] local.INFO: current time: 2024-12-19 09:28:56  
[2024-12-19 09:29:01] local.ERROR: Exception thrown here ...

Constructer time remains the same for every retry.