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', "The type of job to search for", false, true );
38 public function execute() {
41 $type = $this->getOption( 'type', false );
43 $memcKey = 'jobqueue:dbs:v3';
44 $pendingDbInfo = $wgMemc->get( $memcKey );
46 // If the cache entry wasn't present, is stale, or in .1% of cases otherwise,
47 // regenerate the cache. Use any available stale cache if another process is
48 // currently regenerating the pending DB information.
49 if ( !is_array( $pendingDbInfo )
50 ||
( time() - $pendingDbInfo['timestamp'] ) > 300 // 5 minutes
51 ||
mt_rand( 0, 999 ) == 0
53 if ( $wgMemc->add( "$memcKey:rebuild", 1, 1800 ) ) { // lock
54 $pendingDbInfo = array(
55 'pendingDBs' => $this->getPendingDbs(),
58 for ( $attempts=1; $attempts <= 25; ++
$attempts ) {
59 if ( $wgMemc->add( "$memcKey:lock", 1, 60 ) ) { // lock
60 $wgMemc->set( $memcKey, $pendingDbInfo );
61 $wgMemc->delete( "$memcKey:lock" ); // unlock
65 $wgMemc->delete( "$memcKey:rebuild" ); // unlock
69 if ( !is_array( $pendingDbInfo ) ||
!$pendingDbInfo['pendingDBs'] ) {
70 return; // no DBs with jobs or cache is both empty and locked
73 $pendingDBs = $pendingDbInfo['pendingDBs']; // convenience
77 if ( $type === false ) {
78 $candidates = call_user_func_array( 'array_merge', $pendingDBs );
79 } elseif ( isset( $pendingDBs[$type] ) ) {
80 $candidates = $pendingDBs[$type];
82 $candidates = array();
88 $candidates = array_values( $candidates );
89 $db = $candidates[ mt_rand( 0, count( $candidates ) - 1 ) ];
90 if ( !$this->checkJob( $type, $db ) ) {
91 $pendingDBs = $this->delistDB( $pendingDBs, $db, $type );
92 // Update the cache to remove the outdated information.
93 // Make sure that this does not race (especially with full rebuilds).
94 if ( $wgMemc->add( "$memcKey:lock", 1, 60 ) ) { // lock
95 $curInfo = $wgMemc->get( $memcKey );
96 if ( is_array( $curInfo ) ) {
97 $curInfo['pendingDBs'] =
98 $this->delistDB( $curInfo['pendingDBs'], $db, $type );
99 $wgMemc->set( $memcKey, $curInfo );
100 // May as well make use of this newer information
101 $pendingDBs = $curInfo['pendingDBs'];
103 $wgMemc->delete( "$memcKey:lock" ); // unlock
109 $this->output( $db . "\n" );
112 private function delistDB( array $pendingDBs, $db, $type ) {
113 if ( $type === false ) {
114 // There are no jobs available in the current database
115 foreach ( $pendingDBs as $type2 => $dbs ) {
116 $pendingDBs[$type2] = array_diff( $pendingDBs[$type2], array( $db ) );
119 // There are no jobs of this type available in the current database
120 $pendingDBs[$type] = array_diff( $pendingDBs[$type], array( $db ) );
126 * Check if the specified database has a job of the specified type in it.
127 * The type may be false to indicate "all".
128 * @param $type string
129 * @param $dbName string
132 private function checkJob( $type, $dbName ) {
133 $group = JobQueueGroup
::singleton( $dbName );
134 if ( $type === false ) {
135 foreach ( $group->getDefaultQueueTypes() as $type ) {
136 if ( !$group->get( $type )->isEmpty() ) {
142 return !$group->get( $type )->isEmpty();
147 * Get all databases that have a pending job
150 private function getPendingDbs() {
151 global $wgLocalDatabases;
153 $pendingDBs = array(); // (job type => (db list))
154 foreach ( $wgLocalDatabases as $db ) {
155 $types = JobQueueGroup
::singleton( $db )->getQueuesWithJobs();
156 foreach ( $types as $type ) {
157 $pendingDBs[$type][] = $db;
165 $maintClass = "nextJobDb";
166 require_once( RUN_MAINTENANCE_IF_MAIN
);