Last active
May 6, 2022 08:18
-
-
Save vuthaihoc/666e48dc5ccccc5da961af7d362b8d75 to your computer and use it in GitHub Desktop.
Add CLI_MEMORY_LIMIT to change memory_limit of php when run commands as scheduled task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// config/app.php | |
//... | |
'name' => env( 'APP_NAME', '123dok' ), | |
'cli_memory_limit' => env('CLI_MEMORY_LIMIT', "300M"), | |
//... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/Providers/AppServiceProvider.php | |
// .... | |
public function boot(){ | |
if ($this->app->runningInConsole()) { | |
$this->setMemoryLimit(); | |
} | |
// .... | |
} | |
//.... | |
protected function setMemoryLimit(){ | |
$current_memory_limit = ini_get('memory_limit'); | |
$cli_memory_limit = config('app.cli_memory_limit'); | |
if($current_memory_limit == -1){ | |
return; | |
} | |
if($cli_memory_limit == -1) { | |
ini_set('memory_limit', $cli_memory_limit); | |
}else{ | |
ini_set('memory_limit', max($this->convertReadableSizeToByte($current_memory_limit), $this->convertReadableSizeToByte($cli_memory_limit))); | |
} | |
} | |
protected function convertReadableSizeToByte($size_string) : int { | |
$size = (int)$size_string; | |
$string = str_replace($size, "", $size_string); | |
return match ($string) { | |
"M" => $size * 1048576, | |
"K" => $size * 1024, | |
"G" => $size * 1073741824, | |
default => $size, | |
}; | |
} | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment