Skip to content

Commit

Permalink
VACMS-16233 make script batches pick up where they left off (#17236)
Browse files Browse the repository at this point in the history
* VACMS-16233 Make script-library sandbox keep track of the last success and resume.

* VACMS-16233 Make menu script report progress more specific.

* Fix logging category.
  • Loading branch information
swirtSJW authored Feb 15, 2024
1 parent 3ffc27a commit 6d4696e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
13 changes: 8 additions & 5 deletions scripts/content/VACMS-16233-reorder-vamc-menu-items.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
}

/**
Expand Down Expand Up @@ -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;
}
22 changes: 20 additions & 2 deletions scripts/content/script-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -325,6 +336,7 @@ function script_library_sandbox_init(array &$sandbox, $counter_callback, array $
);
}
}
$sandbox['element'] = array_key_first($sandbox['items_to_process']);
}

/**
Expand All @@ -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);
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.
\Drupal::state()->delete($sandbox['multi_run_state_key']);
}
return $message;
}
Expand Down

0 comments on commit 6d4696e

Please sign in to comment.