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 * @todo Make this work on PostgreSQL and maybe other database servers
22 * @ingroup Maintenance
25 require_once( __DIR__
. '/Maintenance.php' );
28 * Maintenance script that picks a database that has pending jobs.
30 * @ingroup Maintenance
32 class nextJobDB
extends Maintenance
{
33 public function __construct() {
34 parent
::__construct();
35 $this->mDescription
= "Pick a database that has pending jobs";
36 $this->addOption( 'type', "Search by job type", false, true );
37 $this->addOption( 'types', "Space separated list of job types to search for", false, true );
40 public function execute() {
43 $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 $memcKey = 'jobqueue:dbs:v3';
53 $pendingDbInfo = $wgMemc->get( $memcKey );
55 // If the cache entry wasn't present, is stale, or in .1% of cases otherwise,
56 // regenerate the cache. Use any available stale cache if another process is
57 // currently regenerating the pending DB information.
58 if ( !is_array( $pendingDbInfo )
59 ||
( time() - $pendingDbInfo['timestamp'] ) > 300 // 5 minutes
60 ||
mt_rand( 0, 999 ) == 0
62 if ( $wgMemc->add( "$memcKey:rebuild", 1, 1800 ) ) { // lock
63 $pendingDbInfo = array(
64 'pendingDBs' => $this->getPendingDbs(),
67 for ( $attempts=1; $attempts <= 25; ++
$attempts ) {
68 if ( $wgMemc->add( "$memcKey:lock", 1, 60 ) ) { // lock
69 $wgMemc->set( $memcKey, $pendingDbInfo );
70 $wgMemc->delete( "$memcKey:lock" ); // unlock
74 $wgMemc->delete( "$memcKey:rebuild" ); // unlock
78 if ( !is_array( $pendingDbInfo ) ||
!$pendingDbInfo['pendingDBs'] ) {
79 return; // no DBs with jobs or cache is both empty and locked
82 $pendingDBs = $pendingDbInfo['pendingDBs']; // convenience
86 if ( $types === false ) {
87 $candidates = call_user_func_array( 'array_merge', $pendingDBs );
89 $candidates = array();
90 $possTypes = array_intersect( $types, array_keys( $pendingDBs ) );
92 $possTypes = array_values( $possTypes );
93 $type = $possTypes[ mt_rand( 0, count( $possTypes ) - 1 ) ];
94 $candidates = $pendingDBs[$type];
98 return; // no jobs for this type
101 $candidates = array_values( $candidates );
102 $db = $candidates[ mt_rand( 0, count( $candidates ) - 1 ) ];
103 if ( !$this->checkJob( $type, $db ) ) {
104 if ( $type === false ) {
105 // There are no jobs available in the current database
106 foreach ( $pendingDBs as $type2 => $dbs ) {
107 $pendingDBs[$type2] = array_diff( $pendingDBs[$type2], array( $db ) );
110 // There are no jobs of this type available in the current database
111 $pendingDBs[$type] = array_diff( $pendingDBs[$type], array( $db ) );
113 // Update the cache to remove the outdated information.
114 // Make sure that this does not race (especially with full rebuilds).
115 $pendingDbInfo['pendingDBs'] = $pendingDBs;
116 if ( $wgMemc->add( "$memcKey:lock", 1, 60 ) ) { // lock
117 $curInfo = $wgMemc->get( $memcKey );
118 if ( $curInfo && $curInfo['timestamp'] === $pendingDbInfo['timestamp'] ) {
119 $wgMemc->set( $memcKey, $pendingDbInfo );
121 $wgMemc->delete( "$memcKey:lock" ); // unlock
127 if ( $this->hasOption( 'types' ) ) {
128 $this->output( $db . " " . $type . "\n" );
130 $this->output( $db . "\n" );
135 * Check if the specified database has a job of the specified type in it.
136 * The type may be false to indicate "all".
137 * @param $type string
138 * @param $dbName string
141 function checkJob( $type, $dbName ) {
142 $group = JobQueueGroup
::singleton( $dbName );
143 if ( $type === false ) {
144 foreach ( $group->getDefaultQueueTypes() as $type ) {
145 if ( !$group->get( $type )->isEmpty() ) {
151 return !$group->get( $type )->isEmpty();
156 * Get all databases that have a pending job
159 private function getPendingDbs() {
160 global $wgLocalDatabases;
162 $pendingDBs = array(); // (job type => (db list))
163 foreach ( $wgLocalDatabases as $db ) {
164 $types = JobQueueGroup
::singleton( $db )->getQueuesWithJobs();
165 foreach ( $types as $type ) {
166 $pendingDBs[$type][] = $db;
174 $maintClass = "nextJobDb";
175 require_once( RUN_MAINTENANCE_IF_MAIN
);