3 if ( !defined( 'MEDIAWIKI' ) ) {
4 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
8 * Class to both describe a background job and handle jobs.
18 /*-------------------------------------------------------------------------
20 *------------------------------------------------------------------------*/
24 * @return boolean success
26 abstract function run();
28 /*-------------------------------------------------------------------------
30 *------------------------------------------------------------------------*/
33 * @deprecated use LinksUpdate::queueRecursiveJobs()
36 * static function queueLinksJobs( $titles ) {}
40 * Pop a job of a certain type. This tries less hard than pop() to
41 * actually find a job; it may be adversely affected by concurrent job
44 static function pop_type($type) {
45 wfProfilein( __METHOD__
);
47 $dbw = wfGetDB( DB_MASTER
);
50 $row = $dbw->selectRow( 'job', '*', array( 'job_cmd' => $type ), __METHOD__
,
51 array( 'LIMIT' => 1 ));
54 wfProfileOut( __METHOD__
);
58 /* Ensure we "own" this row */
59 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
60 $affected = $dbw->affectedRows();
63 wfProfileOut( __METHOD__
);
67 $namespace = $row->job_namespace
;
68 $dbkey = $row->job_title
;
69 $title = Title
::makeTitleSafe( $namespace, $dbkey );
70 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
72 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
73 $dbw->immediateCommit();
75 wfProfileOut( __METHOD__
);
80 * Pop a job off the front of the queue
82 * @param $offset Number of jobs to skip
83 * @return Job or false if there's no jobs
85 static function pop($offset=0) {
86 wfProfileIn( __METHOD__
);
88 $dbr = wfGetDB( DB_SLAVE
);
90 /* Get a job from the slave, start with an offset,
91 scan full set afterwards, avoid hitting purged rows
93 NB: If random fetch previously was used, offset
94 will always be ahead of few entries
97 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__
,
98 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
100 // Refetching without offset is needed as some of job IDs could have had delayed commits
101 // and have lower IDs than jobs already executed, blame concurrency :)
103 if ( $row === false) {
105 $row = $dbr->selectRow( 'job', '*', '', __METHOD__
,
106 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
108 if ($row === false ) {
109 wfProfileOut( __METHOD__
);
113 $offset = $row->job_id
;
115 // Try to delete it from the master
116 $dbw = wfGetDB( DB_MASTER
);
117 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
118 $affected = $dbw->affectedRows();
119 $dbw->immediateCommit();
122 // Failed, someone else beat us to it
123 // Try getting a random row
124 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
125 'MAX(job_id) as maxjob' ), "job_id >= $offset", __METHOD__
);
126 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
128 wfProfileOut( __METHOD__
);
131 // Get the random row
132 $row = $dbw->selectRow( 'job', '*',
133 'job_id >= ' . mt_rand( $row->minjob
, $row->maxjob
), __METHOD__
);
134 if ( $row === false ) {
135 // Random job gone before we got the chance to select it
137 wfProfileOut( __METHOD__
);
140 // Delete the random row
141 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
142 $affected = $dbw->affectedRows();
143 $dbw->immediateCommit();
146 // Random job gone before we exclusively deleted it
148 wfProfileOut( __METHOD__
);
153 // If execution got to here, there's a row in $row that has been deleted from the database
154 // by this thread. Hence the concurrent pop was successful.
155 $namespace = $row->job_namespace
;
156 $dbkey = $row->job_title
;
157 $title = Title
::makeTitleSafe( $namespace, $dbkey );
158 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
160 // Remove any duplicates it may have later in the queue
161 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
163 wfProfileOut( __METHOD__
);
168 * Create the appropriate object to handle a specific job
170 * @param string $command Job command
171 * @param Title $title Associated title
172 * @param array $params Job parameters
173 * @param int $id Job identifier
176 static function factory( $command, $title, $params = false, $id = 0 ) {
177 global $wgJobClasses;
178 if( isset( $wgJobClasses[$command] ) ) {
179 $class = $wgJobClasses[$command];
180 return new $class( $title, $params, $id );
182 throw new MWException( "Invalid job command `{$command}`" );
185 static function makeBlob( $params ) {
186 if ( $params !== false ) {
187 return serialize( $params );
193 static function extractBlob( $blob ) {
194 if ( (string)$blob !== '' ) {
195 return unserialize( $blob );
202 * Batch-insert a group of jobs into the queue.
203 * This will be wrapped in a transaction with a forced commit.
205 * This may add duplicate at insert time, but they will be
206 * removed later on, when the first one is popped.
208 * @param $jobs array of Job objects
210 static function batchInsert( $jobs ) {
211 if( count( $jobs ) ) {
212 $dbw = wfGetDB( DB_MASTER
);
214 foreach( $jobs as $job ) {
215 $rows[] = $job->insertFields();
217 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
222 /*-------------------------------------------------------------------------
223 * Non-static functions
224 *------------------------------------------------------------------------*/
226 function __construct( $command, $title, $params = false, $id = 0 ) {
227 $this->command
= $command;
228 $this->title
= $title;
229 $this->params
= $params;
232 // A bit of premature generalisation
233 // Oh well, the whole class is premature generalisation really
234 $this->removeDuplicates
= true;
238 * Insert a single job into the queue.
241 $fields = $this->insertFields();
243 $dbw = wfGetDB( DB_MASTER
);
245 if ( $this->removeDuplicates
) {
246 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__
);
247 if ( $dbw->numRows( $res ) ) {
251 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
252 $dbw->insert( 'job', $fields, __METHOD__
);
255 protected function insertFields() {
257 'job_cmd' => $this->command
,
258 'job_namespace' => $this->title
->getNamespace(),
259 'job_title' => $this->title
->getDBkey(),
260 'job_params' => Job
::makeBlob( $this->params
)
264 function toString() {
266 if ( $this->params
) {
267 foreach ( $this->params
as $key => $value ) {
268 if ( $paramString != '' ) {
271 $paramString .= "$key=$value";
275 if ( is_object( $this->title
) ) {
276 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
277 if ( $paramString !== '' ) {
278 $s .= ' ' . $paramString;
282 return "{$this->command} $paramString";
286 function getLastError() {