Merge "Test table / list interaction"
[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 * @todo Make this work on PostgreSQL and maybe other database servers
22 * @ingroup Maintenance
23 */
24
25 require_once( __DIR__ . '/Maintenance.php' );
26
27 /**
28 * Maintenance script that picks a database that has pending jobs.
29 *
30 * @ingroup Maintenance
31 */
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', "The type of job to search for", false, true );
37 }
38
39 public function execute() {
40 global $wgMemc;
41
42 $type = $this->getOption( 'type', false );
43
44 $memcKey = 'jobqueue:dbs:v3';
45 $pendingDbInfo = $wgMemc->get( $memcKey );
46
47 // If the cache entry wasn't present, is stale, or in .1% of cases otherwise,
48 // regenerate the cache. Use any available stale cache if another process is
49 // currently regenerating the pending DB information.
50 if ( !is_array( $pendingDbInfo )
51 || ( time() - $pendingDbInfo['timestamp'] ) > 300 // 5 minutes
52 || mt_rand( 0, 999 ) == 0
53 ) {
54 if ( $wgMemc->add( "$memcKey:rebuild", 1, 1800 ) ) { // lock
55 $pendingDbInfo = array(
56 'pendingDBs' => $this->getPendingDbs(),
57 'timestamp' => time()
58 );
59 for ( $attempts=1; $attempts <= 25; ++$attempts ) {
60 if ( $wgMemc->add( "$memcKey:lock", 1, 60 ) ) { // lock
61 $wgMemc->set( $memcKey, $pendingDbInfo );
62 $wgMemc->delete( "$memcKey:lock" ); // unlock
63 break;
64 }
65 }
66 $wgMemc->delete( "$memcKey:rebuild" ); // unlock
67 }
68 }
69
70 if ( !is_array( $pendingDbInfo ) || !$pendingDbInfo['pendingDBs'] ) {
71 return; // no DBs with jobs or cache is both empty and locked
72 }
73
74 $pendingDBs = $pendingDbInfo['pendingDBs']; // convenience
75 do {
76 $again = false;
77
78 if ( $type === false ) {
79 $candidates = call_user_func_array( 'array_merge', $pendingDBs );
80 } elseif ( isset( $pendingDBs[$type] ) ) {
81 $candidates = $pendingDBs[$type];
82 } else {
83 $candidates = array();
84 }
85 if ( !$candidates ) {
86 return;
87 }
88
89 $candidates = array_values( $candidates );
90 $db = $candidates[ mt_rand( 0, count( $candidates ) - 1 ) ];
91 if ( !$this->checkJob( $type, $db ) ) {
92 if ( $type === false ) {
93 // There are no jobs available in the current database
94 foreach ( $pendingDBs as $type2 => $dbs ) {
95 $pendingDBs[$type2] = array_diff( $pendingDBs[$type2], array( $db ) );
96 }
97 } else {
98 // There are no jobs of this type available in the current database
99 $pendingDBs[$type] = array_diff( $pendingDBs[$type], array( $db ) );
100 }
101 // Update the cache to remove the outdated information.
102 // Make sure that this does not race (especially with full rebuilds).
103 $pendingDbInfo['pendingDBs'] = $pendingDBs;
104 if ( $wgMemc->add( "$memcKey:lock", 1, 60 ) ) { // lock
105 $curInfo = $wgMemc->get( $memcKey );
106 if ( $curInfo && $curInfo['timestamp'] === $pendingDbInfo['timestamp'] ) {
107 $wgMemc->set( $memcKey, $pendingDbInfo );
108 }
109 $wgMemc->delete( "$memcKey:lock" ); // unlock
110 }
111 $again = true;
112 }
113 } while ( $again );
114
115 $this->output( $db . "\n" );
116 }
117
118 /**
119 * Check if the specified database has a job of the specified type in it.
120 * The type may be false to indicate "all".
121 * @param $type string
122 * @param $dbName string
123 * @return bool
124 */
125 function checkJob( $type, $dbName ) {
126 $group = JobQueueGroup::singleton( $dbName );
127 if ( $type === false ) {
128 foreach ( $group->getDefaultQueueTypes() as $type ) {
129 if ( !$group->get( $type )->isEmpty() ) {
130 return true;
131 }
132 }
133 return false;
134 } else {
135 return !$group->get( $type )->isEmpty();
136 }
137 }
138
139 /**
140 * Get all databases that have a pending job
141 * @return array
142 */
143 private function getPendingDbs() {
144 global $wgLocalDatabases;
145
146 $pendingDBs = array(); // (job type => (db list))
147 foreach ( $wgLocalDatabases as $db ) {
148 $types = JobQueueGroup::singleton( $db )->getQueuesWithJobs();
149 foreach ( $types as $type ) {
150 $pendingDBs[$type][] = $db;
151 }
152 }
153
154 return $pendingDBs;
155 }
156 }
157
158 $maintClass = "nextJobDb";
159 require_once( RUN_MAINTENANCE_IF_MAIN );