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.
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.
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
21 * @defgroup JobQueue JobQueue
25 * Class to both describe a background job and handle jobs.
26 * The queue aspects of this class are now deprecated.
42 /*-------------------------------------------------------------------------
44 *------------------------------------------------------------------------*/
48 * @return boolean success
50 abstract public function run();
52 /*-------------------------------------------------------------------------
54 *------------------------------------------------------------------------*/
57 * Create the appropriate object to handle a specific job
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
66 public static function factory( $command, Title
$title, $params = false, $id = 0 ) {
68 if( isset( $wgJobClasses[$command] ) ) {
69 $class = $wgJobClasses[$command];
70 return new $class( $title, $params, $id );
72 throw new MWException( "Invalid job command `{$command}`" );
76 * Batch-insert a group of jobs into the queue.
77 * This will be wrapped in a transaction with a forced commit.
79 * This may add duplicate at insert time, but they will be
80 * removed later on, when the first one is popped.
82 * @param $jobs array of Job objects
85 public static function batchInsert( $jobs ) {
86 return JobQueueGroup
::singleton()->push( $jobs );
90 * Insert a group of jobs into the queue.
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.
96 * @param $jobs array of Job objects
99 public static function safeBatchInsert( $jobs ) {
100 return JobQueueGroup
::singleton()->push( $jobs, JobQueue
::QoS_Atomic
);
104 * Pop a job of a certain type. This tries less hard than pop() to
105 * actually find a job; it may be adversely affected by concurrent job
108 * @param $type string
112 public static function pop_type( $type ) {
113 return JobQueueGroup
::singleton()->get( $type )->pop();
117 * Pop a job off the front of the queue.
118 * This is subject to $wgJobTypesExcludedFromDefaultQueue.
120 * @return Job or false if there's no jobs
123 public static function pop() {
124 return JobQueueGroup
::singleton()->pop();
127 /*-------------------------------------------------------------------------
128 * Non-static functions
129 *------------------------------------------------------------------------*/
134 * @param $params array|bool
137 public function __construct( $command, $title, $params = false, $id = 0 ) {
138 $this->command
= $command;
139 $this->title
= $title;
140 $this->params
= $params;
143 $this->removeDuplicates
= false; // expensive jobs may set this to true
147 * @return integer May be 0 for jobs stored outside the DB
149 public function getId() {
156 public function getType() {
157 return $this->command
;
163 public function getTitle() {
170 public function getParams() {
171 return $this->params
;
177 public function ignoreDuplicates() {
178 return $this->removeDuplicates
;
182 * Insert a single job into the queue.
183 * @return bool true on success
186 public function insert() {
187 return JobQueueGroup
::singleton()->push( $this );
193 public function toString() {
195 if ( $this->params
) {
196 foreach ( $this->params
as $key => $value ) {
197 if ( $paramString != '' ) {
200 $paramString .= "$key=$value";
204 if ( is_object( $this->title
) ) {
205 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
206 if ( $paramString !== '' ) {
207 $s .= ' ' . $paramString;
211 return "{$this->command} $paramString";
215 protected function setLastError( $error ) {
216 $this->error
= $error;
219 public function getLastError() {