r32045 committed from wrong working branch. Revert and commit the one I wanted.
[mediawiki.git] / includes / JobQueue.php
blob5cec3106dfcdfc01c3c509414c4a40001f8aa02f
1 <?php
3 if ( !defined( 'MEDIAWIKI' ) ) {
4 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
7 /**
8 * Class to both describe a background job and handle jobs.
9 */
10 abstract class Job {
11 var $command,
12 $title,
13 $params,
14 $id,
15 $removeDuplicates,
16 $error;
18 /*-------------------------------------------------------------------------
19 * Abstract functions
20 *------------------------------------------------------------------------*/
22 /**
23 * Run the job
24 * @return boolean success
26 abstract function run();
28 /*-------------------------------------------------------------------------
29 * Static functions
30 *------------------------------------------------------------------------*/
32 /**
33 * @deprecated use LinksUpdate::queueRecursiveJobs()
35 /**
36 * static function queueLinksJobs( $titles ) {}
39 /**
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
42 * runners.
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 ));
53 if ($row === false) {
54 wfProfileOut( __METHOD__ );
55 return false;
58 /* Ensure we "own" this row */
59 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
60 $affected = $dbw->affectedRows();
62 if ($affected == 0) {
63 wfProfileOut( __METHOD__ );
64 return false;
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__ );
76 return $job;
79 /**
80 * Pop a job off the front of the queue
81 * @static
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) {
104 if ($offset!=0)
105 $row = $dbr->selectRow( 'job', '*', '', __METHOD__,
106 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
108 if ($row === false ) {
109 wfProfileOut( __METHOD__ );
110 return false;
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();
121 if ( !$affected ) {
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 ) ) {
127 // No jobs to get
128 wfProfileOut( __METHOD__ );
129 return false;
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
136 // Give up
137 wfProfileOut( __METHOD__ );
138 return false;
140 // Delete the random row
141 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
142 $affected = $dbw->affectedRows();
143 $dbw->immediateCommit();
145 if ( !$affected ) {
146 // Random job gone before we exclusively deleted it
147 // Give up
148 wfProfileOut( __METHOD__ );
149 return false;
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__ );
164 return $job;
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
174 * @return Job
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 );
188 } else {
189 return '';
193 static function extractBlob( $blob ) {
194 if ( (string)$blob !== '' ) {
195 return unserialize( $blob );
196 } else {
197 return false;
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 );
213 $dbw->begin();
214 foreach( $jobs as $job ) {
215 $rows[] = $job->insertFields();
217 $dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
218 $dbw->commit();
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;
230 $this->id = $id;
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.
240 function insert() {
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 ) ) {
248 return;
251 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
252 $dbw->insert( 'job', $fields, __METHOD__ );
255 protected function insertFields() {
256 return array(
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() {
265 $paramString = '';
266 if ( $this->params ) {
267 foreach ( $this->params as $key => $value ) {
268 if ( $paramString != '' ) {
269 $paramString .= ' ';
271 $paramString .= "$key=$value";
275 if ( is_object( $this->title ) ) {
276 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
277 if ( $paramString !== '' ) {
278 $s .= ' ' . $paramString;
280 return $s;
281 } else {
282 return "{$this->command} $paramString";
286 function getLastError() {
287 return $this->error;