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
21 * @ingroup Maintenance
24 require_once( __DIR__
. '/Maintenance.php' );
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 $wgMemc, $wgJobTypesExcludedFromDefaultQueue;
42 $type = false; // job type required/picked
44 if ( $this->hasOption( 'types' ) ) {
45 $types = explode( ' ', $this->getOption( 'types' ) );
46 } elseif ( $this->hasOption( 'type' ) ) {
47 $types = array( $this->getOption( 'type' ) );
52 // Handle any required periodic queue maintenance
53 $this->executeReadyPeriodicTasks();
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
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 ) {
69 ( is_array( $types ) && in_array( $type, $types ) ) ||
70 ( $types === false && !in_array( $type, $wgJobTypesExcludedFromDefaultQueue ) )
72 foreach ( $dbs as $db ) {
73 $candidates[] = array( $type, $db );
77 if ( !count( $candidates ) ) {
78 return; // no jobs for this type
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 );
89 if ( $this->hasOption( 'types' ) ) {
90 $this->output( $db . " " . $type . "\n" );
92 $this->output( $db . "\n" );
97 * Check if the specified database has a job of the specified type in it.
98 * The type may be false to indicate "all".
100 * @param $dbName string
103 private function checkJob( $type, $dbName ) {
104 return !JobQueueGroup
::singleton( $dbName )->get( $type )->isEmpty();
108 * Do all ready periodic jobs for all databases every 5 minutes (and .1% of the time)
111 private function executeReadyPeriodicTasks() {
112 global $wgLocalDatabases, $wgMemc;
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();
122 $wgMemc->set( $memcKey, time() );
123 $wgMemc->delete( "$memcKey:rebuild" ); // unlock
131 $maintClass = "nextJobDb";
132 require_once( RUN_MAINTENANCE_IF_MAIN
);