MediaWiki 1.40.4
purgeParserCache.php
Go to the documentation of this file.
1<?php
21require_once __DIR__ . '/Maintenance.php';
22
25use Wikimedia\Timestamp\ConvertibleTimestamp;
26
38
40 private $lastProgress;
41
43 private $lastTimestamp;
44
45 private $tmpCount = 0;
46 private $usleep = 0;
47
48 public function __construct() {
49 parent::__construct();
50 $this->addDescription( "Remove old objects from the parser cache. " .
51 "This only works when the parser cache is in an SQL database." );
52 $this->addOption( 'expiredate', 'Delete objects expiring before this date.', false, true );
53 $this->addOption(
54 'age',
55 'Delete objects created more than this many seconds ago, assuming ' .
56 '$wgParserCacheExpireTime has remained consistent.',
57 false,
58 true );
59 $this->addOption( 'dry-run', 'Perform a dry run, to verify age and date calculation.' );
60 $this->addOption( 'msleep', 'Milliseconds to sleep between purge chunks of $wgUpdateRowsPerQuery.',
61 false,
62 true );
63 $this->addOption(
64 'tag',
65 'Purge a single server only. This feature is designed for use by large wiki farms where ' .
66 'one has to purge multiple servers concurrently in order to keep up with new writes. ' .
67 'This requires using the SqlBagOStuff "servers" option in $wgObjectCaches.',
68 false,
69 true );
70 }
71
72 public function execute() {
73 $inputDate = $this->getOption( 'expiredate' );
74 $inputAge = $this->getOption( 'age' );
75
76 if ( $inputDate !== null ) {
77 $timestamp = strtotime( $inputDate );
78 } elseif ( $inputAge !== null ) {
79 $expireTime = (int)$this->getConfig()->get( MainConfigNames::ParserCacheExpireTime );
80 $timestamp = time() + $expireTime - intval( $inputAge );
81 } else {
82 $this->fatalError( "Must specify either --expiredate or --age" );
83 }
84 $this->usleep = 1e3 * $this->getOption( 'msleep', 0 );
85 $this->lastTimestamp = microtime( true );
86
87 $humanDate = ConvertibleTimestamp::convert( TS_RFC2822, $timestamp );
88 if ( $this->hasOption( 'dry-run' ) ) {
89 $this->fatalError( "\nDry run mode, would delete objects having an expiry before " . $humanDate . "\n" );
90 }
91
92 $this->output( "Deleting objects expiring before " . $humanDate . "\n" );
93
94 $pc = MediaWikiServices::getInstance()->getParserCache()->getCacheStorage();
95 $success = $pc->deleteObjectsExpiringBefore(
96 $timestamp,
97 [ $this, 'showProgressAndWait' ],
98 INF,
99 // Note that "0" can be a valid server tag, and must not be discarded or changed to null.
100 $this->getOption( 'tag', null )
101 );
102 if ( !$success ) {
103 $this->fatalError( "\nCannot purge this kind of parser cache." );
104 }
105 $this->showProgressAndWait( 100 );
106 $this->output( "\nDone\n" );
107 }
108
109 public function showProgressAndWait( $percent ) {
110 // Parser caches involve mostly-unthrottled writes of large blobs. This is sometimes prone
111 // to replication lag. As such, while our purge queries are simple primary key deletes,
112 // we want to avoid adding significant load to the replication stream, by being
113 // proactively graceful with these sleeps between each batch.
114 // The reason we don't explicitly wait for replication is that that would require the script
115 // to be aware of cross-dc replicas, which we prefer not to, and waiting for replication
116 // and confirmation latency might actually be *too* graceful and take so long that the
117 // purge script would not be able to finish within 24 hours for large wiki farms.
118 // (T150124).
119 usleep( $this->usleep );
120 $this->tmpCount++;
121
122 $percentString = sprintf( "%.1f", $percent );
123 if ( $percentString === $this->lastProgress ) {
124 // Only print a line if we've progressed >= 0.1% since the last printed line.
125 // This does not mean every 0.1% step is printed since we only run this callback
126 // once after a deletion batch. How often and how many lines we print depends on the
127 // batch size (SqlBagOStuff::deleteObjectsExpiringBefore, $wgUpdateRowsPerQuery),
128 // and on how many table rows there are.
129 return;
130 }
131 $now = microtime( true );
132 $sec = sprintf( "%.1f", $now - $this->lastTimestamp );
133
134 // Give a sense of how much time is spent in the delete operations vs the sleep time,
135 // by recording the number of iterations we've completed since the last progress update.
136 $this->output( "... {$percentString}% done (+{$this->tmpCount} iterations in {$sec}s)\n" );
137
138 $this->lastProgress = $percentString;
139 $this->tmpCount = 0;
140 $this->lastTimestamp = $now;
141 }
142}
143
144$maintClass = PurgeParserCache::class;
145require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Remove expired objects from the parser cache database.
execute()
Do the actual work.
__construct()
Default constructor.
showProgressAndWait( $percent)