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

Foks's avatar
Level 15

How to make jobs run in sequence after each other?

I have a job which dispatches quite a few of another jobs. What the specific jobs do is irrelevant.

My problem is that I have a manager job, which dispatches all these jobs. The manager job gets for every user, but I cannot start processing the next manager job before all the sub-jobs dispatched by the prior manager job are completed. How would I achieve this?

The manager job has a "supervisor-job" which figures out which users need to have the manager job run on.

So the code looks something like this:

Supervisor-job handle

    public function handle(): void
    {
        $count = count(User::list());

        if (Manager::count() == $count) {
            return;
        }

        $chain = [];
        for ($i = 0; $i < $count; $i++) {
            $chain[] = new ManagerJob($i);
        }

        Bus::chain($chain)
            ->dispatch();
    }

ManagerJob handle

    public function handle(): void
    {
       $chain = [];

        $chain[] = new JobOne($data->one);
        $chain[] = new JobTwo($data->two);
        $chain[] = new JobTree($data->three);

        Bus::chain($chain)
            ->dispatch();
    }
0 likes
4 replies
martinbean's avatar

@foks So what’s the issue? You’re using a job chain, which is the appropriate solution here.

Foks's avatar
Level 15

@martinbean My issue is that some manager jobs are finishing before the one prior to it.

Exampel: The chain in the supervisor job chains the jobs like this:

[ManagerJob(1), ManagerJob(2), ManagerJob(3)]

Then in Laravel Horizon I see that they finish off like this:

ManagerJob(1)
ManagerJob(3)
ManagerJob(2)

Instead of 1,2,3.

I hope it makes sense.

Foks's avatar
Level 15

@martinbean I use Redis, however, I think that the reason why it happens is that the manger job doesn't wait until the chain inside the ManagerJob has finished. The specific chains inside the different ManagerJobs can have a lot of verity in how long it takes to process.

Please or to participate in this conversation.