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();
72 if ( $affected == 0 ) {
73 wfProfileOut( __METHOD__
);
77 $namespace = $row->job_namespace
;
78 $dbkey = $row->job_title
;
79 $title = Title
::makeTitleSafe( $namespace, $dbkey );
80 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
),
83 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
86 wfProfileOut( __METHOD__
);
91 * Pop a job off the front of the queue
93 * @param $offset Integer: Number of jobs to skip
94 * @return Job or false if there's no jobs
96 static function pop( $offset = 0 ) {
97 wfProfileIn( __METHOD__
);
99 $dbr = wfGetDB( DB_SLAVE
);
101 /* Get a job from the slave, start with an offset,
102 scan full set afterwards, avoid hitting purged rows
104 NB: If random fetch previously was used, offset
105 will always be ahead of few entries
108 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__
,
109 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) );
111 // Refetching without offset is needed as some of job IDs could have had delayed commits
112 // and have lower IDs than jobs already executed, blame concurrency :)
114 if ( $row === false ) {
115 if ( $offset != 0 ) {
116 $row = $dbr->selectRow( 'job', '*', '', __METHOD__
,
117 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) );
120 if ( $row === false ) {
121 wfProfileOut( __METHOD__
);
126 // Try to delete it from the master
127 $dbw = wfGetDB( DB_MASTER
);
128 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
129 $affected = $dbw->affectedRows();
133 // Failed, someone else beat us to it
134 // Try getting a random row
135 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
136 'MAX(job_id) as maxjob' ), '1=1', __METHOD__
);
137 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
139 wfProfileOut( __METHOD__
);
142 // Get the random row
143 $row = $dbw->selectRow( 'job', '*',
144 'job_id >= ' . mt_rand( $row->minjob
, $row->maxjob
), __METHOD__
);
145 if ( $row === false ) {
146 // Random job gone before we got the chance to select it
148 wfProfileOut( __METHOD__
);
151 // Delete the random row
152 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
153 $affected = $dbw->affectedRows();
157 // Random job gone before we exclusively deleted it
159 wfProfileOut( __METHOD__
);
164 // If execution got to here, there's a row in $row that has been deleted from the database
165 // by this thread. Hence the concurrent pop was successful.
166 $namespace = $row->job_namespace
;
167 $dbkey = $row->job_title
;
168 $title = Title
::makeTitleSafe( $namespace, $dbkey );
169 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
171 // Remove any duplicates it may have later in the queue
172 // Deadlock prone section
174 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
177 wfProfileOut( __METHOD__
);
182 * Create the appropriate object to handle a specific job
184 * @param $command String: Job command
185 * @param $title Title: Associated title
186 * @param $params Array: Job parameters
187 * @param $id Int: Job identifier
190 static function factory( $command, $title, $params = false, $id = 0 ) {
191 global $wgJobClasses;
192 if( isset( $wgJobClasses[$command] ) ) {
193 $class = $wgJobClasses[$command];
194 return new $class( $title, $params, $id );
196 throw new MWException( "Invalid job command `{$command}`" );
199 static function makeBlob( $params ) {
200 if ( $params !== false ) {
201 return serialize( $params );
207 static function extractBlob( $blob ) {
208 if ( (string)$blob !== '' ) {
209 return unserialize( $blob );
216 * Batch-insert a group of jobs into the queue.
217 * This will be wrapped in a transaction with a forced commit.
219 * This may add duplicate at insert time, but they will be
220 * removed later on, when the first one is popped.
222 * @param $jobs array of Job objects
224 static function batchInsert( $jobs ) {
225 if( !count( $jobs ) ) {
228 $dbw = wfGetDB( DB_MASTER
);
230 foreach( $jobs as $job ) {
231 $rows[] = $job->insertFields();
232 if ( count( $rows ) >= 50 ) {
233 # Do a small transaction to avoid slave lag
235 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
242 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
247 /*-------------------------------------------------------------------------
248 * Non-static functions
249 *------------------------------------------------------------------------*/
251 function __construct( $command, $title, $params = false, $id = 0 ) {
252 $this->command
= $command;
253 $this->title
= $title;
254 $this->params
= $params;
257 // A bit of premature generalisation
258 // Oh well, the whole class is premature generalisation really
259 $this->removeDuplicates
= true;
263 * Insert a single job into the queue.
264 * @return bool true on success
267 $fields = $this->insertFields();
269 $dbw = wfGetDB( DB_MASTER
);
271 if ( $this->removeDuplicates
) {
272 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__
);
273 if ( $dbw->numRows( $res ) ) {
277 return $dbw->insert( 'job', $fields, __METHOD__
);
280 protected function insertFields() {
281 $dbw = wfGetDB( DB_MASTER
);
283 'job_id' => $dbw->nextSequenceValue( 'job_job_id_seq' ),
284 'job_cmd' => $this->command
,
285 'job_namespace' => $this->title
->getNamespace(),
286 'job_title' => $this->title
->getDBkey(),
287 'job_params' => Job
::makeBlob( $this->params
)
291 function toString() {
293 if ( $this->params
) {
294 foreach ( $this->params
as $key => $value ) {
295 if ( $paramString != '' ) {
298 $paramString .= "$key=$value";
302 if ( is_object( $this->title
) ) {
303 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
304 if ( $paramString !== '' ) {
305 $s .= ' ' . $paramString;
309 return "{$this->command} $paramString";
313 protected function setLastError( $error ) {
314 $this->error
= $error;
317 function getLastError() {