Merge "Make line breaks in <blockquote> behave like <div> (bug 6200)."
[mediawiki.git] / maintenance / nextJobDB.php
blob219b5d8e573a821678548bf308a10663d1f3ebdc
1 <?php
2 /**
3 * Pick a database that has pending jobs
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Maintenance script that picks a database that has pending jobs.
29 * @ingroup Maintenance
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 );
39 public function execute() {
40 global $wgJobTypesExcludedFromDefaultQueue;
42 // 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 = false;
51 // Handle any required periodic queue maintenance
52 $this->executeReadyPeriodicTasks();
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
60 do {
61 $again = false;
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 (
68 ( is_array( $types ) && in_array( $type, $types ) ) ||
69 ( $types === false && !in_array( $type, $wgJobTypesExcludedFromDefaultQueue ) )
70 ) {
71 foreach ( $dbs as $db ) {
72 $candidates[] = array( $type, $db );
76 if ( !count( $candidates ) ) {
77 return; // no jobs for this type
80 list( $type, $db ) = $candidates[mt_rand( 0, count( $candidates ) - 1 )];
81 if ( JobQueueGroup::singleton( $db )->isQueueDeprioritized( $type ) ) {
82 $pendingDBs[$type] = array_diff( $pendingDBs[$type], array( $db ) );
83 $again = true;
85 } while ( $again );
87 if ( $this->hasOption( 'types' ) ) {
88 $this->output( $db . " " . $type . "\n" );
89 } else {
90 $this->output( $db . "\n" );
94 /**
95 * Do all ready periodic jobs for all databases every 5 minutes (and .1% of the time)
96 * @return integer
98 private function executeReadyPeriodicTasks() {
99 global $wgLocalDatabases, $wgMemc;
101 $count = 0;
102 $memcKey = 'jobqueue:periodic:lasttime';
103 $timestamp = (int)$wgMemc->get( $memcKey ); // UNIX timestamp or 0
104 if ( ( time() - $timestamp ) > 300 || mt_rand( 0, 999 ) == 0 ) { // 5 minutes
105 if ( $wgMemc->add( "$memcKey:rebuild", 1, 1800 ) ) { // lock
106 foreach ( $wgLocalDatabases as $db ) {
107 $count += JobQueueGroup::singleton( $db )->executeReadyPeriodicTasks();
109 $wgMemc->set( $memcKey, time() );
110 $wgMemc->delete( "$memcKey:rebuild" ); // unlock
114 return $count;
118 $maintClass = "nextJobDb";
119 require_once RUN_MAINTENANCE_IF_MAIN;