Daniel Benzie
Daniel Benzie

Reputation: 479

Laravel job chaining

I have been tearing my hair out this afternoon attempting to get job chaining to work. No matter what happens I cannot get a chained job to run. The first always runs and succeeds but the chained will never run.

For example I have an event listener like

  class PersistPreviousStatement implements ShouldQueue
    {
        /**
         * @param StatementCreated $event
         */
        public function handle(StatementCreated $event): void
        {
            DoOneThing::withChain([
                new DoSomething()
            ])->dispatch();
        }
}

My two job classes are below:

class DoOneThing
{
    use Dispatchable, Queueable;

    public function handle(): void
    {
        $statement = Statement::find(20);
        $statement->file = 'yyy';
        $statement->save();
    }
}


class DoSomething
{
    use Dispatchable, Queueable;

    public function handle(): void
    {
        $statement = Statement::find(10);
        $statement->file = 'xxxx';
        $statement->save();
    }
}

In the situation above. My DoOneThing class correctly sets the record with 'yyy' however the DoSomething class does not run. If I change the order like

DoSomething::withChain([
            new DoOneThing()
        ])->dispatch();

DoSomething is ran not DoOnething. I have both jobs set to queueable/Dispatchable and my driver is set to sync. I also can dispatch the jobs after each other independently like:

DoSomething::dispatch() DoOneThing::dispatch()

and both will run.

Any ideas?

Upvotes: 3

Views: 3578

Answers (1)

Daniel Benzie
Daniel Benzie

Reputation: 479

I was missing the InteractsWithQueue trait in my jobs.

Upvotes: 1

Related Questions