1

I'm using Laravel scheduler that loop over some configuration and call artisan commands.

What I noticed that every time the scheduler starts, it consumes 60% of CPU and same for commands started from the scheduler, starts with high CPU and ends with 1.5% of the CPU.

Any clue what could result in such behaviour?

Thanks ;)

Ahmed Alaa El-Din
  • 1,813
  • 1
  • 16
  • 19
  • Did you eventually figure it out? I'm having a similar issue... Turns out that in my development environmente (Homestead, so nginx) this isn't an issue but on production (a cloud-based Windows Server VM), CPU usage skyrockets. – clesmalo Jun 26 '21 at 02:48

1 Answers1

0

Since the question is about CPU consumption, I'm guessing optmization could be a reason.

1) Let's first try it with php function memory_get_usage. Since you are using Laravel Scheduler, I recommend adding the below block of code where needed and Log them step by step to see memory usage.

<?php
function convert($size)
{
    $unit=array('b','kb','mb','gb','tb','pb');
    return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}

echo convert(memory_get_usage(true)); // 123 kb
Log::debug('memory usage');
?>

You can access the logs under /storage/logs folder. At least you will get and idea where you memory is consumed unnecessarily.

2) Next, you can go through these 6 steps of optimization elaborated in this stack overflow question.

3) How often the scheduler is running? Maybe you can increase the interval.

4) When do you run it? If you are running it when the portal is swamped by users, then try changing the time to later/before stage where the server is not used by users.

Lastly, I can say that most of the time it's because of bad coding. Refactor so you can re-use code. Optimize so you use what is needed. Hope it helps. Cheers!

Digvijay
  • 7,836
  • 3
  • 32
  • 53