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 wfProfileIn( __METHOD__
);
100 $dbr = wfGetDB( DB_SLAVE
);
102 /* Get a job from the slave, start with an offset,
103 scan full set afterwards, avoid hitting purged rows
105 NB: If random fetch previously was used, offset
106 will always be ahead of few entries
109 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__
,
110 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) );
112 // Refetching without offset is needed as some of job IDs could have had delayed commits
113 // and have lower IDs than jobs already executed, blame concurrency :)
115 if ( $row === false ) {
116 if ( $offset != 0 ) {
117 $row = $dbr->selectRow( 'job', '*', '', __METHOD__
,
118 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) );
121 if ( $row === false ) {
122 wfProfileOut( __METHOD__
);
127 // Try to delete it from the master
128 $dbw = wfGetDB( DB_MASTER
);
129 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
130 $affected = $dbw->affectedRows();
134 // Failed, someone else beat us to it
135 // Try getting a random row
136 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
137 'MAX(job_id) as maxjob' ), '1=1', __METHOD__
);
138 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
140 wfProfileOut( __METHOD__
);
143 // Get the random row
144 $row = $dbw->selectRow( 'job', '*',
145 'job_id >= ' . mt_rand( $row->minjob
, $row->maxjob
), __METHOD__
);
146 if ( $row === false ) {
147 // Random job gone before we got the chance to select it
149 wfProfileOut( __METHOD__
);
152 // Delete the random row
153 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
154 $affected = $dbw->affectedRows();
158 // Random job gone before we exclusively deleted it
160 wfProfileOut( __METHOD__
);
165 // If execution got to here, there's a row in $row that has been deleted from the database
166 // by this thread. Hence the concurrent pop was successful.
167 wfIncrStats( 'job-pop' );
168 $namespace = $row->job_namespace
;
169 $dbkey = $row->job_title
;
170 $title = Title
::makeTitleSafe( $namespace, $dbkey );
171 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
173 // Remove any duplicates it may have later in the queue
174 $job->removeDuplicates();
176 wfProfileOut( __METHOD__
);
181 * Create the appropriate object to handle a specific job
183 * @param $command String: Job command
184 * @param $title Title: Associated title
185 * @param $params Array: Job parameters
186 * @param $id Int: Job identifier
189 static function factory( $command, $title, $params = false, $id = 0 ) {
190 global $wgJobClasses;
191 if( isset( $wgJobClasses[$command] ) ) {
192 $class = $wgJobClasses[$command];
193 return new $class( $title, $params, $id );
195 throw new MWException( "Invalid job command `{$command}`" );
198 static function makeBlob( $params ) {
199 if ( $params !== false ) {
200 return serialize( $params );
206 static function extractBlob( $blob ) {
207 if ( (string)$blob !== '' ) {
208 return unserialize( $blob );
215 * Batch-insert a group of jobs into the queue.
216 * This will be wrapped in a transaction with a forced commit.
218 * This may add duplicate at insert time, but they will be
219 * removed later on, when the first one is popped.
221 * @param $jobs array of Job objects
223 static function batchInsert( $jobs ) {
224 if( !count( $jobs ) ) {
227 $dbw = wfGetDB( DB_MASTER
);
229 foreach( $jobs as $job ) {
230 $rows[] = $job->insertFields();
231 if ( count( $rows ) >= 50 ) {
232 # Do a small transaction to avoid slave lag
234 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
240 wfIncrStats( 'job-insert', count( $rows ) );
242 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
247 /*-------------------------------------------------------------------------
248 * Non-static functions
249 *------------------------------------------------------------------------*/
254 * @param $params array
257 function __construct( $command, $title, $params = false, $id = 0 ) {
258 $this->command
= $command;
259 $this->title
= $title;
260 $this->params
= $params;
263 // A bit of premature generalisation
264 // Oh well, the whole class is premature generalisation really
265 $this->removeDuplicates
= true;
269 * Insert a single job into the queue.
270 * @return bool true on success
273 $fields = $this->insertFields();
275 $dbw = wfGetDB( DB_MASTER
);
277 if ( $this->removeDuplicates
) {
278 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__
);
279 if ( $dbw->numRows( $res ) ) {
283 wfIncrStats( 'job-insert' );
284 return $dbw->insert( 'job', $fields, __METHOD__
);
287 protected function insertFields() {
288 $dbw = wfGetDB( DB_MASTER
);
290 'job_id' => $dbw->nextSequenceValue( 'job_job_id_seq' ),
291 'job_cmd' => $this->command
,
292 'job_namespace' => $this->title
->getNamespace(),
293 'job_title' => $this->title
->getDBkey(),
294 'job_params' => Job
::makeBlob( $this->params
)
299 * Remove jobs in the job queue which are duplicates of this job.
300 * This is deadlock-prone and so starts its own transaction.
302 function removeDuplicates() {
303 if ( !$this->removeDuplicates
) {
307 $fields = $this->insertFields();
308 unset( $fields['job_id'] );
309 $dbw = wfGetDB( DB_MASTER
);
311 $dbw->delete( 'job', $fields, __METHOD__
);
312 $affected = $dbw->affectedRows();
315 wfIncrStats( 'job-dup-delete', $affected );
319 function toString() {
321 if ( $this->params
) {
322 foreach ( $this->params
as $key => $value ) {
323 if ( $paramString != '' ) {
326 $paramString .= "$key=$value";
330 if ( is_object( $this->title
) ) {
331 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
332 if ( $paramString !== '' ) {
333 $s .= ' ' . $paramString;
337 return "{$this->command} $paramString";
341 protected function setLastError( $error ) {
342 $this->error
= $error;
345 function getLastError() {