allow to specify offset at which to pop a Job.
[lhc/web/wiklou.git] / includes / JobQueue.php
1 <?php
2
3 if ( !defined( 'MEDIAWIKI' ) ) {
4 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
5 }
6
7 abstract class Job {
8 var $command,
9 $title,
10 $params,
11 $id,
12 $removeDuplicates,
13 $error;
14
15 /*-------------------------------------------------------------------------
16 * Static functions
17 *------------------------------------------------------------------------*/
18
19 /**
20 * @deprecated use LinksUpdate::queueRecursiveJobs()
21 */
22 /**
23 * static function queueLinksJobs( $titles ) {}
24 */
25
26 /**
27 * Pop a job off the front of the queue
28 * @static
29 * @param $offset Number of jobs to skip
30 * @return Job or false if there's no jobs
31 */
32 static function pop($offset=0) {
33 wfProfileIn( __METHOD__ );
34
35 $dbr = wfGetDB( DB_SLAVE );
36
37 /* Get a job from the slave, start with an offset,
38 scan full set afterwards, avoid hitting purged rows
39
40 NB: If random fetch previously was used, offset
41 will always be ahead of few entries
42 */
43
44 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__,
45 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
46
47 // Refetching without offset is needed as some of job IDs could have had delayed commits
48 // and have lower IDs than jobs already executed, blame concurrency :)
49
50 if ( $row === false && $offset != 0 ) {
51 $offset=0;
52 $row = $dbr->selectRow( 'job', '*', '', __METHOD__,
53 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
54
55 if ($row === false ) {
56 wfProfileOut( __METHOD__ );
57 return false;
58 }
59 }
60
61 /* Still avoid scanning purged rows */
62 $offset = $row->job_id;
63
64 // Try to delete it from the master
65 $dbw = wfGetDB( DB_MASTER );
66 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
67 $affected = $dbw->affectedRows();
68 $dbw->immediateCommit();
69
70 if ( !$affected ) {
71 // Failed, someone else beat us to it
72 // Try getting a random row
73 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
74 'MAX(job_id) as maxjob' ), "job_id >= $offset", __METHOD__ );
75 if ( $row === false || is_null( $row->minjob ) || is_null( $row->maxjob ) ) {
76 // No jobs to get
77 wfProfileOut( __METHOD__ );
78 return false;
79 }
80 // Get the random row
81 $row = $dbw->selectRow( 'job', '*',
82 'job_id >= ' . mt_rand( $row->minjob, $row->maxjob ), __METHOD__ );
83 if ( $row === false ) {
84 // Random job gone before we got the chance to select it
85 // Give up
86 wfProfileOut( __METHOD__ );
87 return false;
88 }
89 // Delete the random row
90 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
91 $affected = $dbw->affectedRows();
92 $dbw->immediateCommit();
93
94 if ( !$affected ) {
95 // Random job gone before we exclusively deleted it
96 // Give up
97 wfProfileOut( __METHOD__ );
98 return false;
99 }
100 }
101
102 // If execution got to here, there's a row in $row that has been deleted from the database
103 // by this thread. Hence the concurrent pop was successful.
104 $namespace = $row->job_namespace;
105 $dbkey = $row->job_title;
106 $title = Title::makeTitleSafe( $namespace, $dbkey );
107 $job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id );
108
109 // Remove any duplicates it may have later in the queue
110 $dbw->delete( 'job', $job->insertFields(), __METHOD__ );
111
112 wfProfileOut( __METHOD__ );
113 return $job;
114 }
115
116 /**
117 * Create an object of a subclass
118 */
119 static function factory( $command, $title, $params = false, $id = 0 ) {
120 switch ( $command ) {
121 case 'refreshLinks':
122 return new RefreshLinksJob( $title, $params, $id );
123 case 'htmlCacheUpdate':
124 case 'html_cache_update': # BC
125 return new HTMLCacheUpdateJob( $title, $params['table'], $params['start'], $params['end'], $id );
126 default:
127 throw new MWException( "Invalid job command \"$command\"" );
128 }
129 }
130
131 static function makeBlob( $params ) {
132 if ( $params !== false ) {
133 return serialize( $params );
134 } else {
135 return '';
136 }
137 }
138
139 static function extractBlob( $blob ) {
140 if ( (string)$blob !== '' ) {
141 return unserialize( $blob );
142 } else {
143 return false;
144 }
145 }
146
147 /*-------------------------------------------------------------------------
148 * Non-static functions
149 *------------------------------------------------------------------------*/
150
151 function __construct( $command, $title, $params = false, $id = 0 ) {
152 $this->command = $command;
153 $this->title = $title;
154 $this->params = $params;
155 $this->id = $id;
156
157 // A bit of premature generalisation
158 // Oh well, the whole class is premature generalisation really
159 $this->removeDuplicates = true;
160 }
161
162 /**
163 * Insert a single job into the queue.
164 */
165 function insert() {
166 $fields = $this->insertFields();
167
168 $dbw = wfGetDB( DB_MASTER );
169
170 if ( $this->removeDuplicates ) {
171 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__ );
172 if ( $dbw->numRows( $res ) ) {
173 return;
174 }
175 }
176 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
177 $dbw->insert( 'job', $fields, __METHOD__ );
178 }
179
180 protected function insertFields() {
181 return array(
182 'job_cmd' => $this->command,
183 'job_namespace' => $this->title->getNamespace(),
184 'job_title' => $this->title->getDBkey(),
185 'job_params' => Job::makeBlob( $this->params )
186 );
187 }
188
189 /**
190 * Batch-insert a group of jobs into the queue.
191 * This will be wrapped in a transaction with a forced commit.
192 *
193 * This may add duplicate at insert time, but they will be
194 * removed later on, when the first one is popped.
195 *
196 * @param $jobs array of Job objects
197 */
198 static function batchInsert( $jobs ) {
199 if( count( $jobs ) ) {
200 $dbw = wfGetDB( DB_MASTER );
201 $dbw->begin();
202 foreach( $jobs as $job ) {
203 $rows[] = $job->insertFields();
204 }
205 $dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
206 $dbw->commit();
207 }
208 }
209
210 /**
211 * Run the job
212 * @return boolean success
213 */
214 abstract function run();
215
216 function toString() {
217 $paramString = '';
218 if ( $this->params ) {
219 foreach ( $this->params as $key => $value ) {
220 if ( $paramString != '' ) {
221 $paramString .= ' ';
222 }
223 $paramString .= "$key=$value";
224 }
225 }
226
227 if ( is_object( $this->title ) ) {
228 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
229 if ( $paramString !== '' ) {
230 $s .= ' ' . $paramString;
231 }
232 return $s;
233 } else {
234 return "{$this->command} $paramString";
235 }
236 }
237
238 function getLastError() {
239 return $this->error;
240 }
241 }
242
243 class RefreshLinksJob extends Job {
244 function __construct( $title, $params = '', $id = 0 ) {
245 parent::__construct( 'refreshLinks', $title, $params, $id );
246 }
247
248 /**
249 * Run a refreshLinks job
250 * @return boolean success
251 */
252 function run() {
253 global $wgParser;
254 wfProfileIn( __METHOD__ );
255
256 $linkCache =& LinkCache::singleton();
257 $linkCache->clear();
258
259 if ( is_null( $this->title ) ) {
260 $this->error = "refreshLinks: Invalid title";
261 wfProfileOut( __METHOD__ );
262 return false;
263 }
264
265 $revision = Revision::newFromTitle( $this->title );
266 if ( !$revision ) {
267 $this->error = 'refreshLinks: Article not found "' . $this->title->getPrefixedDBkey() . '"';
268 wfProfileOut( __METHOD__ );
269 return false;
270 }
271
272 wfProfileIn( __METHOD__.'-parse' );
273 $options = new ParserOptions;
274 $parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options, true, true, $revision->getId() );
275 wfProfileOut( __METHOD__.'-parse' );
276 wfProfileIn( __METHOD__.'-update' );
277 $update = new LinksUpdate( $this->title, $parserOutput, false );
278 $update->doUpdate();
279 wfProfileOut( __METHOD__.'-update' );
280 wfProfileOut( __METHOD__ );
281 return true;
282 }
283 }
284
285 ?>