Merge "Add pp_propname_page index to page_props"
[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, $wgJobTypesExcludedFromDefaultQueue;
41
42 $type = false; // job type required/picked
43
44 if ( $this->hasOption( 'types' ) ) {
45 $types = explode( ' ', $this->getOption( 'types' ) );
46 } elseif ( $this->hasOption( 'type' ) ) {
47 $types = array( $this->getOption( 'type' ) );
48 } else {
49 $types = false;
50 }
51
52 // Handle any required periodic queue maintenance
53 $this->executeReadyPeriodicTasks();
54
55 // Get all the queues with jobs in them
56 $pendingDBs = JobQueueAggregator::singleton()->getAllReadyWikiQueues();
57 if ( !count( $pendingDBs ) ) {
58 return; // no DBs with jobs or cache is both empty and locked
59 }
60
61 do {
62 $again = false;
63
64 $candidates = array(); // list of (type, db)
65 // Flatten the tree of candidates into a flat list so that a random
66 // item can be selected, weighing each queue (type/db tuple) equally.
67 foreach ( $pendingDBs as $type => $dbs ) {
68 if (
69 ( is_array( $types ) && in_array( $type, $types ) ) ||
70 ( $types === false && !in_array( $type, $wgJobTypesExcludedFromDefaultQueue ) )
71 ) {
72 foreach ( $dbs as $db ) {
73 $candidates[] = array( $type, $db );
74 }
75 }
76 }
77 if ( !count( $candidates ) ) {
78 return; // no jobs for this type
79 }
80
81 list( $type, $db ) = $candidates[ mt_rand( 0, count( $candidates ) - 1 ) ];
82 if ( !$this->checkJob( $type, $db ) ) { // queue is actually empty?
83 $pendingDBs[$type] = array_diff( $pendingDBs[$type], $db );
84 JobQueueAggregator::singleton()->notifyQueueEmpty( $db, $type );
85 $again = true;
86 }
87 } while ( $again );
88
89 if ( $this->hasOption( 'types' ) ) {
90 $this->output( $db . " " . $type . "\n" );
91 } else {
92 $this->output( $db . "\n" );
93 }
94 }
95
96 /**
97 * Check if the specified database has a job of the specified type in it.
98 * The type may be false to indicate "all".
99 * @param $type string
100 * @param $dbName string
101 * @return bool
102 */
103 private function checkJob( $type, $dbName ) {
104 return !JobQueueGroup::singleton( $dbName )->get( $type )->isEmpty();
105 }
106
107 /**
108 * Do all ready periodic jobs for all databases every 5 minutes (and .1% of the time)
109 * @return integer
110 */
111 private function executeReadyPeriodicTasks() {
112 global $wgLocalDatabases, $wgMemc;
113
114 $count = 0;
115 $memcKey = 'jobqueue:periodic:lasttime';
116 $timestamp = (int)$wgMemc->get( $memcKey ); // UNIX timestamp or 0
117 if ( ( time() - $timestamp ) > 300 || mt_rand( 0, 999 ) == 0 ) { // 5 minutes
118 if ( $wgMemc->add( "$memcKey:rebuild", 1, 1800 ) ) { // lock
119 foreach ( $wgLocalDatabases as $db ) {
120 $count += JobQueueGroup::singleton( $db )->executeReadyPeriodicTasks();
121 }
122 $wgMemc->set( $memcKey, time() );
123 $wgMemc->delete( "$memcKey:rebuild" ); // unlock
124 }
125 }
126
127 return $count;
128 }
129 }
130
131 $maintClass = "nextJobDb";
132 require_once( RUN_MAINTENANCE_IF_MAIN );