Add progress output and optional --sleep-time parameter to triminboxes.php

This commit is contained in:
Brion Vibber 2009-12-29 14:05:43 -08:00
parent 2cfa90c752
commit e3850e5273
2 changed files with 31 additions and 2 deletions

View File

@ -106,6 +106,13 @@ class Notice_inbox extends Memcached_DataObject
return Memcached_DataObject::pkeyGet('Notice_inbox', $kv); return Memcached_DataObject::pkeyGet('Notice_inbox', $kv);
} }
/**
* Trim inbox for a given user to latest NOTICE_INBOX_LIMIT items
* (up to NOTICE_INBOX_GC_MAX will be deleted).
*
* @param int $user_id
* @return int count of notices dropped from the inbox, if any
*/
static function gc($user_id) static function gc($user_id)
{ {
$entry = new Notice_inbox(); $entry = new Notice_inbox();
@ -133,6 +140,8 @@ class Notice_inbox extends Memcached_DataObject
$notices = array(); $notices = array();
} }
} }
return $total;
} }
static function deleteMatching($user_id, $notices) static function deleteMatching($user_id, $notices)

View File

@ -21,19 +21,21 @@
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
$shortoptions = 'u::'; $shortoptions = 'u::';
$longoptions = array('start-user-id::'); $longoptions = array('start-user-id=', 'sleep-time=');
$helptext = <<<END_OF_TRIM_HELP $helptext = <<<END_OF_TRIM_HELP
Batch script for trimming notice inboxes to a reasonable size. Batch script for trimming notice inboxes to a reasonable size.
-u <id> -u <id>
--start-user-id=<id> User ID to start after. Default is all. --start-user-id=<id> User ID to start after. Default is all.
--sleep-time=<integer> Amount of time to wait (in seconds) between trims. Default is zero.
END_OF_TRIM_HELP; END_OF_TRIM_HELP;
require_once INSTALLDIR.'/scripts/commandline.inc'; require_once INSTALLDIR.'/scripts/commandline.inc';
$id = null; $id = null;
$sleep_time = 0;
if (have_option('u')) { if (have_option('u')) {
$id = get_option_value('u'); $id = get_option_value('u');
@ -43,6 +45,12 @@ if (have_option('u')) {
$id = null; $id = null;
} }
if (have_option('--sleep-time')) {
$sleep_time = intval(get_option_value('--sleep-time'));
}
$quiet = have_option('q') || have_option('--quiet');
$user = new User(); $user = new User();
if (!empty($id)) { if (!empty($id)) {
@ -52,5 +60,17 @@ if (!empty($id)) {
$cnt = $user->find(); $cnt = $user->find();
while ($user->fetch()) { while ($user->fetch()) {
Notice_inbox::gc($user->id); if (!$quiet) {
print "Trimming inbox for user $user->id";
}
$count = Notice_inbox::gc($user->id);
if ($count) {
if (!$quiet) {
print ": $count trimmed...";
}
sleep($sleep_time);
}
if (!$quiet) {
print "\n";
}
} }