Laravel 8 looped job chaining order

Viewed 285

I have two jobs that need to run one after the other. These two jobs are chained, and the entire chain is run twice.

$models = array(1,2);
foreach($models as $model) { 
    Bus::chain([
      new getRecommendations($model),
      new processRecommendations($model),
    ])->dispatch();
}

By my understanding, this is how the queue should run:

getRecommendations(1)
processRecommendations(1)
getRecommendations(2)
processRecommendations(2)

Instead, the queue is running as below:

getRecommendations(1)
getRecommendations(2)
processRecommendations(1)
processRecommendations(2)

Screenshot of PHP Artisan

How do I get the jobs to run the way I need them? I don't want processRecommendations to wait until all instances of getRecommendations are done before they run.

These are the job classes:

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

    protected $model;

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


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

        // do stuff

    }
}


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

    protected $model;

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


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

        // do stuff

    }
}
0 Answers
Related