cf0215b8bfb83be6c17b818f4fcc1e2e0dfbce55
[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 /** @var ProcessCacheLRU */
35 protected $cache;
36
37 protected $wiki; // string; wiki ID
38
39 const TYPE_DEFAULT = 1; // integer; jobs popped by default
40 const TYPE_ANY = 2; // integer; any job
41
42 const USE_CACHE = 1; // integer; use process cache
43
44 const PROC_CACHE_TTL = 15; // integer; seconds
45
46 /**
47 * @param $wiki string Wiki ID
48 */
49 protected function __construct( $wiki ) {
50 $this->wiki = $wiki;
51 $this->cache = new ProcessCacheLRU( 1 );
52 }
53
54 /**
55 * @param $wiki string Wiki ID
56 * @return JobQueueGroup
57 */
58 public static function singleton( $wiki = false ) {
59 $wiki = ( $wiki === false ) ? wfWikiID() : $wiki;
60 if ( !isset( self::$instances[$wiki] ) ) {
61 self::$instances[$wiki] = new self( $wiki );
62 }
63 return self::$instances[$wiki];
64 }
65
66 /**
67 * Destroy the singleton instances
68 *
69 * @return void
70 */
71 public static function destroySingletons() {
72 self::$instances = array();
73 }
74
75 /**
76 * @param $type string
77 * @return JobQueue Job queue object for a given queue type
78 */
79 public function get( $type ) {
80 global $wgJobTypeConf;
81
82 $conf = array( 'wiki' => $this->wiki, 'type' => $type );
83 if ( isset( $wgJobTypeConf[$type] ) ) {
84 $conf = $conf + $wgJobTypeConf[$type];
85 } else {
86 $conf = $conf + $wgJobTypeConf['default'];
87 }
88
89 return JobQueue::factory( $conf );
90 }
91
92 /**
93 * Insert jobs into the respective queues of with the belong.
94 * This inserts the jobs into the queue specified by $wgJobTypeConf.
95 *
96 * @param $jobs Job|array A single Job or a list of Jobs
97 * @throws MWException
98 * @return bool
99 */
100 public function push( $jobs ) {
101 $jobs = is_array( $jobs ) ? $jobs : array( $jobs );
102
103 $jobsByType = array(); // (job type => list of jobs)
104 foreach ( $jobs as $job ) {
105 if ( $job instanceof Job ) {
106 $jobsByType[$job->getType()][] = $job;
107 } else {
108 throw new MWException( "Attempted to push a non-Job object into a queue." );
109 }
110 }
111
112 $ok = true;
113 foreach ( $jobsByType as $type => $jobs ) {
114 if ( !$this->get( $type )->push( $jobs ) ) {
115 $ok = false;
116 }
117 }
118
119 if ( $this->cache->has( 'queues-ready', 'list' ) ) {
120 $list = $this->cache->get( 'queues-ready', 'list' );
121 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
122 $this->cache->clear( 'queues-ready' );
123 }
124 }
125
126 return $ok;
127 }
128
129 /**
130 * Pop a job off one of the job queues
131 *
132 * @param $queueType integer JobQueueGroup::TYPE_* constant
133 * @param $flags integer Bitfield of JobQueueGroup::USE_* constants
134 * @return Job|bool Returns false on failure
135 */
136 public function pop( $queueType = self::TYPE_DEFAULT, $flags = 0 ) {
137 if ( $flags & self::USE_CACHE ) {
138 if ( !$this->cache->has( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) {
139 $this->cache->set( 'queues-ready', 'list', $this->getQueuesWithJobs() );
140 }
141 $types = $this->cache->get( 'queues-ready', 'list' );
142 } else {
143 $types = $this->getQueuesWithJobs();
144 }
145
146 if ( $queueType == self::TYPE_DEFAULT ) {
147 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
148 }
149 shuffle( $types ); // avoid starvation
150
151 foreach ( $types as $type ) { // for each queue...
152 $job = $this->get( $type )->pop();
153 if ( $job ) { // found
154 return $job;
155 } else { // not found
156 $this->cache->clear( 'queues-ready' );
157 }
158 }
159
160 return false; // no jobs found
161 }
162
163 /**
164 * Acknowledge that a job was completed
165 *
166 * @param $job Job
167 * @return bool
168 */
169 public function ack( Job $job ) {
170 return $this->get( $job->getType() )->ack( $job );
171 }
172
173 /**
174 * Register the "root job" of a given job into the queue for de-duplication.
175 * This should only be called right *after* all the new jobs have been inserted.
176 *
177 * @param $job Job
178 * @return bool
179 */
180 public function deduplicateRootJob( Job $job ) {
181 return $this->get( $job->getType() )->deduplicateRootJob( $job );
182 }
183
184 /**
185 * Get the list of queue types
186 *
187 * @return array List of strings
188 */
189 public function getQueueTypes() {
190 global $wgJobClasses;
191
192 return array_keys( $wgJobClasses );
193 }
194
195 /**
196 * Get the list of default queue types
197 *
198 * @return array List of strings
199 */
200 public function getDefaultQueueTypes() {
201 global $wgJobTypesExcludedFromDefaultQueue;
202
203 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
204 }
205
206 /**
207 * @return Array List of job types that have non-empty queues
208 */
209 public function getQueuesWithJobs() {
210 $types = array();
211 foreach ( $this->getQueueTypes() as $type ) {
212 if ( !$this->get( $type )->isEmpty() ) {
213 $types[] = $type;
214 }
215 }
216 return $types;
217 }
218
219 /**
220 * @return Array List of default job types that have non-empty queues
221 */
222 public function getDefaultQueuesWithJobs() {
223 $types = array();
224 foreach ( $this->getDefaultQueueTypes() as $type ) {
225 if ( !$this->get( $type )->isEmpty() ) {
226 $types[] = $type;
227 }
228 }
229 return $types;
230 }
231 }