Wait for slaves in RecentChangesUpdateJob
[lhc/web/wiklou.git] / includes / jobqueue / jobs / RecentChangesUpdateJob.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Aaron Schulz
20 */
21
22 /**
23 * Job for pruning recent changes
24 *
25 * @ingroup JobQueue
26 * @since 1.25
27 */
28 class RecentChangesUpdateJob extends Job {
29 function __construct( $title, $params ) {
30 parent::__construct( 'recentChangesUpdate', $title, $params );
31
32 if ( !isset( $params['type'] ) ) {
33 throw new Exception( "Missing 'type' parameter." );
34 }
35
36 $this->removeDuplicates = true;
37 }
38
39 /**
40 * @return RecentChangesUpdateJob
41 */
42 final public static function newPurgeJob() {
43 return new self(
44 SpecialPage::getTitleFor( 'Recentchanges' ), array( 'type' => 'purge' )
45 );
46 }
47
48 public function run() {
49 if ( $this->params['type'] === 'purge' ) {
50 $this->purgeExpiredRows();
51 } else {
52 throw new Exception( "Invalid 'type' parameter '{$this->params['type']}'." );
53 }
54
55 return true;
56 }
57
58 protected function purgeExpiredRows() {
59 global $wgRCMaxAge;
60
61 $lockKey = wfWikiID() . ':recentchanges-prune';
62
63 $dbw = wfGetDB( DB_MASTER );
64 if ( !$dbw->lock( $lockKey, __METHOD__, 1 ) ) {
65 return; // already in progress
66 }
67 $batchSize = 100; // Avoid slave lag
68
69 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
70 do {
71 $rcIds = $dbw->selectFieldValues( 'recentchanges',
72 'rc_id',
73 array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
74 __METHOD__,
75 array( 'LIMIT' => $batchSize )
76 );
77 if ( $rcIds ) {
78 $dbw->delete( 'recentchanges', array( 'rc_id' => $rcIds ), __METHOD__ );
79 }
80 // No need for this to be in a transaction.
81 $dbw->commit( __METHOD__, 'flush' );
82
83 if ( count( $rcIds ) === $batchSize ) {
84 // There might be more, so try waiting for slaves
85 if ( !wfWaitForSlaves( null, false, false, /* $timeout = */ 3 ) ) {
86 // Another job will continue anyway
87 break;
88 }
89 }
90 } while ( $rcIds );
91
92 $dbw->unlock( $lockKey, __METHOD__ );
93 }
94 }