Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

matpratta's avatar

Add job to queue but wait for it to complete on request

I'm currently working on a project which requires certain requests to always be executed in order, based on which request come first, always waiting for the previous one to complete.

With that requirement in mind, I've considered using queues, as it seems to match the use case very well, but noticed that the controller would queue the job and return, even though it has not completed yet.

My question is, is there a way to "await" for a background job to be completed?

Thanks in advance!

0 likes
6 replies
pdigital's avatar

Is running the queue connection in sync an option? This way all processes will be run synchronously though.

matpratta's avatar

@pdigital not really, from what I understand, running it as sync would make it run directly in the request's process, completely ignoring other incoming requests.

The idea was keeping all incoming requests being processed in order, one by one, maybe in a worker process, but still having the actual requests "wait" until their respective jobs get done.

maxxd's avatar

@matpratta You can always dispatch the next job in the flow from the currently running job. Basically, create an execution flow and start the next step at the end of the current step.

edit: So, basically, what kevinbul just showed much easier.

Snapey's avatar

obviously the queue approach will only work if you restrict yourself to one worker, and if the job fails it might be pushed back to the queue behind other requests.

That consideration aside, you would need a way for the job to communicate to the request through some shared medium such as cache

You could put a unique key in cache, then give that same key to the queued job.

In the request, test if the key still exists, and if it does, sleep for 250ms, then check again. If the key has vanished, finish your request

in the queued job, then when finished, clear the cache key

Snapey's avatar

The other alternative is to not use queue at all, and to obtain an atomic lock, then perform the work, clear the lock and finish the request

A second request must wait for the lock to be clear before it can proceed

potentially a simpler solution than queueing

Please or to participate in this conversation.