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

amirkamizi's avatar

laravel scheduler stops the job

in the console/kernerl.php scheduler runs a job and this jobs run multiple jobs in a loop

$schedule->job(new \App\Jobs\RunProductList)->everySixHours();

RunProductList.php

try {
        set_time_limit(3000);
        $timeStart = microtime(true);
        $products= \App\Product::all();
        Log::channel('psrs')->notice('started sending jobs at ' . date("Y-m-d h:i:sa"));
        foreach ($products as $product) {
            $limit = 20;
            $offset = 0;
            $suppliers = $product->suppliers()->take($limit)->skip($offset);
            while($suppliers->count() > 0 ){
                $supplierCode = implode('-',$suppliers->pluck('ps_code')->toArray()) . '-';
                $type_codes = ['a-1','a-2'];
                foreach ($type_codes as $type_code) {
                    $today = \App\CalendarDate::where('full_date',today()->format('Y-m-d'))->first();
                    // from today till the next 7 days get cache
                    for ($i=$today->id; $i <$today->id + 7 ; $i++) {
                        \App\Jobs\GetProduct::dispatch($supplierCode,$product,$i,$type_code);
                    }
                }
                $offset = $offset + $limit;
                $properties =  $product->suppliers()->take($limit)->skip($offset);
            }
        }
        $timeEnd = microtime(true);
        $executionTime = ($timeEnd - $timeStart);
        Log::channel('psrs')->notice('sent the jobs for all the products in '.$executionTime . ' seconds');
    } catch (\Throwable $th) {
        Log::channel('psrs')->error('could not execute the jobs becuase of the following error');
        Log::channel('psrs')->error($th);
    }

GetProduct Job has a curl request and updates the results in the database.

Now the issue is that there should be around 4000 requests but only 200 requests (only the first product) is sent and then none of the rest is sent it's like foreach loop only goes through the loop once OR laravel stops the whole process.

each process takes 100 seconds. and it's laravel 7

0 likes
6 replies
sr57's avatar

-1- What's in your logs?

-2- Don't know what your code do, but why use call job in the loop, you can directly run your sql(s) or see dispatchSync

amirkamizi's avatar

1- in the log files there is not text regarding any error. it's just the things I have logged like started sending jobs at sent the jobs for all the products in seconds

not in the main log nor in the psrs log. no errors whatsoever

2- inside that job it sends curl request ( which takes around 20 seconds) to get info from supplier's api. then another function parses the info I get and then update the database based on the informationn received. if I don't send another job this one would run for hours. but this way it makes smaller tasks. I was hoping horizon would take care of the queuo managemtn and I could see the analysis of which tasks ran and which didn't. if you think if I run them here it would be better then I'll do it.

amirkamizi's avatar

in the end it says :

Setting the after_commit configuration option to true will also cause any queued event listeners, mailables, notifications, and broadcast events to be dispatched after all open database transactions have been committed.

since the process of getting the product info updates from suppliers is something that would go on and on. then if I set after_commit to true wouldn't that stop all the emails and other jobs and tasks? if yes then how to solve that issue?

sr57's avatar

wouldn't that stop

No, but wait until ... (queue)

Commit is a lock to ensure the consistence of your data.

You have 6 hours to do the jobs, it should be enough, but you always have to look/mange you running queue and failed jobs.

If you can not use commit (I don't know your usage) you have also withoutOverlapping() that's another lock

1 like
amirkamizi's avatar

oh great thank you very much for your explanation. I will try both of them. and in a few days i will let you know about the results. thanks

Please or to participate in this conversation.