mardi 6 septembre 2022

Laravel wait for a specific queued listener to finish then return

I have an Event with a bunch of queued listeners. Can't run sync because I am calling external APIs etc

Events\Invoice\InvoiceEvent::class => [
    Listeners\Invoice\Listener1::class, 
    Listeners\Invoice\Listener2::class,
    Listeners\Invoice\Listener3::class, // calling external APIs
    Listeners\Invoice\Listener4::class,
    Listeners\Invoice\Listener5::class,
],

Calling this event from a controller method.

public function store(Request $request)
{
    $invoice = Invoice::findOrFail($request->id);
    InvoiceEvent::dispatch($invoice); // Async event, it cannot be sync

    return $invoice; // need to return only when Listener3 finish execution
}

return $invoice is dependent on Listener3, otherwise, it will return incomplete data.

How can I return only when Listener3 is finished executing?

I came up with sleep(10); but it's not an ideal solution.

Listener3 saves data from third-party API to the invoices table which needs to be returned, that's why cannot return incomplete invoice data, right now the required data gets added to the invoice but after its return



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/qmG2r08
via IFTTT

Aucun commentaire:

Enregistrer un commentaire