Merge "ParserOutput::addLanguageLink needs a string"
[lhc/web/wiklou.git] / includes / job / JobQueueGroup.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 * @author Aaron Schulz
22 */
23
24 /**
25 * Class to handle enqueueing of background jobs
26 *
27 * @ingroup JobQueue
28 * @since 1.21
29 */
30 class JobQueueGroup {
31 /** @var Array */
32 protected static $instances = array();
33
34 protected $wiki; // string; wiki ID
35
36 const TYPE_DEFAULT = 1; // integer; job not in $wgJobTypesExcludedFromDefaultQueue
37 const TYPE_ANY = 2; // integer; any job
38
39 /**
40 * @param $wiki string Wiki ID
41 */
42 protected function __construct( $wiki ) {
43 $this->wiki = $wiki;
44 }
45
46 /**
47 * @param $wiki string Wiki ID
48 * @return JobQueueGroup
49 */
50 public static function singleton( $wiki = false ) {
51 $wiki = ( $wiki === false ) ? wfWikiID() : $wiki;
52 if ( !isset( self::$instances[$wiki] ) ) {
53 self::$instances[$wiki] = new self( $wiki );
54 }
55 return self::$instances[$wiki];
56 }
57
58 /**
59 * @param $type string
60 * @return JobQueue Job queue object for a given queue type
61 */
62 public function get( $type ) {
63 global $wgJobTypeConf;
64
65 $conf = array( 'wiki' => $this->wiki, 'type' => $type );
66 if ( isset( $wgJobTypeConf[$type] ) ) {
67 $conf = $conf + $wgJobTypeConf[$type];
68 } else {
69 $conf = $conf + $wgJobTypeConf['default'];
70 }
71
72 return JobQueue::factory( $conf );
73 }
74
75 /**
76 * Insert jobs into the respective queues of with the belong
77 *
78 * @param $jobs Job|array A single Job or a list of Jobs
79 * @throws MWException
80 * @return bool
81 */
82 public function push( $jobs ) {
83 $jobs = is_array( $jobs ) ? $jobs : array( $jobs );
84
85 $jobsByType = array(); // (job type => list of jobs)
86 foreach ( $jobs as $job ) {
87 if ( $job instanceof Job ) {
88 $jobsByType[$job->getType()][] = $job;
89 } else {
90 throw new MWException( "Attempted to push a non-Job object into a queue." );
91 }
92 }
93
94 $ok = true;
95 foreach ( $jobsByType as $type => $jobs ) {
96 if ( !$this->get( $type )->batchPush( $jobs ) ) {
97 $ok = false;
98 }
99 }
100
101 return $ok;
102 }
103
104 /**
105 * Pop a job off one of the job queues
106 *
107 * @param $type integer JobQueueGroup::TYPE_* constant
108 * @return Job|bool Returns false on failure
109 */
110 public function pop( $type = self::TYPE_DEFAULT ) {
111 $types = ( $type == self::TYPE_DEFAULT )
112 ? $this->getDefaultQueueTypes()
113 : $this->getQueueTypes();
114 shuffle( $types ); // avoid starvation
115
116 foreach ( $types as $type ) { // for each queue...
117 $job = $this->get( $type )->pop();
118 if ( $job ) {
119 return $job; // found
120 }
121 }
122
123 return false; // no jobs found
124 }
125
126 /**
127 * Acknowledge that a job was completed
128 *
129 * @param $job Job
130 * @return bool
131 */
132 public function ack( Job $job ) {
133 return $this->get( $job->getType() )->ack( $job );
134 }
135
136 /**
137 * Register the "root job" of a given job into the queue for de-duplication.
138 * This should only be called right *after* all the new jobs have been inserted.
139 *
140 * @param $job Job
141 * @return bool
142 */
143 public function deduplicateRootJob( Job $job ) {
144 return $this->get( $job->getType() )->deduplicateRootJob( $job );
145 }
146
147 /**
148 * Get the list of queue types
149 *
150 * @return array List of strings
151 */
152 public function getQueueTypes() {
153 global $wgJobClasses;
154
155 return array_keys( $wgJobClasses );
156 }
157
158 /**
159 * Get the list of default queue types
160 *
161 * @return array List of strings
162 */
163 public function getDefaultQueueTypes() {
164 global $wgJobTypesExcludedFromDefaultQueue;
165
166 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
167 }
168
169 /**
170 * @return Array List of job types that have non-empty queues
171 */
172 public function getQueuesWithJobs() {
173 $types = array();
174 foreach ( $this->getQueueTypes() as $type ) {
175 if ( !$this->get( $type )->isEmpty() ) {
176 $types[] = $type;
177 }
178 }
179 return $types;
180 }
181 }