laravel / ideas

Issues board used for Laravel internals discussions.
939 stars 28 forks source link

Allow delaying jobs which are added to a batch #2463

Open larsnystrom opened 3 years ago

larsnystrom commented 3 years ago

At the moment, if you add a new job to a batch (with batch->add() in the handle method of a job which part of that batch) the job is dispatched immediately, even if you call delay(...) on the job before adding it. This was brought up in laravel/framework#35619 and was marked as expected behavior.

I think this behavior is an unnecessary limitation and I propose that batches respect delays of individual jobs.

At the moment my work-around is to add a $releaseFor property to the job I want delayed, and then manually release the job for $releaseFor seconds. Like this:

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    if ($this->releaseFor > 0 && $this->attempts() < 2) {
        $this->release($this->releaseFor);
        return;
    }

    ...

This is pretty messy, but from what I can see it works. It relies on attempts() being incremented when releasing a job, so that it is delayed only once.

It would be a lot easier if the normal delay() functionality worked for batched jobs though.