-
Notifications
You must be signed in to change notification settings - Fork 50
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
New drush command to batch purge old messages. #23
base: 7.x-1.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Defines drush commands. | ||
*/ | ||
|
||
|
||
/** | ||
* Implements hook_drush_command(). | ||
*/ | ||
function message_drush_command() { | ||
$items = array(); | ||
$items['message-purge'] = array( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New line here. |
||
'description' => t('Batch purge old messages (will only affect messages that are eligible for purging based on message settings).'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace |
||
'arguments' => array( | ||
'purge_limit' => 'The total number of messages to purge. Defaults to 10,000. Set to -1 to purge all eligible messages (warning: this can cause memory limit errors for very large sets).', | ||
'loop_limit' => 'The number of messages to purge per loop. Defaults to 100.', | ||
), | ||
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, | ||
'aliases' => array('purge-messages'), | ||
'drupal dependencies' => array('message'), | ||
'callback' => 'drush_message_purge', | ||
); | ||
return $items; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add new a new line. |
||
} | ||
|
||
/** | ||
* Drush command to batch purge old messages. | ||
*/ | ||
function drush_message_purge() { | ||
drush_print("Starting message purge."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's use |
||
|
||
// Get drush params. | ||
$args = drush_get_arguments(); | ||
$purge_limit = MESSAGE_PURGE_LIMIT_DRUSH_DEFAULT; | ||
$loop_limit = MESSAGE_PURGE_LOOP_DRUSH_DEFAULT; | ||
if (isset($args[1]) && is_numeric($args[1])) { | ||
$purge_limit = $args[1]; | ||
} | ||
if (isset($args[2]) && is_numeric($args[2])) { | ||
$loop_limit = $args[2]; | ||
} | ||
if ($purge_limit > 0) { | ||
drush_print("Purge Limit: $purge_limit"); | ||
} | ||
else { | ||
drush_print("Purge Limit: no limit (purge all eligible messages)"); | ||
} | ||
drush_print("Loop Limit: $loop_limit"); | ||
|
||
$completed = 0; | ||
do { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like the do while loop. Implement a batch operation when deleting a 500 everey time when this would be a drush arguments that can be changed when passing in the command. |
||
// Prevent over-purging. | ||
if ($purge_limit > 0 && $completed + $loop_limit > $purge_limit) { | ||
$loop_limit = $purge_limit - $completed; | ||
} | ||
|
||
// Do the actual purging. | ||
$purge_count = message_purge($loop_limit); | ||
|
||
// Stop if there are no more messages to purge. | ||
if ($purge_count == 0) { | ||
drush_print("No more messages to purge."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use dt. |
||
return; | ||
} | ||
|
||
// Provide feedback on progress. | ||
$completed += $purge_count; | ||
$percent_completed = round(100 * $completed/$purge_limit); | ||
if ($purge_limit > 0) { | ||
drush_print("Completed $completed of $purge_limit purges ($percent_completed%)."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use dt here also. |
||
} | ||
else { | ||
drush_print("Completed $completed purges."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dt |
||
} | ||
|
||
// Stop when we reach purge limit. | ||
if ($purge_limit > 0 && $completed >= $purge_limit) { | ||
drush_print("Purge limit reached."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dt |
||
return; | ||
} | ||
} while (TRUE); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New line here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One one should separate between the blocks.