From 1a3c947b145297511e7e9832d923d78d1113c6a9 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 5 Jan 2023 17:57:52 -0800 Subject: [PATCH] Chunk sync script Signed-off-by: snipe --- app/Console/Commands/SyncAssetCounters.php | 46 ++++++++++++---------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/app/Console/Commands/SyncAssetCounters.php b/app/Console/Commands/SyncAssetCounters.php index ab88168218..d396529f7f 100644 --- a/app/Console/Commands/SyncAssetCounters.php +++ b/app/Console/Commands/SyncAssetCounters.php @@ -39,33 +39,39 @@ class SyncAssetCounters extends Command public function handle() { $start = microtime(true); + + // We need the whole count of all assets in order to set up the progress bar + $assets_count = Asset::withTrashed()->count(); + $bar = $this->output->createProgressBar($assets_count); + $assets = Asset::withCount('checkins as checkins_count', 'checkouts as checkouts_count', 'userRequests as user_requests_count') - ->withTrashed()->get(); + ->withTrashed()->chunk(100, function ($assets) use ($bar, $start) { - if ($assets) { - if ($assets->count() > 0) { - $bar = $this->output->createProgressBar($assets->count()); + if ($assets->count() > 0) { - foreach ($assets as $asset) { - $asset->checkin_counter = (int) $asset->checkins_count; - $asset->checkout_counter = (int) $asset->checkouts_count; - $asset->requests_counter = (int) $asset->user_requests_count; - $asset->unsetEventDispatcher(); - $asset->save(); - $output['info'][] = 'Asset: '.$asset->id.' has '.$asset->checkin_counter.' checkins, '.$asset->checkout_counter.' checkouts, and '.$asset->requests_counter.' requests'; - $bar->advance(); - } - $bar->finish(); + foreach ($assets as $asset) { - foreach ($output['info'] as $key => $output_text) { - $this->info($output_text); - } + $asset->checkin_counter = (int) $asset->checkins_count; + $asset->checkout_counter = (int) $asset->checkouts_count; + $asset->requests_counter = (int) $asset->user_requests_count; + $asset->unsetEventDispatcher(); + $asset->save(); + $bar->advance(); + + \Log::debug('Asset: '.$asset->id.' has '.$asset->checkin_counter.' checkins, '.$asset->checkout_counter.' checkouts, and '.$asset->requests_counter.' requests'); + + } - $time_elapsed_secs = microtime(true) - $start; - $this->info('Sync executed in '.$time_elapsed_secs.' seconds'); } else { $this->info('No assets to sync'); } - } + }); + + + $bar->finish(); + $time_elapsed_secs = microtime(true) - $start; + $this->info("\nSync of ".$assets_count.' assets executed in '.$time_elapsed_secs.' seconds'); + + } }