Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VACMS-16233 make script batches pick up where they left off #17236

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading