[JobQueue] Job queue refactoring and generalizing.
[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 * This 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 /*-------------------------------------------------------------------------
43 * Abstract functions
44 *------------------------------------------------------------------------*/
45
46 /**
47 * Run the job
48 * @return boolean success
49 */
50 abstract public function run();
51
52 /*-------------------------------------------------------------------------
53 * Static functions
54 *------------------------------------------------------------------------*/
55
56 /**
57 * Create the appropriate object to handle a specific job
58 *
59 * @param $command String: Job command
60 * @param $title Title: Associated title
61 * @param $params Array|bool: Job parameters
62 * @param $id Int: Job identifier
63 * @throws MWException
64 * @return Job
65 */
66 public static function factory( $command, Title $title, $params = false, $id = 0 ) {
67 global $wgJobClasses;
68 if( isset( $wgJobClasses[$command] ) ) {
69 $class = $wgJobClasses[$command];
70 return new $class( $title, $params, $id );
71 }
72 throw new MWException( "Invalid job command `{$command}`" );
73 }
74
75 /**
76 * Batch-insert a group of jobs into the queue.
77 * This will be wrapped in a transaction with a forced commit.
78 *
79 * This may add duplicate at insert time, but they will be
80 * removed later on, when the first one is popped.
81 *
82 * @param $jobs array of Job objects
83 * @deprecated 1.20
84 */
85 public static function batchInsert( $jobs ) {
86 return JobQueueGroup::singleton()->push( $jobs );
87 }
88
89 /**
90 * Insert a group of jobs into the queue.
91 *
92 * Same as batchInsert() but does not commit and can thus
93 * be rolled-back as part of a larger transaction. However,
94 * large batches of jobs can cause slave lag.
95 *
96 * @param $jobs array of Job objects
97 * @deprecated 1.20
98 */
99 public static function safeBatchInsert( $jobs ) {
100 return JobQueueGroup::singleton()->push( $jobs, JobQueue::QoS_Atomic );
101 }
102
103 /*-------------------------------------------------------------------------
104 * Non-static functions
105 *------------------------------------------------------------------------*/
106
107 /**
108 * @param $command
109 * @param $title
110 * @param $params array|bool
111 * @param $id int
112 */
113 public function __construct( $command, $title, $params = false, $id = 0 ) {
114 $this->command = $command;
115 $this->title = $title;
116 $this->params = $params;
117 $this->id = $id;
118
119 $this->removeDuplicates = false; // expensive jobs may set this to true
120 }
121
122 /**
123 * @return integer May be 0 for jobs stored outside the DB
124 */
125 public function getId() {
126 return $this->id;
127 }
128
129 /**
130 * @return string
131 */
132 public function getType() {
133 return $this->command;
134 }
135
136 /**
137 * @return Title
138 */
139 public function getTitle() {
140 return $this->title;
141 }
142
143 /**
144 * @return array
145 */
146 public function getParams() {
147 return $this->params;
148 }
149
150 /**
151 * @return bool
152 */
153 public function ignoreDuplicates() {
154 return $this->removeDuplicates;
155 }
156
157 /**
158 * Insert a single job into the queue.
159 * @return bool true on success
160 * @deprecated 1.20
161 */
162 public function insert() {
163 return JobQueueGroup::singleton()->push( $this );
164 }
165
166 /**
167 * @return string
168 */
169 public function toString() {
170 $paramString = '';
171 if ( $this->params ) {
172 foreach ( $this->params as $key => $value ) {
173 if ( $paramString != '' ) {
174 $paramString .= ' ';
175 }
176 $paramString .= "$key=$value";
177 }
178 }
179
180 if ( is_object( $this->title ) ) {
181 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
182 if ( $paramString !== '' ) {
183 $s .= ' ' . $paramString;
184 }
185 return $s;
186 } else {
187 return "{$this->command} $paramString";
188 }
189 }
190
191 protected function setLastError( $error ) {
192 $this->error = $error;
193 }
194
195 public function getLastError() {
196 return $this->error;
197 }
198 }