O
O
on_click2021-08-28 12:39:02
Laravel
on_click, 2021-08-28 12:39:02

Bus::batch how to wait for an asynchronous task to complete before moving on to the next one?

Greetings!

There is a chain of tasks that:
1) sends a request to create a download
2) checks the status of this download
3) if the download is completed, sends a request for encoding
4) if the encoding is completed, updates the entry in the database

The problem is that task 2 completes immediately, that is the file has not yet been loaded, but it is already working 3rd
In normal jobs, if you call any Exception, the task tries to run again and the next one in the chain does not work, for some reason it does not work in bus::batch

Please tell me how can I wait for the execution of the second asynchronous task, before the start of the third?

$batch = Bus::batch([])->dispatch();

foreach ($video_files as $video_file) {
    $file_chain = [
        new FileDownloadTask($video_file->id),
        new FileDownloadStatus($video_file->id),
        new TranscoderTask($video_file->id),
        new TranscoderStatus($video_file->id),
    ];

    $batch->add($file_chain);
}


class FileDownloadStatus implements ShouldQueue
{
    use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $backoff = 10;

    public function handle() {
        ...
        if ($response->status == "completed") {
            ...
        } else {
            throw new \Exception('Download not ready');
        }
    }

    public function retryUntil()
    {
        return now()->addHours(5);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2021-08-28
@on_click

Add tasks to a batch in progress: launch the second from the first one, launch the third one from the second, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question