Merge "[JobQueue] Added a JobQueueRedis class."
[lhc/web/wiklou.git] / includes / job / Job.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 */
23
24 /**
25 * Class to both describe a background job and handle jobs.
26 * The queue aspects of this class are now deprecated.
27 *
28 * @ingroup JobQueue
29 */
30 abstract class Job {
31 /**
32 * @var Title
33 */
34 var $title;
35
36 var $command,
37 $params,
38 $id,
39 $removeDuplicates,
40 $error;
41
42 /** @var Array Additional queue metadata */
43 public $metadata = array();
44
45 /*-------------------------------------------------------------------------
46 * Abstract functions
47 *------------------------------------------------------------------------*/
48
49 /**
50 * Run the job
51 * @return boolean success
52 */
53 abstract public function run();
54
55 /*-------------------------------------------------------------------------
56 * Static functions
57 *------------------------------------------------------------------------*/
58
59 /**
60 * Create the appropriate object to handle a specific job
61 *
62 * @param $command String: Job command
63 * @param $title Title: Associated title
64 * @param $params Array|bool: Job parameters
65 * @param $id Int: Job identifier
66 * @throws MWException
67 * @return Job
68 */
69 public static function factory( $command, Title $title, $params = false, $id = 0 ) {
70 global $wgJobClasses;
71 if( isset( $wgJobClasses[$command] ) ) {
72 $class = $wgJobClasses[$command];
73 return new $class( $title, $params, $id );
74 }
75 throw new MWException( "Invalid job command `{$command}`" );
76 }
77
78 /**
79 * Batch-insert a group of jobs into the queue.
80 * This will be wrapped in a transaction with a forced commit.
81 *
82 * This may add duplicate at insert time, but they will be
83 * removed later on, when the first one is popped.
84 *
85 * @param $jobs array of Job objects
86 * @deprecated 1.21
87 */
88 public static function batchInsert( $jobs ) {
89 return JobQueueGroup::singleton()->push( $jobs );
90 }
91
92 /**
93 * Insert a group of jobs into the queue.
94 *
95 * Same as batchInsert() but does not commit and can thus
96 * be rolled-back as part of a larger transaction. However,
97 * large batches of jobs can cause slave lag.
98 *
99 * @param $jobs array of Job objects
100 * @deprecated 1.21
101 */
102 public static function safeBatchInsert( $jobs ) {
103 return JobQueueGroup::singleton()->push( $jobs, JobQueue::QoS_Atomic );
104 }
105
106 /**
107 * Pop a job of a certain type. This tries less hard than pop() to
108 * actually find a job; it may be adversely affected by concurrent job
109 * runners.
110 *
111 * @param $type string
112 * @return Job
113 * @deprecated 1.21
114 */
115 public static function pop_type( $type ) {
116 return JobQueueGroup::singleton()->get( $type )->pop();
117 }
118
119 /**
120 * Pop a job off the front of the queue.
121 * This is subject to $wgJobTypesExcludedFromDefaultQueue.
122 *
123 * @return Job or false if there's no jobs
124 * @deprecated 1.21
125 */
126 public static function pop() {
127 return JobQueueGroup::singleton()->pop();
128 }
129
130 /*-------------------------------------------------------------------------
131 * Non-static functions
132 *------------------------------------------------------------------------*/
133
134 /**
135 * @param $command
136 * @param $title
137 * @param $params array|bool
138 * @param $id int
139 */
140 public function __construct( $command, $title, $params = false, $id = 0 ) {
141 $this->command = $command;
142 $this->title = $title;
143 $this->params = $params;
144 $this->id = $id;
145
146 $this->removeDuplicates = false; // expensive jobs may set this to true
147 }
148
149 /**
150 * @return integer May be 0 for jobs stored outside the DB
151 */
152 public function getId() {
153 return $this->id;
154 }
155
156 /**
157 * @return string
158 */
159 public function getType() {
160 return $this->command;
161 }
162
163 /**
164 * @return Title
165 */
166 public function getTitle() {
167 return $this->title;
168 }
169
170 /**
171 * @return array
172 */
173 public function getParams() {
174 return $this->params;
175 }
176
177 /**
178 * @return bool
179 */
180 public function ignoreDuplicates() {
181 return $this->removeDuplicates;
182 }
183
184 /**
185 * Subclasses may need to override this to make duplication detection work
186 *
187 * @return Array Map of key/values
188 */
189 public function getDeduplicationInfo() {
190 $info = array(
191 'type' => $this->getType(),
192 'namespace' => $this->getTitle()->getNamespace(),
193 'title' => $this->getTitle()->getDBkey(),
194 'params' => $this->getParams()
195 );
196 // Identical jobs with different "root" jobs should count as duplicates
197 if ( is_array( $info['params'] ) ) {
198 unset( $info['params']['rootJobSignature'] );
199 unset( $info['params']['rootJobTimestamp'] );
200 }
201 return $info;
202 }
203
204 /**
205 * @param $key string A key that identifies the task
206 * @return Array
207 */
208 public static function newRootJobParams( $key ) {
209 return array(
210 'rootJobSignature' => sha1( $key ),
211 'rootJobTimestamp' => wfTimestampNow()
212 );
213 }
214
215 /**
216 * @return Array
217 */
218 public function getRootJobParams() {
219 return array(
220 'rootJobSignature' => isset( $this->params['rootJobSignature'] )
221 ? $this->params['rootJobSignature']
222 : null,
223 'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
224 ? $this->params['rootJobTimestamp']
225 : null
226 );
227 }
228
229 /**
230 * Insert a single job into the queue.
231 * @return bool true on success
232 * @deprecated 1.21
233 */
234 public function insert() {
235 return JobQueueGroup::singleton()->push( $this );
236 }
237
238 /**
239 * @return string
240 */
241 public function toString() {
242 $paramString = '';
243 if ( $this->params ) {
244 foreach ( $this->params as $key => $value ) {
245 if ( $paramString != '' ) {
246 $paramString .= ' ';
247 }
248
249 if ( is_array( $value ) ) {
250 $value = "array(" . count( $value ) . ")";
251 } else if ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
252 $value = "object(" . get_class( $value ) . ")";
253 }
254
255 $paramString .= "$key=$value";
256 }
257 }
258
259 if ( is_object( $this->title ) ) {
260 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
261 if ( $paramString !== '' ) {
262 $s .= ' ' . $paramString;
263 }
264 return $s;
265 } else {
266 return "{$this->command} $paramString";
267 }
268 }
269
270 protected function setLastError( $error ) {
271 $this->error = $error;
272 }
273
274 public function getLastError() {
275 return $this->error;
276 }
277 }