From cba9c2c015a696dadf04d041a742056f32f6414b Mon Sep 17 00:00:00 2001 From: Steve Wirt Date: Tue, 13 Feb 2024 20:33:28 -0500 Subject: [PATCH 1/3] VACMS-16233 Make script-library sandbox keep track of the last success and resume. --- scripts/content/script-library.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/scripts/content/script-library.php b/scripts/content/script-library.php index 6363a152b9..1b4bb4f604 100644 --- a/scripts/content/script-library.php +++ b/scripts/content/script-library.php @@ -317,6 +317,17 @@ function script_library_sandbox_init(array &$sandbox, $counter_callback, array $ $sandbox['items_to_process'] = call_user_func_array($counter_callback, $callback_args); $sandbox['total'] = count($sandbox['items_to_process']); $sandbox['current'] = 0; + $sandbox['multi_run_state_key'] = "script_library_$counter_callback"; + + // This seems like the first run, see if there is already a state saved + // from a previous attempt. + $last_run_completed = \Drupal::state()->get($sandbox['multi_run_state_key']); + if (!is_null($last_run_completed)) { + // A state exists, so alter the 'current' and 'items_to_process'. + $sandbox['current'] = $last_run_completed + 1; + // Remove the last successful run, and all that came before it. + $sandbox['items_to_process'] = array_slice($sandbox['items_to_process'], $last_run_completed); + } } else { // Something went wrong could not use callback. Throw exception. @@ -325,6 +336,7 @@ function script_library_sandbox_init(array &$sandbox, $counter_callback, array $ ); } } + $sandbox['element'] = array_key_first($sandbox['items_to_process']); } /** @@ -341,17 +353,23 @@ function script_library_sandbox_init(array &$sandbox, $counter_callback, array $ function script_library_sandbox_complete(array &$sandbox, $completed_message) { // Determine when to stop batching. $sandbox['current'] = ($sandbox['total'] - count($sandbox['items_to_process'])); + // Save the 'current' value to state, to record a successful run. + \Drupal::state()->set($sandbox['multi_run_state_key'], $sandbox['current']); $sandbox['#finished'] = (empty($sandbox['total'])) ? 1 : ($sandbox['current'] / $sandbox['total']); $vars = [ '@completed' => $sandbox['current'], + '@element' => $sandbox['element'], '@total' => $sandbox['total'], ]; - $message = t('Processing... @completed/@total.', $vars) . PHP_EOL; + + $message = t('Processed @element. @completed/@total.', $vars) . PHP_EOL; // Log the all finished notice. if ($sandbox['#finished'] === 1) { Drupal::logger('va_gov_vamc')->log(LogLevel::INFO, $completed_message, $vars); $logged_message = new FormattableMarkup($completed_message, $vars); $message = t('Process completed:') . " {$logged_message}" . PHP_EOL; + // Delete the state as it is no longer needed. + \Drupal::state()->delete($sandbox['multi_run_state_key']); } return $message; } From 2be0fbefd801b8b413d7b059e3c949c69814304f Mon Sep 17 00:00:00 2001 From: Steve Wirt Date: Tue, 13 Feb 2024 20:37:17 -0500 Subject: [PATCH 2/3] VACMS-16233 Make menu script report progress more specific. --- .../content/VACMS-16233-reorder-vamc-menu-items.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/scripts/content/VACMS-16233-reorder-vamc-menu-items.php b/scripts/content/VACMS-16233-reorder-vamc-menu-items.php index 41123b2247..15c50e79c8 100644 --- a/scripts/content/VACMS-16233-reorder-vamc-menu-items.php +++ b/scripts/content/VACMS-16233-reorder-vamc-menu-items.php @@ -12,7 +12,10 @@ * If for some reason the run crashes before it is complete: * - Check CMS recent log messages for cause * /admin/reports/dblog?type%5B%5D=codit_menu_tools - * - Simply re-run the script. + * - Simply re-run the script, it will pick up where it left off. + * - If for some reason you need to start it at a different number, you can use + * `drush php:eval '\Drupal::state()->set("script_library__va_gov_vamc_get_system_menus", DESIRED_NUMBER);'` + * to set the last number that ran successfully. */ use Drupal\codit_menu_tools\MenuManipulator; @@ -47,8 +50,8 @@ function run(): string { */ function va_gov_vamc_deploy_resort_vamc_menus(&$sandbox) { script_library_sandbox_init($sandbox, '_va_gov_vamc_get_system_menus', []); - _va_gov_vamc_arrange_menus($sandbox); - return script_library_sandbox_complete($sandbox, "Re-arranged @total VAMC System Menus."); + $message = _va_gov_vamc_arrange_menus($sandbox); + return $message . script_library_sandbox_complete($sandbox, "Re-arranged @total VAMC System Menus."); } /** @@ -91,9 +94,9 @@ function _va_gov_vamc_arrange_menus(&$sandbox) { ]; $menu_arranger = new MenuManipulator($menu_name); $menu_arranger->matchPattern($pattern); - $message = "The menu {$sandbox['items_to_process'][$menu_name]} had been rearranged."; + $message = "The menu {$sandbox['items_to_process'][$menu_name]} had been rearranged. "; + // It has been run successfully, so remove it. unset($sandbox['items_to_process'][$menu_name]); - $sandbox['current']++; return $message; } From e7d5dd20aa4a51a6c6f2a77aee70a5d2c30d32cf Mon Sep 17 00:00:00 2001 From: Steve Wirt Date: Wed, 14 Feb 2024 23:22:03 -0500 Subject: [PATCH 3/3] Fix logging category. --- scripts/content/script-library.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/content/script-library.php b/scripts/content/script-library.php index 1b4bb4f604..ac00016965 100644 --- a/scripts/content/script-library.php +++ b/scripts/content/script-library.php @@ -365,7 +365,7 @@ function script_library_sandbox_complete(array &$sandbox, $completed_message) { $message = t('Processed @element. @completed/@total.', $vars) . PHP_EOL; // Log the all finished notice. if ($sandbox['#finished'] === 1) { - Drupal::logger('va_gov_vamc')->log(LogLevel::INFO, $completed_message, $vars); + Drupal::logger('script_library')->log(LogLevel::INFO, $completed_message, $vars); $logged_message = new FormattableMarkup($completed_message, $vars); $message = t('Process completed:') . " {$logged_message}" . PHP_EOL; // Delete the state as it is no longer needed.