From 33da8354db3d40a54c265c210653c8e462772386 Mon Sep 17 00:00:00 2001 From: Rev Date: Tue, 26 Oct 2021 11:37:51 +0900 Subject: [PATCH] set execution time and memory when refreshing the data --- app/Data/ItemsCollection.php | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/app/Data/ItemsCollection.php b/app/Data/ItemsCollection.php index 808ea59..e4010f1 100644 --- a/app/Data/ItemsCollection.php +++ b/app/Data/ItemsCollection.php @@ -3,6 +3,7 @@ namespace App\Data; use App\Exceptions\ItemNotFoundException; +use Exception; use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Http; @@ -34,8 +35,16 @@ class ItemsCollection } } + /** + * @return void + * @throws Exception + */ public function refreshLocalesCache(): void { + $time_limit = ini_get('max_execution_time'); + $memory_limit = ini_get('memory_limit'); + set_time_limit(180); + ini_set('memory_limit', '256M'); $this->locales = []; $rawLocalesGlobalBaseUrl = GiteaConfig::RAW_LOCALES_GLOBAL_BASE_URL; @@ -55,18 +64,35 @@ class ItemsCollection $customizationLocale = $currentLocaleJson['customization']; $this->locales[$trimmedCurrentLocaleName] = array_merge($templateLocale, $customizationLocale); } - file_put_contents(storage_path($this->locales_file_key ), json_encode($this->locales)); + if (!$handle = fopen(storage_path($this->locales_file_key ), 'w')) { + throw new Exception('Cannot open the file'); + } + fwrite($handle, json_encode($this->locales)); + fclose($handle); + set_time_limit($time_limit); + ini_set('memory_limit', $memory_limit); } /** * @return void + * @throws Exception */ public function refreshItemsCache(): void { + $time_limit = ini_get('max_execution_time'); + $memory_limit = ini_get('memory_limit'); + set_time_limit(180); + ini_set('memory_limit', '256M'); $items = Http::withOptions(['verify' => false])->get(GiteaConfig::RAW_ITEMS_URL)->json(); $customization = Http::withOptions(['verify' => false])->get(GiteaConfig::RAW_CUSTOMIZATION_URL)->json(); $this->items = array_merge($items, $customization); - file_put_contents(storage_path($this->items_file_key), json_encode($this->items)); + if (!$handle = fopen(storage_path($this->items_file_key ), 'w')) { + throw new Exception('Cannot open the file'); + } + fwrite($handle, json_encode($this->items)); + fclose($handle); + set_time_limit($time_limit); + ini_set('memory_limit', $memory_limit); } /** @@ -79,6 +105,7 @@ class ItemsCollection /** * @return void + * @throws Exception */ public function refreshAllCache(): void {