From b49935701b335efa28709fea216fb2522d3e0dfc Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 5 Feb 2024 20:48:24 +0000 Subject: [PATCH] Chunk data to reduce memory on large datasets when updating `next_audit_date` Signed-off-by: snipe --- app/Http/Controllers/SettingsController.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index b83e015070..7c9924f7c6 100755 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -639,18 +639,18 @@ class SettingsController extends Controller // Be careful - this could be a negative number $audit_diff_months = ((int)$request->input('audit_interval') - (int)($setting->audit_interval)); - // Grab all of the assets that have an existing next_audit_date - $assets = Asset::whereNotNull('next_audit_date')->get(); + // Grab all assets that have an existing next_audit_date, chunking to handle very large datasets + Asset::whereNotNull('next_audit_date')->chunk(20, function ($assets) use ($audit_diff_months) { - // Update all of the assets' next_audit_date values - foreach ($assets as $asset) { - - if ($asset->next_audit_date != '') { - $old_next_audit = new \DateTime($asset->next_audit_date); - $asset->next_audit_date = $old_next_audit->modify($audit_diff_months.' month')->format('Y-m-d'); - $asset->forceSave(); + // Update assets' next_audit_date values + foreach ($assets as $asset) { + if ($asset->next_audit_date != '') { + $old_next_audit = new \DateTime($asset->next_audit_date); + $asset->next_audit_date = $old_next_audit->modify($audit_diff_months . ' month')->format('Y-m-d'); + $asset->forceSave(); + } } - } + }); } $alert_email = rtrim($request->input('alert_email'), ',');