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 * @deprecated use LinksUpdate::queueRecursiveJobs()
41 * static function queueLinksJobs( $titles ) {}
45 * Pop a job of a certain type. This tries less hard than pop() to
46 * actually find a job; it may be adversely affected by concurrent job
49 static function pop_type($type) {
50 wfProfilein( __METHOD__
);
52 $dbw = wfGetDB( DB_MASTER
);
55 $row = $dbw->selectRow( 'job', '*', array( 'job_cmd' => $type ), __METHOD__
,
56 array( 'LIMIT' => 1 ));
59 wfProfileOut( __METHOD__
);
63 /* Ensure we "own" this row */
64 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
65 $affected = $dbw->affectedRows();
68 wfProfileOut( __METHOD__
);
72 $namespace = $row->job_namespace
;
73 $dbkey = $row->job_title
;
74 $title = Title
::makeTitleSafe( $namespace, $dbkey );
75 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
77 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
78 $dbw->immediateCommit();
80 wfProfileOut( __METHOD__
);
85 * Pop a job off the front of the queue
87 * @param $offset Number of jobs to skip
88 * @return Job or false if there's no jobs
90 static function pop($offset=0) {
91 wfProfileIn( __METHOD__
);
93 $dbr = wfGetDB( DB_SLAVE
);
95 /* Get a job from the slave, start with an offset,
96 scan full set afterwards, avoid hitting purged rows
98 NB: If random fetch previously was used, offset
99 will always be ahead of few entries
102 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__
,
103 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
105 // Refetching without offset is needed as some of job IDs could have had delayed commits
106 // and have lower IDs than jobs already executed, blame concurrency :)
108 if ( $row === false) {
110 $row = $dbr->selectRow( 'job', '*', '', __METHOD__
,
111 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
113 if ($row === false ) {
114 wfProfileOut( __METHOD__
);
118 $offset = $row->job_id
;
120 // Try to delete it from the master
121 $dbw = wfGetDB( DB_MASTER
);
122 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
123 $affected = $dbw->affectedRows();
124 $dbw->immediateCommit();
127 // Failed, someone else beat us to it
128 // Try getting a random row
129 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
130 'MAX(job_id) as maxjob' ), "job_id >= $offset", __METHOD__
);
131 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
133 wfProfileOut( __METHOD__
);
136 // Get the random row
137 $row = $dbw->selectRow( 'job', '*',
138 'job_id >= ' . mt_rand( $row->minjob
, $row->maxjob
), __METHOD__
);
139 if ( $row === false ) {
140 // Random job gone before we got the chance to select it
142 wfProfileOut( __METHOD__
);
145 // Delete the random row
146 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
147 $affected = $dbw->affectedRows();
148 $dbw->immediateCommit();
151 // Random job gone before we exclusively deleted it
153 wfProfileOut( __METHOD__
);
158 // If execution got to here, there's a row in $row that has been deleted from the database
159 // by this thread. Hence the concurrent pop was successful.
160 $namespace = $row->job_namespace
;
161 $dbkey = $row->job_title
;
162 $title = Title
::makeTitleSafe( $namespace, $dbkey );
163 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
165 // Remove any duplicates it may have later in the queue
166 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
168 wfProfileOut( __METHOD__
);
173 * Create the appropriate object to handle a specific job
175 * @param $command String: Job command
176 * @param $title Title: Associated title
177 * @param $params Array: Job parameters
178 * @param $id Int: Job identifier
181 static function factory( $command, $title, $params = false, $id = 0 ) {
182 global $wgJobClasses;
183 if( isset( $wgJobClasses[$command] ) ) {
184 $class = $wgJobClasses[$command];
185 return new $class( $title, $params, $id );
187 throw new MWException( "Invalid job command `{$command}`" );
190 static function makeBlob( $params ) {
191 if ( $params !== false ) {
192 return serialize( $params );
198 static function extractBlob( $blob ) {
199 if ( (string)$blob !== '' ) {
200 return unserialize( $blob );
207 * Batch-insert a group of jobs into the queue.
208 * This will be wrapped in a transaction with a forced commit.
210 * This may add duplicate at insert time, but they will be
211 * removed later on, when the first one is popped.
213 * @param $jobs array of Job objects
215 static function batchInsert( $jobs ) {
216 if( count( $jobs ) ) {
217 $dbw = wfGetDB( DB_MASTER
);
219 foreach( $jobs as $job ) {
220 $rows[] = $job->insertFields();
222 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
227 /*-------------------------------------------------------------------------
228 * Non-static functions
229 *------------------------------------------------------------------------*/
231 function __construct( $command, $title, $params = false, $id = 0 ) {
232 $this->command
= $command;
233 $this->title
= $title;
234 $this->params
= $params;
237 // A bit of premature generalisation
238 // Oh well, the whole class is premature generalisation really
239 $this->removeDuplicates
= true;
243 * Insert a single job into the queue.
246 $fields = $this->insertFields();
248 $dbw = wfGetDB( DB_MASTER
);
250 if ( $this->removeDuplicates
) {
251 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__
);
252 if ( $dbw->numRows( $res ) ) {
256 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
257 $dbw->insert( 'job', $fields, __METHOD__
);
260 protected function insertFields() {
262 'job_cmd' => $this->command
,
263 'job_namespace' => $this->title
->getNamespace(),
264 'job_title' => $this->title
->getDBkey(),
265 'job_params' => Job
::makeBlob( $this->params
)
269 function toString() {
271 if ( $this->params
) {
272 foreach ( $this->params
as $key => $value ) {
273 if ( $paramString != '' ) {
276 $paramString .= "$key=$value";
280 if ( is_object( $this->title
) ) {
281 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
282 if ( $paramString !== '' ) {
283 $s .= ' ' . $paramString;
287 return "{$this->command} $paramString";
291 function getLastError() {