繁体   English   中英

在 Laravel 8 中一个接一个地运行作业

[英]Run jobs one after another in Laravel 8

我正在尝试自动化由多个作业组成的流程。 每项工作都有一个目标,所以我正在运行一个负责创建子工作的全球工作。

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

我想一个接一个地运行它们,因为知道队列进程同时运行所有四个作业,这是我不想要的。

是关于配置吗? 否则它是处理这种需求的完美方式

这就是你的全球工作类的样子

<?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;
        }
    }
}

你可以使用 Laravel 的 Job Batches 进行分组。 https://laravel.com/docs/8.x/queues#job-batching

作业链接按顺序进行。 https://laravel.com/docs/8.x/queues#job-chaining

您可以通过将链接的作业放置在数组中来定义批处理中的一组链接作业

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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