[JobQueue] Added aggregate empty/non-empty queue caching.
[lhc/web/wiklou.git] / maintenance / nextJobDB.php
1 <?php
2 /**
3 * Pick a database that has pending jobs
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once( __DIR__ . '/Maintenance.php' );
25
26 /**
27 * Maintenance script that picks a database that has pending jobs.
28 *
29 * @ingroup Maintenance
30 */
31 class nextJobDB extends Maintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = "Pick a database that has pending jobs";
35 $this->addOption( 'type', "Search by job type", false, true );
36 $this->addOption( 'types', "Space separated list of job types to search for", false, true );
37 }
38
39 public function execute() {
40 global $wgMemc;
41
42 $type = false; // job type required/picked
43 if ( $this->hasOption( 'types' ) ) {
44 $types = explode( ' ', $this->getOption( 'types' ) );
45 } elseif ( $this->hasOption( 'type' ) ) {
46 $types = array( $this->getOption( 'type' ) );
47 } else {
48 $types = JobQueueGroup::singleton()->getDefaultQueueTypes();
49 }
50
51 // Handle any required periodic queue maintenance
52 $this->executeReadyPeriodicTasks();
53
54 // Get all the queues with jobs in them
55 $pendingDBs = JobQueueAggregator::singleton()->getAllReadyWikiQueues();
56 if ( !count( $pendingDBs ) ) {
57 return; // no DBs with jobs or cache is both empty and locked
58 }
59
60 do {
61 $again = false;
62
63 $candidates = array(); // list of (type, db)
64 // Flatten the tree of candidates into a flat list so that a random
65 // item can be selected, weighing each queue (type/db tuple) equally.
66 foreach ( $pendingDBs as $type => $dbs ) {
67 if ( in_array( $type, $types ) ) {
68 foreach ( $dbs as $db ) {
69 $candidates[] = array( $type, $db );
70 }
71 }
72 }
73 if ( !count( $candidates ) ) {
74 return; // no jobs for this type
75 }
76
77 list( $type, $db ) = $candidates[ mt_rand( 0, count( $candidates ) - 1 ) ];
78 if ( !$this->checkJob( $type, $db ) ) { // queue is actually empty?
79 $pendingDBs[$type] = array_diff( $pendingDBs[$type], $db );
80 JobQueueAggregator::singleton()->notifyQueueEmpty( $db, $type );
81 $again = true;
82 }
83 } while ( $again );
84
85 if ( $this->hasOption( 'types' ) ) {
86 $this->output( $db . " " . $type . "\n" );
87 } else {
88 $this->output( $db . "\n" );
89 }
90 }
91
92 /**
93 * Check if the specified database has a job of the specified type in it.
94 * The type may be false to indicate "all".
95 * @param $type string
96 * @param $dbName string
97 * @return bool
98 */
99 private function checkJob( $type, $dbName ) {
100 return !JobQueueGroup::singleton( $dbName )->get( $type )->isEmpty();
101 }
102
103 /**
104 * Do all ready periodic jobs for all databases every 5 minutes (and .1% of the time)
105 * @return integer
106 */
107 private function executeReadyPeriodicTasks() {
108 global $wgLocalDatabases, $wgMemc;
109
110 $count = 0;
111 $memcKey = 'jobqueue:periodic:lasttime';
112 $timestamp = (int)$wgMemc->get( $memcKey ); // UNIX timestamp or 0
113 if ( ( time() - $timestamp ) > 300 || mt_rand( 0, 999 ) == 0 ) { // 5 minutes
114 if ( $wgMemc->add( "$memcKey:rebuild", 1, 1800 ) ) { // lock
115 foreach ( $wgLocalDatabases as $db ) {
116 $count += JobQueueGroup::singleton( $db )->executeReadyPeriodicTasks();
117 }
118 $wgMemc->set( $memcKey, time() );
119 $wgMemc->delete( "$memcKey:rebuild" ); // unlock
120 }
121 }
122
123 return $count;
124 }
125 }
126
127 $maintClass = "nextJobDb";
128 require_once( RUN_MAINTENANCE_IF_MAIN );