Merge "Include Postgres tables and maintenance scripts for Change I23c47c2c"
[lhc/web/wiklou.git] / includes / job / JobQueue.php
1 <?php
2 /**
3 * Job queue base code.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @defgroup JobQueue JobQueue
22 * @author Aaron Schulz
23 */
24
25 /**
26 * Class to handle enqueueing and running of background jobs
27 *
28 * @ingroup JobQueue
29 * @since 1.21
30 */
31 abstract class JobQueue {
32 protected $wiki; // string; wiki ID
33 protected $type; // string; job type
34 protected $order; // string; job priority for pop()
35 protected $claimTTL; // integer; seconds
36
37 const QoS_Atomic = 1; // integer; "all-or-nothing" job insertions
38
39 const MAX_ATTEMPTS = 3; // integer; number of times to try a job
40
41 /**
42 * @param $params array
43 */
44 protected function __construct( array $params ) {
45 $this->wiki = $params['wiki'];
46 $this->type = $params['type'];
47 $this->order = isset( $params['order'] ) ? $params['order'] : 'random';
48 $this->claimTTL = isset( $params['claimTTL'] ) ? $params['claimTTL'] : 0;
49 }
50
51 /**
52 * Get a job queue object of the specified type.
53 * $params includes:
54 * - class : What job class to use (determines job type)
55 * - wiki : wiki ID of the wiki the jobs are for (defaults to current wiki)
56 * - type : The name of the job types this queue handles
57 * - order : Order that pop() selects jobs, one of "fifo", "timestamp" or "random".
58 * If "fifo" is used, the queue will effectively be FIFO. Note that
59 * job completion will not appear to be exactly FIFO if there are multiple
60 * job runners since jobs can take different times to finish once popped.
61 * If "timestamp" is used, the queue will at least be loosely ordered
62 * by timestamp, allowing for some jobs to be popped off out of order.
63 * If "random" is used, pop() will pick jobs in random order. This might be
64 * useful for improving concurrency depending on the queue storage medium.
65 * - claimTTL : If supported, the queue will recycle jobs that have been popped
66 * but not acknowledged as completed after this many seconds. Recycling
67 * of jobs simple means re-inserting them into the queue. Jobs can be
68 * attempted up to three times before being discarded.
69 *
70 * Queue classes should throw an exception if they do not support the options given.
71 *
72 * @param $params array
73 * @return JobQueue
74 * @throws MWException
75 */
76 final public static function factory( array $params ) {
77 $class = $params['class'];
78 if ( !MWInit::classExists( $class ) ) {
79 throw new MWException( "Invalid job queue class '$class'." );
80 }
81 $obj = new $class( $params );
82 if ( !( $obj instanceof self ) ) {
83 throw new MWException( "Class '$class' is not a " . __CLASS__ . " class." );
84 }
85 return $obj;
86 }
87
88 /**
89 * @return string Wiki ID
90 */
91 final public function getWiki() {
92 return $this->wiki;
93 }
94
95 /**
96 * @return string Job type that this queue handles
97 */
98 final public function getType() {
99 return $this->type;
100 }
101
102 /**
103 * Quickly check if the queue is empty (has no available jobs).
104 * Queue classes should use caching if they are any slower without memcached.
105 *
106 * @return bool
107 */
108 final public function isEmpty() {
109 wfProfileIn( __METHOD__ );
110 $res = $this->doIsEmpty();
111 wfProfileOut( __METHOD__ );
112 return $res;
113 }
114
115 /**
116 * @see JobQueue::isEmpty()
117 * @return bool
118 */
119 abstract protected function doIsEmpty();
120
121 /**
122 * Get the number of available jobs in the queue.
123 * Queue classes should use caching if they are any slower without memcached.
124 *
125 * @return integer
126 */
127 final public function getSize() {
128 wfProfileIn( __METHOD__ );
129 $res = $this->doGetSize();
130 wfProfileOut( __METHOD__ );
131 return $res;
132 }
133
134 /**
135 * @see JobQueue::getSize()
136 * @return integer
137 */
138 abstract protected function doGetSize();
139
140 /**
141 * Get the number of acquired jobs (these are temporarily out of the queue).
142 * Queue classes should use caching if they are any slower without memcached.
143 *
144 * @return integer
145 */
146 final public function getAcquiredCount() {
147 wfProfileIn( __METHOD__ );
148 $res = $this->doGetAcquiredCount();
149 wfProfileOut( __METHOD__ );
150 return $res;
151 }
152
153 /**
154 * @see JobQueue::getAcquiredCount()
155 * @return integer
156 */
157 abstract protected function doGetAcquiredCount();
158
159 /**
160 * Push a single jobs into the queue.
161 * This does not require $wgJobClasses to be set for the given job type.
162 *
163 * @param $jobs Job|Array
164 * @param $flags integer Bitfield (supports JobQueue::QoS_Atomic)
165 * @throws MWException
166 * @return bool Returns false on failure
167 */
168 final public function push( $jobs, $flags = 0 ) {
169 $jobs = is_array( $jobs ) ? $jobs : array( $jobs );
170
171 return $this->batchPush( $jobs, $flags );
172 }
173
174 /**
175 * Push a batch of jobs into the queue.
176 * This does not require $wgJobClasses to be set for the given job type.
177 *
178 * @param $jobs array List of Jobs
179 * @param $flags integer Bitfield (supports JobQueue::QoS_Atomic)
180 * @throws MWException
181 * @return bool Returns false on failure
182 */
183 final public function batchPush( array $jobs, $flags = 0 ) {
184 foreach ( $jobs as $job ) {
185 if ( $job->getType() !== $this->type ) {
186 throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
187 }
188 }
189 wfProfileIn( __METHOD__ );
190 $ok = $this->doBatchPush( $jobs, $flags );
191 wfProfileOut( __METHOD__ );
192 return $ok;
193 }
194
195 /**
196 * @see JobQueue::batchPush()
197 * @return bool
198 */
199 abstract protected function doBatchPush( array $jobs, $flags );
200
201 /**
202 * Pop a job off of the queue.
203 * This requires $wgJobClasses to be set for the given job type.
204 *
205 * @return Job|bool Returns false on failure
206 */
207 final public function pop() {
208 wfProfileIn( __METHOD__ );
209 $job = $this->doPop();
210 wfProfileOut( __METHOD__ );
211 return $job;
212 }
213
214 /**
215 * @see JobQueue::pop()
216 * @return Job
217 */
218 abstract protected function doPop();
219
220 /**
221 * Acknowledge that a job was completed.
222 *
223 * This does nothing for certain queue classes or if "claimTTL" is not set.
224 *
225 * @param $job Job
226 * @throws MWException
227 * @return bool
228 */
229 final public function ack( Job $job ) {
230 if ( $job->getType() !== $this->type ) {
231 throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
232 }
233 wfProfileIn( __METHOD__ );
234 $ok = $this->doAck( $job );
235 wfProfileOut( __METHOD__ );
236 return $ok;
237 }
238
239 /**
240 * @see JobQueue::ack()
241 * @return bool
242 */
243 abstract protected function doAck( Job $job );
244
245 /**
246 * Register the "root job" of a given job into the queue for de-duplication.
247 * This should only be called right *after* all the new jobs have been inserted.
248 * This is used to turn older, duplicate, job entries into no-ops. The root job
249 * information will remain in the registry until it simply falls out of cache.
250 *
251 * This requires that $job has two special fields in the "params" array:
252 * - rootJobSignature : hash (e.g. SHA1) that identifies the task
253 * - rootJobTimestamp : TS_MW timestamp of this instance of the task
254 *
255 * A "root job" is a conceptual job that consist of potentially many smaller jobs
256 * that are actually inserted into the queue. For example, "refreshLinks" jobs are
257 * spawned when a template is edited. One can think of the task as "update links
258 * of pages that use template X" and an instance of that task as a "root job".
259 * However, what actually goes into the queue are potentially many refreshLinks2 jobs.
260 * Since these jobs include things like page ID ranges and DB master positions, and morph
261 * into smaller refreshLinks2 jobs recursively, simple duplicate detection (like job_sha1)
262 * for individual jobs being identical is not useful.
263 *
264 * In the case of "refreshLinks", if these jobs are still in the queue when the template
265 * is edited again, we want all of these old refreshLinks jobs for that template to become
266 * no-ops. This can greatly reduce server load, since refreshLinks jobs involves parsing.
267 * Essentially, the new batch of jobs belong to a new "root job" and the older ones to a
268 * previous "root job" for the same task of "update links of pages that use template X".
269 *
270 * This does nothing for certain queue classes.
271 *
272 * @param $job Job
273 * @throws MWException
274 * @return bool
275 */
276 final public function deduplicateRootJob( Job $job ) {
277 if ( $job->getType() !== $this->type ) {
278 throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
279 }
280 wfProfileIn( __METHOD__ );
281 $ok = $this->doDeduplicateRootJob( $job );
282 wfProfileOut( __METHOD__ );
283 return $ok;
284 }
285
286 /**
287 * @see JobQueue::deduplicateRootJob()
288 * @param $job Job
289 * @return bool
290 */
291 protected function doDeduplicateRootJob( Job $job ) {
292 return true;
293 }
294
295 /**
296 * Wait for any slaves or backup servers to catch up.
297 *
298 * This does nothing for certain queue classes.
299 *
300 * @return void
301 */
302 final public function waitForBackups() {
303 wfProfileIn( __METHOD__ );
304 $this->doWaitForBackups();
305 wfProfileOut( __METHOD__ );
306 }
307
308 /**
309 * @see JobQueue::waitForBackups()
310 * @return void
311 */
312 protected function doWaitForBackups() {}
313 }