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 * @defgroup JobQueue JobQueue
25 * Class to both describe a background job and handle jobs.
42 /*-------------------------------------------------------------------------
44 *------------------------------------------------------------------------*/
48 * @return boolean success
50 abstract function run();
52 /*-------------------------------------------------------------------------
54 *------------------------------------------------------------------------*/
57 * Pop a job of a certain type. This tries less hard than pop() to
58 * actually find a job; it may be adversely affected by concurrent job
65 static function pop_type( $type ) {
66 wfProfilein( __METHOD__
);
68 $dbw = wfGetDB( DB_MASTER
);
70 $dbw->begin( __METHOD__
);
72 $row = $dbw->selectRow(
75 array( 'job_cmd' => $type ),
77 array( 'LIMIT' => 1, 'FOR UPDATE' )
80 if ( $row === false ) {
81 $dbw->commit( __METHOD__
);
82 wfProfileOut( __METHOD__
);
86 /* Ensure we "own" this row */
87 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
88 $affected = $dbw->affectedRows();
89 $dbw->commit( __METHOD__
);
91 if ( $affected == 0 ) {
92 wfProfileOut( __METHOD__
);
96 wfIncrStats( 'job-pop' );
97 $namespace = $row->job_namespace
;
98 $dbkey = $row->job_title
;
99 $title = Title
::makeTitleSafe( $namespace, $dbkey );
100 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
),
103 $job->removeDuplicates();
105 wfProfileOut( __METHOD__
);
110 * Pop a job off the front of the queue
112 * @param $offset Integer: Number of jobs to skip
113 * @return Job or false if there's no jobs
115 static function pop( $offset = 0 ) {
116 wfProfileIn( __METHOD__
);
118 $dbr = wfGetDB( DB_SLAVE
);
120 /* Get a job from the slave, start with an offset,
121 scan full set afterwards, avoid hitting purged rows
123 NB: If random fetch previously was used, offset
124 will always be ahead of few entries
127 $conditions = self
::defaultQueueConditions();
129 $offset = intval( $offset );
130 $options = array( 'ORDER BY' => 'job_id', 'USE INDEX' => 'PRIMARY' );
132 $row = $dbr->selectRow( 'job', '*',
133 array_merge( $conditions, array( "job_id >= $offset" ) ),
138 // Refetching without offset is needed as some of job IDs could have had delayed commits
139 // and have lower IDs than jobs already executed, blame concurrency :)
141 if ( $row === false ) {
142 if ( $offset != 0 ) {
143 $row = $dbr->selectRow( 'job', '*', $conditions, __METHOD__
, $options );
146 if ( $row === false ) {
147 wfProfileOut( __METHOD__
);
152 // Try to delete it from the master
153 $dbw = wfGetDB( DB_MASTER
);
154 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
155 $affected = $dbw->affectedRows();
156 $dbw->commit( __METHOD__
);
159 // Failed, someone else beat us to it
160 // Try getting a random row
161 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
162 'MAX(job_id) as maxjob' ), '1=1', __METHOD__
);
163 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
165 wfProfileOut( __METHOD__
);
168 // Get the random row
169 $row = $dbw->selectRow( 'job', '*',
170 'job_id >= ' . mt_rand( $row->minjob
, $row->maxjob
), __METHOD__
);
171 if ( $row === false ) {
172 // Random job gone before we got the chance to select it
174 wfProfileOut( __METHOD__
);
177 // Delete the random row
178 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
179 $affected = $dbw->affectedRows();
180 $dbw->commit( __METHOD__
);
183 // Random job gone before we exclusively deleted it
185 wfProfileOut( __METHOD__
);
190 // If execution got to here, there's a row in $row that has been deleted from the database
191 // by this thread. Hence the concurrent pop was successful.
192 wfIncrStats( 'job-pop' );
193 $namespace = $row->job_namespace
;
194 $dbkey = $row->job_title
;
195 $title = Title
::makeTitleSafe( $namespace, $dbkey );
197 if ( is_null( $title ) ) {
198 wfProfileOut( __METHOD__
);
202 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
204 // Remove any duplicates it may have later in the queue
205 $job->removeDuplicates();
207 wfProfileOut( __METHOD__
);
212 * Create the appropriate object to handle a specific job
214 * @param $command String: Job command
215 * @param $title Title: Associated title
216 * @param $params Array: Job parameters
217 * @param $id Int: Job identifier
220 static function factory( $command, Title
$title, $params = false, $id = 0 ) {
221 global $wgJobClasses;
222 if( isset( $wgJobClasses[$command] ) ) {
223 $class = $wgJobClasses[$command];
224 return new $class( $title, $params, $id );
226 throw new MWException( "Invalid job command `{$command}`" );
233 static function makeBlob( $params ) {
234 if ( $params !== false ) {
235 return serialize( $params );
245 static function extractBlob( $blob ) {
246 if ( (string)$blob !== '' ) {
247 return unserialize( $blob );
254 * Batch-insert a group of jobs into the queue.
255 * This will be wrapped in a transaction with a forced commit.
257 * This may add duplicate at insert time, but they will be
258 * removed later on, when the first one is popped.
260 * @param $jobs array of Job objects
262 static function batchInsert( $jobs ) {
263 if ( !count( $jobs ) ) {
266 $dbw = wfGetDB( DB_MASTER
);
272 foreach ( $jobs as $job ) {
273 $rows[] = $job->insertFields();
274 if ( count( $rows ) >= 50 ) {
275 # Do a small transaction to avoid slave lag
276 $dbw->begin( __METHOD__
);
277 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
278 $dbw->commit( __METHOD__
);
282 if ( $rows ) { // last chunk
283 $dbw->begin( __METHOD__
);
284 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
285 $dbw->commit( __METHOD__
);
287 wfIncrStats( 'job-insert', count( $jobs ) );
291 * Insert a group of jobs into the queue.
293 * Same as batchInsert() but does not commit and can thus
294 * be rolled-back as part of a larger transaction. However,
295 * large batches of jobs can cause slave lag.
297 * @param $jobs array of Job objects
299 static function safeBatchInsert( $jobs ) {
300 if ( !count( $jobs ) ) {
303 $dbw = wfGetDB( DB_MASTER
);
305 foreach ( $jobs as $job ) {
306 $rows[] = $job->insertFields();
307 if ( count( $rows ) >= 500 ) {
308 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
312 if ( $rows ) { // last chunk
313 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
315 wfIncrStats( 'job-insert', count( $jobs ) );
320 * SQL conditions to apply on most JobQueue queries
322 * Whenever we exclude jobs types from the default queue, we want to make
323 * sure that queries to the job queue actually ignore them.
325 * @return array SQL conditions suitable for Database:: methods
327 static function defaultQueueConditions( ) {
328 global $wgJobTypesExcludedFromDefaultQueue;
329 $conditions = array();
330 if ( count( $wgJobTypesExcludedFromDefaultQueue ) > 0 ) {
331 $dbr = wfGetDB( DB_SLAVE
);
332 foreach ( $wgJobTypesExcludedFromDefaultQueue as $cmdType ) {
333 $conditions[] = "job_cmd != " . $dbr->addQuotes( $cmdType );
339 /*-------------------------------------------------------------------------
340 * Non-static functions
341 *------------------------------------------------------------------------*/
346 * @param $params array
349 function __construct( $command, $title, $params = false, $id = 0 ) {
350 $this->command
= $command;
351 $this->title
= $title;
352 $this->params
= $params;
355 // A bit of premature generalisation
356 // Oh well, the whole class is premature generalisation really
357 $this->removeDuplicates
= true;
361 * Insert a single job into the queue.
362 * @return bool true on success
365 $fields = $this->insertFields();
367 $dbw = wfGetDB( DB_MASTER
);
369 if ( $this->removeDuplicates
) {
370 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__
);
371 if ( $dbw->numRows( $res ) ) {
375 wfIncrStats( 'job-insert' );
376 return $dbw->insert( 'job', $fields, __METHOD__
);
382 protected function insertFields() {
383 $dbw = wfGetDB( DB_MASTER
);
385 'job_id' => $dbw->nextSequenceValue( 'job_job_id_seq' ),
386 'job_cmd' => $this->command
,
387 'job_namespace' => $this->title
->getNamespace(),
388 'job_title' => $this->title
->getDBkey(),
389 'job_timestamp' => $dbw->timestamp(),
390 'job_params' => Job
::makeBlob( $this->params
)
395 * Remove jobs in the job queue which are duplicates of this job.
396 * This is deadlock-prone and so starts its own transaction.
398 function removeDuplicates() {
399 if ( !$this->removeDuplicates
) {
403 $fields = $this->insertFields();
404 unset( $fields['job_id'] );
405 $dbw = wfGetDB( DB_MASTER
);
406 $dbw->begin( __METHOD__
);
407 $dbw->delete( 'job', $fields, __METHOD__
);
408 $affected = $dbw->affectedRows();
409 $dbw->commit( __METHOD__
);
411 wfIncrStats( 'job-dup-delete', $affected );
418 function toString() {
420 if ( $this->params
) {
421 foreach ( $this->params
as $key => $value ) {
422 if ( $paramString != '' ) {
425 $paramString .= "$key=$value";
429 if ( is_object( $this->title
) ) {
430 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
431 if ( $paramString !== '' ) {
432 $s .= ' ' . $paramString;
436 return "{$this->command} $paramString";
440 protected function setLastError( $error ) {
441 $this->error
= $error;
444 function getLastError() {