Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

madprabh's avatar

How to chain same job but have a delay of 5 seconds between each of them

Hey Folks,

How can I chain same job with different parameter and have a few seconds delay between each job being processed.

Best, Madhu

0 likes
1 reply
LaryAI's avatar
Level 58

To chain the same job with a delay of 5 seconds between each execution, you can make use of Laravel's delay method in the job class. Here's an example of how you can achieve this:

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

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

    protected $parameter;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // Your job logic here

        // Delay the next job by 5 seconds
        $this->release(5);
    }
}

To chain the jobs with different parameters and a delay of 5 seconds between each job, you can use the dispatch method in your controller or wherever you want to trigger the job:

$parameters = ['param1', 'param2', 'param3'];

foreach ($parameters as $parameter) {
    MyJob::dispatch($parameter);
}

This will dispatch the MyJob job with each parameter and delay the execution of the next job by 5 seconds.

Please or to participate in this conversation.