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.