简体   繁体   中英

Run jobs one after another in Laravel 8

I'm trying to automate a process composed from multiple jobs. Every job had a goal, so I'm running a global job who is responsible for creating child jobs.

               - Job-1
Global Job ->  - Job-2
               - Job-3
               - Job-4

I want to run them one after another, for know the queue process run all the four jobs in the same time and this is what I don't want.

Is it about configration ? otherwise what it the prefect way to handle this kind of needs

This is what y Global job class looks like

<?php

namespace App\Jobs;

use App\Models\Objects;
use App\Helpers\UtilsHelper;
use Illuminate\Support\Facades\Log;
use App\Services\JobsLimitControler;

/**
 * This job will have the responsability to create other ones 
 */
class AutoImport extends Job
{
    
    /**
     * Job key
     * @var string $key
     */
    public $key;

    /**
     * Job type
     * @var string $jobType
     */
    public $type = 'auto-import';

    /**
     * Job type
     * @var string $jobType
     */
    public $title = 'Automatic import';

    /**
     * Job params array
     * @var array $params
     */
    public $params;

    /**
     * Location
     * @var Objects $object
     */
    public Locations $object;
    
    /**
     * The number of seconds after which the job's unique lock will be released.
     *
     * @var int
     */
    public $uniqueFor = 600;
 
    /**
     * The unique ID of the job.
     *
     * @return string
     */
    public function uniqueId()
    {
        return $this->key;
    }


    /**
     * Create a new job instance.
     * @param string $id Job id
     * @param Objects $object
     */
    public function __construct(string $id,Objects $object)
    {   
        # Job key attribute
        $this->key = $id;
        # Preparing job to be trackable
        $this->prepareStatus(['key' => $id]);
        # Location instance
        $this->object = $object;
    }

    /**
     * Execute the job
     */
    public function handle()
    {
        try{
            # First we verify if the object is well setted
            if($this->object){
                 $this->callJobOne();
                 $this->callJobTwo();
                 $this->callJobThree();
                 $this->callJobFour();
            }else{
                Log::channel('auto-import')->info("some log");
            }
            
        }catch(\Exception $e){
            # Mark the job as failed
            $this->fail($e);
            # We throw the error to be intercepted by the job tracker
            throw $e;
        }
    }
}

You can use Laravel's Job Batches for grouping purpose. https://laravel.com/docs/8.x/queues#job-batching

And Job Chaining to progress sequentially. https://laravel.com/docs/8.x/queues#job-chaining

You may define a set of chained jobs within a batch by placing the chained jobs within an array

https://laravel.com/docs/8.x/queues#chains-within-batches

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM