Merge "Add some entries removed in I41f1995d back."
[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 /**
40 * @param $params array
41 */
42 protected function __construct( array $params ) {
43 $this->wiki = $params['wiki'];
44 $this->type = $params['type'];
45 $this->order = isset( $params['order'] ) ? $params['order'] : 'random';
46 $this->claimTTL = isset( $params['claimTTL'] ) ? $params['claimTTL'] : 0;
47 }
48
49 /**
50 * Get a job queue object of the specified type.
51 * $params includes:
52 * class : What job class to use (determines job type)
53 * wiki : wiki ID of the wiki the jobs are for (defaults to current wiki)
54 * type : The name of the job types this queue handles
55 * order : Order that pop() selects jobs, one of "fifo", "timestamp" or "random".
56 * If "fifo" is used, the queue will effectively be FIFO. Note that
57 * job completion will not appear to be exactly FIFO if there are multiple
58 * job runners since jobs can take different times to finish once popped.
59 * If "timestamp" is used, the queue will at least be loosely ordered
60 * by timestamp, allowing for some jobs to be popped off out of order.
61 * If "random" is used, pop() will pick jobs in random order. This might be
62 * useful for improving concurrency depending on the queue storage medium.
63 * claimTTL : If supported, the queue will recycle jobs that have been popped
64 * but not acknowledged as completed after this many seconds.
65 *
66 * @param $params array
67 * @return JobQueue
68 * @throws MWException
69 */
70 final public static function factory( array $params ) {
71 $class = $params['class'];
72 if ( !MWInit::classExists( $class ) ) {
73 throw new MWException( "Invalid job queue class '$class'." );
74 }
75 $obj = new $class( $params );
76 if ( !( $obj instanceof self ) ) {
77 throw new MWException( "Class '$class' is not a " . __CLASS__ . " class." );
78 }
79 return $obj;
80 }
81
82 /**
83 * @return string Wiki ID
84 */
85 final public function getWiki() {
86 return $this->wiki;
87 }
88
89 /**
90 * @return string Job type that this queue handles
91 */
92 final public function getType() {
93 return $this->type;
94 }
95
96 /**
97 * Quickly check if the queue is empty.
98 * Queue classes should use caching if they are any slower without memcached.
99 *
100 * @return bool
101 */
102 final public function isEmpty() {
103 wfProfileIn( __METHOD__ );
104 $res = $this->doIsEmpty();
105 wfProfileOut( __METHOD__ );
106 return $res;
107 }
108
109 /**
110 * @see JobQueue::isEmpty()
111 * @return bool
112 */
113 abstract protected function doIsEmpty();
114
115 /**
116 * Push a batch of jobs into the queue
117 *
118 * @param $jobs array List of Jobs
119 * @param $flags integer Bitfield (supports JobQueue::QoS_Atomic)
120 * @throws MWException
121 * @return bool
122 */
123 final public function batchPush( array $jobs, $flags = 0 ) {
124 foreach ( $jobs as $job ) {
125 if ( $job->getType() !== $this->type ) {
126 throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
127 }
128 }
129 wfProfileIn( __METHOD__ );
130 $ok = $this->doBatchPush( $jobs, $flags );
131 wfProfileOut( __METHOD__ );
132 return $ok;
133 }
134
135 /**
136 * @see JobQueue::batchPush()
137 * @return bool
138 */
139 abstract protected function doBatchPush( array $jobs, $flags );
140
141 /**
142 * Pop a job off of the queue
143 *
144 * @return Job|bool Returns false on failure
145 */
146 final public function pop() {
147 wfProfileIn( __METHOD__ );
148 $job = $this->doPop();
149 wfProfileOut( __METHOD__ );
150 return $job;
151 }
152
153 /**
154 * @see JobQueue::pop()
155 * @return Job
156 */
157 abstract protected function doPop();
158
159 /**
160 * Acknowledge that a job was completed
161 *
162 * @param $job Job
163 * @throws MWException
164 * @return bool
165 */
166 final public function ack( Job $job ) {
167 if ( $job->getType() !== $this->type ) {
168 throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
169 }
170 wfProfileIn( __METHOD__ );
171 $ok = $this->doAck( $job );
172 wfProfileOut( __METHOD__ );
173 return $ok;
174 }
175
176 /**
177 * @see JobQueue::ack()
178 * @return bool
179 */
180 abstract protected function doAck( Job $job );
181
182 /**
183 * Register the "root job" of a given job into the queue for de-duplication.
184 * This should only be called right *after* all the new jobs have been inserted.
185 * This is used to turn older, duplicate, job entries into no-ops. The root job
186 * information will remain in the registry until it simply falls out of cache.
187 *
188 * This requires that $job has two special fields in the "params" array:
189 * - rootJobSignature : hash (e.g. SHA1) that identifies the task
190 * - rootJobTimestamp : TS_MW timestamp of this instance of the task
191 *
192 * A "root job" is a conceptual job that consist of potentially many smaller jobs
193 * that are actually inserted into the queue. For example, "refreshLinks" jobs are
194 * spawned when a template is edited. One can think of the task as "update links
195 * of pages that use template X" and an instance of that task as a "root job".
196 * However, what actually goes into the queue are potentially many refreshLinks2 jobs.
197 * Since these jobs include things like page ID ranges and DB master positions, and morph
198 * into smaller refreshLinks2 jobs recursively, simple duplicate detection (like job_sha1)
199 * for individual jobs being identical is not useful.
200 *
201 * In the case of "refreshLinks", if these jobs are still in the queue when the template
202 * is edited again, we want all of these old refreshLinks jobs for that template to become
203 * no-ops. This can greatly reduce server load, since refreshLinks jobs involves parsing.
204 * Essentially, the new batch of jobs belong to a new "root job" and the older ones to a
205 * previous "root job" for the same task of "update links of pages that use template X".
206 *
207 * @param $job Job
208 * @throws MWException
209 * @return bool
210 */
211 final public function deduplicateRootJob( Job $job ) {
212 if ( $job->getType() !== $this->type ) {
213 throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
214 }
215 wfProfileIn( __METHOD__ );
216 $ok = $this->doDeduplicateRootJob( $job );
217 wfProfileOut( __METHOD__ );
218 return $ok;
219 }
220
221 /**
222 * @see JobQueue::deduplicateRootJob()
223 * @param $job Job
224 * @return bool
225 */
226 protected function doDeduplicateRootJob( Job $job ) {
227 return true;
228 }
229
230 /**
231 * Wait for any slaves or backup servers to catch up
232 *
233 * @return void
234 */
235 final public function waitForBackups() {
236 wfProfileIn( __METHOD__ );
237 $this->doWaitForBackups();
238 wfProfileOut( __METHOD__ );
239 }
240
241 /**
242 * @see JobQueue::waitForBackups()
243 * @return void
244 */
245 protected function doWaitForBackups() {}
246 }