Home > Mobile >  Laravel - Executing multiple requests after a while
Laravel - Executing multiple requests after a while

Time:08-08

Im working on a delivery app, and it has a feature where the client can pick a schedules date time for the delivery to be sent to the delivery agents, and i was thinking what would be the best way to handle this, so when a new delivery is made, a date time is given (if its null, then the delivery will be triggered immediately and sent to the delivery agents), and many delivery requests can be made at once, so i was thinking in terms of performance on the server side, and i would like to optimize this, and also mind the fact that if i had a large number of scheduled delivery requests, they all have to be sent at the right time (not late).

Would Laravel's Task scheduling be enough for this (reading from documentation now) ? i create an artisan command ( run it each 30 minutes for example) which would check the date time and if the conditions are met, it sends the delivery, else, it continues to the next one ? this kinda worried me in case there was a large number of requests.

Should i run multiple ones ? or perhaps there is a much more suitable solution to this ?

Thank you very much!

CodePudding user response:

Regardless of using the command and scheduling it, I think you need to add a job inside the command and queue the delivery requests.

CodePudding user response:

I'd save all deliveries in a table with a target execute time (or null if it's now). Then run a custom command through scheduler every minute that goes something like this:

    // get all deliveries where execute_time is null or in the past
    $deliveries = Delivery::whereNull('execute_time')->orWhere('execute_time', '<', Carbon::now())->where('executed', false)->get();

    // send deliveries to job queue
    foreach ($deliveries as $delivery) {
        dispatch(new SendDelivery($delivery));
    }

SendDelivery will be a job that sends out the notifications I guess, start multiple queue workers so everything gets done faster.

  • Related