3 * @defgroup JobQueue JobQueue
6 if ( !defined( 'MEDIAWIKI' ) ) {
7 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
11 * Class to both describe a background job and handle jobs.
23 /*-------------------------------------------------------------------------
25 *------------------------------------------------------------------------*/
29 * @return boolean success
31 abstract function run();
33 /*-------------------------------------------------------------------------
35 *------------------------------------------------------------------------*/
38 * Pop a job of a certain type. This tries less hard than pop() to
39 * actually find a job; it may be adversely affected by concurrent job
42 static function pop_type( $type ) {
43 wfProfilein( __METHOD__
);
45 $dbw = wfGetDB( DB_MASTER
);
47 $row = $dbw->selectRow(
50 array( 'job_cmd' => $type ),
55 if ( $row === false ) {
56 wfProfileOut( __METHOD__
);
60 /* Ensure we "own" this row */
61 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
62 $affected = $dbw->affectedRows();
64 if ( $affected == 0 ) {
65 wfProfileOut( __METHOD__
);
69 $namespace = $row->job_namespace
;
70 $dbkey = $row->job_title
;
71 $title = Title
::makeTitleSafe( $namespace, $dbkey );
72 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
),
75 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
78 wfProfileOut( __METHOD__
);
83 * Pop a job off the front of the queue
85 * @param $offset Integer: Number of jobs to skip
86 * @return Job or false if there's no jobs
88 static function pop( $offset = 0 ) {
89 wfProfileIn( __METHOD__
);
91 $dbr = wfGetDB( DB_SLAVE
);
93 /* Get a job from the slave, start with an offset,
94 scan full set afterwards, avoid hitting purged rows
96 NB: If random fetch previously was used, offset
97 will always be ahead of few entries
100 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__
,
101 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) );
103 // Refetching without offset is needed as some of job IDs could have had delayed commits
104 // and have lower IDs than jobs already executed, blame concurrency :)
106 if ( $row === false ) {
107 if ( $offset != 0 ) {
108 $row = $dbr->selectRow( 'job', '*', '', __METHOD__
,
109 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) );
112 if ( $row === false ) {
113 wfProfileOut( __METHOD__
);
117 $offset = $row->job_id
;
119 // Try to delete it from the master
120 $dbw = wfGetDB( DB_MASTER
);
121 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
122 $affected = $dbw->affectedRows();
126 // Failed, someone else beat us to it
127 // Try getting a random row
128 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
129 'MAX(job_id) as maxjob' ), '1=1', __METHOD__
);
130 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
132 wfProfileOut( __METHOD__
);
135 // Get the random row
136 $row = $dbw->selectRow( 'job', '*',
137 'job_id >= ' . mt_rand( $row->minjob
, $row->maxjob
), __METHOD__
);
138 if ( $row === false ) {
139 // Random job gone before we got the chance to select it
141 wfProfileOut( __METHOD__
);
144 // Delete the random row
145 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
146 $affected = $dbw->affectedRows();
150 // Random job gone before we exclusively deleted it
152 wfProfileOut( __METHOD__
);
157 // If execution got to here, there's a row in $row that has been deleted from the database
158 // by this thread. Hence the concurrent pop was successful.
159 $namespace = $row->job_namespace
;
160 $dbkey = $row->job_title
;
161 $title = Title
::makeTitleSafe( $namespace, $dbkey );
162 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
164 // Remove any duplicates it may have later in the queue
165 // Deadlock prone section
167 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
170 wfProfileOut( __METHOD__
);
175 * Create the appropriate object to handle a specific job
177 * @param $command String: Job command
178 * @param $title Title: Associated title
179 * @param $params Array: Job parameters
180 * @param $id Int: Job identifier
183 static function factory( $command, $title, $params = false, $id = 0 ) {
184 global $wgJobClasses;
185 if( isset( $wgJobClasses[$command] ) ) {
186 $class = $wgJobClasses[$command];
187 return new $class( $title, $params, $id );
189 throw new MWException( "Invalid job command `{$command}`" );
192 static function makeBlob( $params ) {
193 if ( $params !== false ) {
194 return serialize( $params );
200 static function extractBlob( $blob ) {
201 if ( (string)$blob !== '' ) {
202 return unserialize( $blob );
209 * Batch-insert a group of jobs into the queue.
210 * This will be wrapped in a transaction with a forced commit.
212 * This may add duplicate at insert time, but they will be
213 * removed later on, when the first one is popped.
215 * @param $jobs array of Job objects
217 static function batchInsert( $jobs ) {
218 if( !count( $jobs ) ) {
221 $dbw = wfGetDB( DB_MASTER
);
223 foreach( $jobs as $job ) {
224 $rows[] = $job->insertFields();
225 if ( count( $rows ) >= 50 ) {
226 # Do a small transaction to avoid slave lag
228 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
235 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
240 /*-------------------------------------------------------------------------
241 * Non-static functions
242 *------------------------------------------------------------------------*/
244 function __construct( $command, $title, $params = false, $id = 0 ) {
245 $this->command
= $command;
246 $this->title
= $title;
247 $this->params
= $params;
250 // A bit of premature generalisation
251 // Oh well, the whole class is premature generalisation really
252 $this->removeDuplicates
= true;
256 * Insert a single job into the queue.
257 * @return bool true on success
260 $fields = $this->insertFields();
262 $dbw = wfGetDB( DB_MASTER
);
264 if ( $this->removeDuplicates
) {
265 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__
);
266 if ( $dbw->numRows( $res ) ) {
270 return $dbw->insert( 'job', $fields, __METHOD__
);
273 protected function insertFields() {
274 $dbw = wfGetDB( DB_MASTER
);
276 'job_id' => $dbw->nextSequenceValue( 'job_job_id_seq' ),
277 'job_cmd' => $this->command
,
278 'job_namespace' => $this->title
->getNamespace(),
279 'job_title' => $this->title
->getDBkey(),
280 'job_params' => Job
::makeBlob( $this->params
)
284 function toString() {
286 if ( $this->params
) {
287 foreach ( $this->params
as $key => $value ) {
288 if ( $paramString != '' ) {
291 $paramString .= "$key=$value";
295 if ( is_object( $this->title
) ) {
296 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
297 if ( $paramString !== '' ) {
298 $s .= ' ' . $paramString;
302 return "{$this->command} $paramString";
306 protected function setLastError( $error ) {
307 $this->error
= $error;
310 function getLastError() {