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

CodeWriter's avatar

Laravel execute job after X seconds of inactivity

Imagine you have a Laravel application (latest version). In this application you have 2 tables: trucks and parcels. The parcels table has a foreign key of truck_id.

The application allows a user to add several parcels to a truck. These may come at irregular but close intervals. They may also be on their own, or part of a ‘batch’. Each request to add a parcel does not have awareness of other requests that may also be happening - it doesn’t know if it’s part of a batch or single.

The application has a Job which can be queued with a Truck as a argument. This job should be executed after 60 seconds of ‘inactivity’ (no parcels added). So imagine the user adds 10 parcels over the course of 3 minutes. This job should execute on the 4th minute only. Or imagine if the user adds 1 parcel in the 1st minute, the job should execute on the 2nd minute.

This is fairly comparable to the ‘unique’ job functionality that already exists, however I need subsequently added jobs to cancel previous jobs, rather than subsequently added jobs to be cancelled themselves because there is already a job.

Is there a solution in Laravel that cater for this that does not use the scheduler? I can think of a way this would be achieved using the scheduler, however this has potential edge-case style weaknesses.

Conceptually what I am looking to achieve is similar to how I would handle form auto saves, whereby when a field is edited a setTimeout is initiated. Any subsequent edits cancel the previous setTimeout and places it’s own, meaning ‘batch’ updates are done at once X time after the last change is made

0 likes
1 reply
Mick79's avatar

It's a complex question but I'd use something in the front end to create a counter, kinda like

// PSEUDO CODE
t = 0
every one second t = t+1
If user adds a parcel t = 0
if t == 60 -> Ajax to the backend and do the job

Please or to participate in this conversation.