SearchResultSet: remove hasResults(), unused
[mediawiki.git] / maintenance / nextJobDB.php
blobd17236338c92a63aa0835b010ee4060613428219
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 $candidates = array(); // list of (type, db)
61 // Flatten the tree of candidates into a flat list so that a random
62 // item can be selected, weighing each queue (type/db tuple) equally.
63 foreach ( $pendingDBs as $type => $dbs ) {
64 if (
65 ( is_array( $types ) && in_array( $type, $types ) ) ||
66 ( $types === false && !in_array( $type, $wgJobTypesExcludedFromDefaultQueue ) )
67 ) {
68 foreach ( $dbs as $db ) {
69 $candidates[] = array( $type, $db );
73 if ( !count( $candidates ) ) {
74 return; // no jobs for this type
77 list( $type, $db ) = $candidates[mt_rand( 0, count( $candidates ) - 1 )];
79 if ( $this->hasOption( 'types' ) ) {
80 $this->output( $db . " " . $type . "\n" );
81 } else {
82 $this->output( $db . "\n" );
86 /**
87 * Do all ready periodic jobs for all databases every 5 minutes (and .1% of the time)
88 * @return int
90 private function executeReadyPeriodicTasks() {
91 global $wgLocalDatabases, $wgMemc;
93 $count = 0;
94 $memcKey = 'jobqueue:periodic:lasttime';
95 $timestamp = (int)$wgMemc->get( $memcKey ); // UNIX timestamp or 0
96 if ( ( time() - $timestamp ) > 300 || mt_rand( 0, 999 ) == 0 ) { // 5 minutes
97 if ( $wgMemc->add( "$memcKey:rebuild", 1, 1800 ) ) { // lock
98 foreach ( $wgLocalDatabases as $db ) {
99 $count += JobQueueGroup::singleton( $db )->executeReadyPeriodicTasks();
101 $wgMemc->set( $memcKey, time() );
102 $wgMemc->delete( "$memcKey:rebuild" ); // unlock
106 return $count;
110 $maintClass = "NextJobDb";
111 require_once RUN_MAINTENANCE_IF_MAIN;