簡體   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