Improved handling of pending queue delisting.
[lhc/web/wiklou.git] / maintenance / nextJobDB.php
1 <?php
2 /**
3 * Pick a database that has pending jobs
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once( __DIR__ . '/Maintenance.php' );
25
26 /**
27 * Maintenance script that picks a database that has pending jobs.
28 *
29 * @ingroup Maintenance
30 */
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 );
36 }
37
38 public function execute() {
39 global $wgMemc;
40
41 $type = $this->getOption( 'type', false );
42
43 $memcKey = 'jobqueue:dbs:v3';
44 $pendingDbInfo = $wgMemc->get( $memcKey );
45
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
52 ) {
53 if ( $wgMemc->add( "$memcKey:rebuild", 1, 1800 ) ) { // lock
54 $pendingDbInfo = array(
55 'pendingDBs' => $this->getPendingDbs(),
56 'timestamp' => time()
57 );
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
62 break;
63 }
64 }
65 $wgMemc->delete( "$memcKey:rebuild" ); // unlock
66 }
67 }
68
69 if ( !is_array( $pendingDbInfo ) || !$pendingDbInfo['pendingDBs'] ) {
70 return; // no DBs with jobs or cache is both empty and locked
71 }
72
73 $pendingDBs = $pendingDbInfo['pendingDBs']; // convenience
74 do {
75 $again = false;
76
77 if ( $type === false ) {
78 $candidates = call_user_func_array( 'array_merge', $pendingDBs );
79 } elseif ( isset( $pendingDBs[$type] ) ) {
80 $candidates = $pendingDBs[$type];
81 } else {
82 $candidates = array();
83 }
84 if ( !$candidates ) {
85 return;
86 }
87
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'];
102 }
103 $wgMemc->delete( "$memcKey:lock" ); // unlock
104 }
105 $again = true;
106 }
107 } while ( $again );
108
109 $this->output( $db . "\n" );
110 }
111
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 ) );
117 }
118 } else {
119 // There are no jobs of this type available in the current database
120 $pendingDBs[$type] = array_diff( $pendingDBs[$type], array( $db ) );
121 }
122 return $pendingDBs;
123 }
124
125 /**
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
130 * @return bool
131 */
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() ) {
137 return true;
138 }
139 }
140 return false;
141 } else {
142 return !$group->get( $type )->isEmpty();
143 }
144 }
145
146 /**
147 * Get all databases that have a pending job
148 * @return array
149 */
150 private function getPendingDbs() {
151 global $wgLocalDatabases;
152
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;
158 }
159 }
160
161 return $pendingDBs;
162 }
163 }
164
165 $maintClass = "nextJobDb";
166 require_once( RUN_MAINTENANCE_IF_MAIN );