6 * @defgroup JobQueue JobQueue
9 if ( !defined( 'MEDIAWIKI' ) ) {
10 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
14 * Class to both describe a background job and handle jobs.
31 /*-------------------------------------------------------------------------
33 *------------------------------------------------------------------------*/
37 * @return boolean success
39 abstract function run();
41 /*-------------------------------------------------------------------------
43 *------------------------------------------------------------------------*/
46 * Pop a job of a certain type. This tries less hard than pop() to
47 * actually find a job; it may be adversely affected by concurrent job
54 static function pop_type( $type ) {
55 wfProfilein( __METHOD__
);
57 $dbw = wfGetDB( DB_MASTER
);
59 $row = $dbw->selectRow(
62 array( 'job_cmd' => $type ),
67 if ( $row === false ) {
68 wfProfileOut( __METHOD__
);
72 /* Ensure we "own" this row */
73 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
74 $affected = $dbw->affectedRows();
77 if ( $affected == 0 ) {
78 wfProfileOut( __METHOD__
);
82 wfIncrStats( 'job-pop' );
83 $namespace = $row->job_namespace
;
84 $dbkey = $row->job_title
;
85 $title = Title
::makeTitleSafe( $namespace, $dbkey );
86 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
),
89 $job->removeDuplicates();
91 wfProfileOut( __METHOD__
);
96 * Pop a job off the front of the queue
98 * @param $offset Integer: Number of jobs to skip
99 * @return Job or false if there's no jobs
101 static function pop( $offset = 0 ) {
102 global $wgJobTypesExcludedFromDefaultQueue;
103 wfProfileIn( __METHOD__
);
105 $dbr = wfGetDB( DB_SLAVE
);
107 /* Get a job from the slave, start with an offset,
108 scan full set afterwards, avoid hitting purged rows
110 NB: If random fetch previously was used, offset
111 will always be ahead of few entries
113 $conditions = array();
114 if ( count( $wgJobTypesExcludedFromDefaultQueue ) != 0 ) {
115 foreach ( $wgJobTypesExcludedFromDefaultQueue as $cmdType ) {
116 $conditions[] = "job_cmd != " . $dbr->addQuotes( $cmdType );
119 $offset = intval( $offset );
120 $row = $dbr->selectRow( 'job', '*', array_merge( $conditions, array( "job_id >= $offset" ) ) , __METHOD__
,
121 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 )
124 // Refetching without offset is needed as some of job IDs could have had delayed commits
125 // and have lower IDs than jobs already executed, blame concurrency :)
127 if ( $row === false ) {
128 if ( $offset != 0 ) {
129 $row = $dbr->selectRow( 'job', '*', $conditions, __METHOD__
,
130 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) );
133 if ( $row === false ) {
134 wfProfileOut( __METHOD__
);
139 // Try to delete it from the master
140 $dbw = wfGetDB( DB_MASTER
);
141 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
142 $affected = $dbw->affectedRows();
146 // Failed, someone else beat us to it
147 // Try getting a random row
148 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
149 'MAX(job_id) as maxjob' ), '1=1', __METHOD__
);
150 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
152 wfProfileOut( __METHOD__
);
155 // Get the random row
156 $row = $dbw->selectRow( 'job', '*',
157 'job_id >= ' . mt_rand( $row->minjob
, $row->maxjob
), __METHOD__
);
158 if ( $row === false ) {
159 // Random job gone before we got the chance to select it
161 wfProfileOut( __METHOD__
);
164 // Delete the random row
165 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
166 $affected = $dbw->affectedRows();
170 // Random job gone before we exclusively deleted it
172 wfProfileOut( __METHOD__
);
177 // If execution got to here, there's a row in $row that has been deleted from the database
178 // by this thread. Hence the concurrent pop was successful.
179 wfIncrStats( 'job-pop' );
180 $namespace = $row->job_namespace
;
181 $dbkey = $row->job_title
;
182 $title = Title
::makeTitleSafe( $namespace, $dbkey );
183 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
185 // Remove any duplicates it may have later in the queue
186 $job->removeDuplicates();
188 wfProfileOut( __METHOD__
);
193 * Create the appropriate object to handle a specific job
195 * @param $command String: Job command
196 * @param $title Title: Associated title
197 * @param $params Array: Job parameters
198 * @param $id Int: Job identifier
201 static function factory( $command, $title, $params = false, $id = 0 ) {
202 global $wgJobClasses;
203 if( isset( $wgJobClasses[$command] ) ) {
204 $class = $wgJobClasses[$command];
205 return new $class( $title, $params, $id );
207 throw new MWException( "Invalid job command `{$command}`" );
210 static function makeBlob( $params ) {
211 if ( $params !== false ) {
212 return serialize( $params );
218 static function extractBlob( $blob ) {
219 if ( (string)$blob !== '' ) {
220 return unserialize( $blob );
227 * Batch-insert a group of jobs into the queue.
228 * This will be wrapped in a transaction with a forced commit.
230 * This may add duplicate at insert time, but they will be
231 * removed later on, when the first one is popped.
233 * @param $jobs array of Job objects
235 static function batchInsert( $jobs ) {
236 if ( !count( $jobs ) ) {
239 $dbw = wfGetDB( DB_MASTER
);
241 foreach ( $jobs as $job ) {
242 $rows[] = $job->insertFields();
243 if ( count( $rows ) >= 50 ) {
244 # Do a small transaction to avoid slave lag
246 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
251 if ( $rows ) { // last chunk
253 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
256 wfIncrStats( 'job-insert', count( $jobs ) );
260 * Insert a group of jobs into the queue.
262 * Same as batchInsert() but does not commit and can thus
263 * be rolled-back as part of a larger transaction. However,
264 * large batches of jobs can cause slave lag.
266 * @param $jobs array of Job objects
268 static function safeBatchInsert( $jobs ) {
269 if ( !count( $jobs ) ) {
272 $dbw = wfGetDB( DB_MASTER
);
274 foreach ( $jobs as $job ) {
275 $rows[] = $job->insertFields();
276 if ( count( $rows ) >= 500 ) {
277 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
281 if ( $rows ) { // last chunk
282 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
284 wfIncrStats( 'job-insert', count( $jobs ) );
287 /*-------------------------------------------------------------------------
288 * Non-static functions
289 *------------------------------------------------------------------------*/
294 * @param $params array
297 function __construct( $command, $title, $params = false, $id = 0 ) {
298 $this->command
= $command;
299 $this->title
= $title;
300 $this->params
= $params;
303 // A bit of premature generalisation
304 // Oh well, the whole class is premature generalisation really
305 $this->removeDuplicates
= true;
309 * Insert a single job into the queue.
310 * @return bool true on success
313 $fields = $this->insertFields();
315 $dbw = wfGetDB( DB_MASTER
);
317 if ( $this->removeDuplicates
) {
318 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__
);
319 if ( $dbw->numRows( $res ) ) {
323 wfIncrStats( 'job-insert' );
324 return $dbw->insert( 'job', $fields, __METHOD__
);
327 protected function insertFields() {
328 $dbw = wfGetDB( DB_MASTER
);
330 'job_id' => $dbw->nextSequenceValue( 'job_job_id_seq' ),
331 'job_cmd' => $this->command
,
332 'job_namespace' => $this->title
->getNamespace(),
333 'job_title' => $this->title
->getDBkey(),
334 'job_params' => Job
::makeBlob( $this->params
)
339 * Remove jobs in the job queue which are duplicates of this job.
340 * This is deadlock-prone and so starts its own transaction.
342 function removeDuplicates() {
343 if ( !$this->removeDuplicates
) {
347 $fields = $this->insertFields();
348 unset( $fields['job_id'] );
349 $dbw = wfGetDB( DB_MASTER
);
351 $dbw->delete( 'job', $fields, __METHOD__
);
352 $affected = $dbw->affectedRows();
355 wfIncrStats( 'job-dup-delete', $affected );
359 function toString() {
361 if ( $this->params
) {
362 foreach ( $this->params
as $key => $value ) {
363 if ( $paramString != '' ) {
366 $paramString .= "$key=$value";
370 if ( is_object( $this->title
) ) {
371 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
372 if ( $paramString !== '' ) {
373 $s .= ' ' . $paramString;
377 return "{$this->command} $paramString";
381 protected function setLastError( $error ) {
382 $this->error
= $error;
385 function getLastError() {