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
50 static function pop_type( $type ) {
51 wfProfilein( __METHOD__
);
53 $dbw = wfGetDB( DB_MASTER
);
55 $row = $dbw->selectRow(
58 array( 'job_cmd' => $type ),
63 if ( $row === false ) {
64 wfProfileOut( __METHOD__
);
68 /* Ensure we "own" this row */
69 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
70 $affected = $dbw->affectedRows();
73 if ( $affected == 0 ) {
74 wfProfileOut( __METHOD__
);
78 wfIncrStats( 'job-pop' );
79 $namespace = $row->job_namespace
;
80 $dbkey = $row->job_title
;
81 $title = Title
::makeTitleSafe( $namespace, $dbkey );
82 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
),
85 $job->removeDuplicates();
87 wfProfileOut( __METHOD__
);
92 * Pop a job off the front of the queue
94 * @param $offset Integer: Number of jobs to skip
95 * @return Job or false if there's no jobs
97 static function pop( $offset = 0 ) {
98 global $wgJobTypesExcludedFromDefaultQueue;
99 wfProfileIn( __METHOD__
);
101 $dbr = wfGetDB( DB_SLAVE
);
103 /* Get a job from the slave, start with an offset,
104 scan full set afterwards, avoid hitting purged rows
106 NB: If random fetch previously was used, offset
107 will always be ahead of few entries
109 $conditions = array();
110 if ( count( $wgJobTypesExcludedFromDefaultQueue ) != 0 ) {
111 foreach ( $wgJobTypesExcludedFromDefaultQueue as $cmdType ) {
112 $conditions[] = "job_cmd != " . $dbr->addQuotes( $cmdType );
115 $offset = intval( $offset );
116 $row = $dbr->selectRow( 'job', '*', array_merge( $conditions, array( "job_id >= $offset" ) ) , __METHOD__
,
117 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 )
120 // Refetching without offset is needed as some of job IDs could have had delayed commits
121 // and have lower IDs than jobs already executed, blame concurrency :)
123 if ( $row === false ) {
124 if ( $offset != 0 ) {
125 $row = $dbr->selectRow( 'job', '*', $conditions, __METHOD__
,
126 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) );
129 if ( $row === false ) {
130 wfProfileOut( __METHOD__
);
135 // Try to delete it from the master
136 $dbw = wfGetDB( DB_MASTER
);
137 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
138 $affected = $dbw->affectedRows();
142 // Failed, someone else beat us to it
143 // Try getting a random row
144 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
145 'MAX(job_id) as maxjob' ), '1=1', __METHOD__
);
146 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
148 wfProfileOut( __METHOD__
);
151 // Get the random row
152 $row = $dbw->selectRow( 'job', '*',
153 'job_id >= ' . mt_rand( $row->minjob
, $row->maxjob
), __METHOD__
);
154 if ( $row === false ) {
155 // Random job gone before we got the chance to select it
157 wfProfileOut( __METHOD__
);
160 // Delete the random row
161 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
162 $affected = $dbw->affectedRows();
166 // Random job gone before we exclusively deleted it
168 wfProfileOut( __METHOD__
);
173 // If execution got to here, there's a row in $row that has been deleted from the database
174 // by this thread. Hence the concurrent pop was successful.
175 wfIncrStats( 'job-pop' );
176 $namespace = $row->job_namespace
;
177 $dbkey = $row->job_title
;
178 $title = Title
::makeTitleSafe( $namespace, $dbkey );
179 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
181 // Remove any duplicates it may have later in the queue
182 $job->removeDuplicates();
184 wfProfileOut( __METHOD__
);
189 * Create the appropriate object to handle a specific job
191 * @param $command String: Job command
192 * @param $title Title: Associated title
193 * @param $params Array: Job parameters
194 * @param $id Int: Job identifier
197 static function factory( $command, $title, $params = false, $id = 0 ) {
198 global $wgJobClasses;
199 if( isset( $wgJobClasses[$command] ) ) {
200 $class = $wgJobClasses[$command];
201 return new $class( $title, $params, $id );
203 throw new MWException( "Invalid job command `{$command}`" );
206 static function makeBlob( $params ) {
207 if ( $params !== false ) {
208 return serialize( $params );
214 static function extractBlob( $blob ) {
215 if ( (string)$blob !== '' ) {
216 return unserialize( $blob );
223 * Batch-insert a group of jobs into the queue.
224 * This will be wrapped in a transaction with a forced commit.
226 * This may add duplicate at insert time, but they will be
227 * removed later on, when the first one is popped.
229 * @param $jobs array of Job objects
231 static function batchInsert( $jobs ) {
232 if ( !count( $jobs ) ) {
235 $dbw = wfGetDB( DB_MASTER
);
237 foreach ( $jobs as $job ) {
238 $rows[] = $job->insertFields();
239 if ( count( $rows ) >= 50 ) {
240 # Do a small transaction to avoid slave lag
242 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
247 if ( $rows ) { // last chunk
249 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
252 wfIncrStats( 'job-insert', count( $jobs ) );
256 * Insert a group of jobs into the queue.
258 * Same as batchInsert() but does not commit and can thus
259 * be rolled-back as part of a larger transaction. However,
260 * large batches of jobs can cause slave lag.
262 * @param $jobs array of Job objects
264 static function safeBatchInsert( $jobs ) {
265 if ( !count( $jobs ) ) {
268 $dbw = wfGetDB( DB_MASTER
);
270 foreach ( $jobs as $job ) {
271 $rows[] = $job->insertFields();
272 if ( count( $rows ) >= 500 ) {
273 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
277 if ( $rows ) { // last chunk
278 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
280 wfIncrStats( 'job-insert', count( $jobs ) );
283 /*-------------------------------------------------------------------------
284 * Non-static functions
285 *------------------------------------------------------------------------*/
290 * @param $params array
293 function __construct( $command, $title, $params = false, $id = 0 ) {
294 $this->command
= $command;
295 $this->title
= $title;
296 $this->params
= $params;
299 // A bit of premature generalisation
300 // Oh well, the whole class is premature generalisation really
301 $this->removeDuplicates
= true;
305 * Insert a single job into the queue.
306 * @return bool true on success
309 $fields = $this->insertFields();
311 $dbw = wfGetDB( DB_MASTER
);
313 if ( $this->removeDuplicates
) {
314 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__
);
315 if ( $dbw->numRows( $res ) ) {
319 wfIncrStats( 'job-insert' );
320 return $dbw->insert( 'job', $fields, __METHOD__
);
323 protected function insertFields() {
324 $dbw = wfGetDB( DB_MASTER
);
326 'job_id' => $dbw->nextSequenceValue( 'job_job_id_seq' ),
327 'job_cmd' => $this->command
,
328 'job_namespace' => $this->title
->getNamespace(),
329 'job_title' => $this->title
->getDBkey(),
330 'job_params' => Job
::makeBlob( $this->params
)
335 * Remove jobs in the job queue which are duplicates of this job.
336 * This is deadlock-prone and so starts its own transaction.
338 function removeDuplicates() {
339 if ( !$this->removeDuplicates
) {
343 $fields = $this->insertFields();
344 unset( $fields['job_id'] );
345 $dbw = wfGetDB( DB_MASTER
);
347 $dbw->delete( 'job', $fields, __METHOD__
);
348 $affected = $dbw->affectedRows();
351 wfIncrStats( 'job-dup-delete', $affected );
355 function toString() {
357 if ( $this->params
) {
358 foreach ( $this->params
as $key => $value ) {
359 if ( $paramString != '' ) {
362 $paramString .= "$key=$value";
366 if ( is_object( $this->title
) ) {
367 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
368 if ( $paramString !== '' ) {
369 $s .= ' ' . $paramString;
373 return "{$this->command} $paramString";
377 protected function setLastError( $error ) {
378 $this->error
= $error;
381 function getLastError() {