[JobQueue] Throttle refreshLinks2 jobs based on finishing the refreshLinks jobs.
[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 or persistent cache
43
44 const PROC_CACHE_TTL = 15; // integer; seconds
45
46 const CACHE_VERSION = 1; // integer; cache version
47
48 /**
49 * @param $wiki string Wiki ID
50 */
51 protected function __construct( $wiki ) {
52 $this->wiki = $wiki;
53 $this->cache = new ProcessCacheLRU( 10 );
54 }
55
56 /**
57 * @param $wiki string Wiki ID
58 * @return JobQueueGroup
59 */
60 public static function singleton( $wiki = false ) {
61 $wiki = ( $wiki === false ) ? wfWikiID() : $wiki;
62 if ( !isset( self::$instances[$wiki] ) ) {
63 self::$instances[$wiki] = new self( $wiki );
64 }
65 return self::$instances[$wiki];
66 }
67
68 /**
69 * Destroy the singleton instances
70 *
71 * @return void
72 */
73 public static function destroySingletons() {
74 self::$instances = array();
75 }
76
77 /**
78 * Get the job queue object for a given queue type
79 *
80 * @param $type string
81 * @return JobQueue
82 */
83 public function get( $type ) {
84 global $wgJobTypeConf;
85
86 $conf = array( 'wiki' => $this->wiki, 'type' => $type );
87 if ( isset( $wgJobTypeConf[$type] ) ) {
88 $conf = $conf + $wgJobTypeConf[$type];
89 } else {
90 $conf = $conf + $wgJobTypeConf['default'];
91 }
92
93 return JobQueue::factory( $conf );
94 }
95
96 /**
97 * Insert jobs into the respective queues of with the belong.
98 *
99 * This inserts the jobs into the queue specified by $wgJobTypeConf
100 * and updates the aggregate job queue information cache as needed.
101 *
102 * @param $jobs Job|array A single Job or a list of Jobs
103 * @throws MWException
104 * @return bool
105 */
106 public function push( $jobs ) {
107 $jobs = is_array( $jobs ) ? $jobs : array( $jobs );
108
109 $jobsByType = array(); // (job type => list of jobs)
110 foreach ( $jobs as $job ) {
111 if ( $job instanceof Job ) {
112 $jobsByType[$job->getType()][] = $job;
113 } else {
114 throw new MWException( "Attempted to push a non-Job object into a queue." );
115 }
116 }
117
118 $ok = true;
119 foreach ( $jobsByType as $type => $jobs ) {
120 if ( $this->get( $type )->push( $jobs ) ) {
121 JobQueueAggregator::singleton()->notifyQueueNonEmpty( $this->wiki, $type );
122 } else {
123 $ok = false;
124 }
125 }
126
127 if ( $this->cache->has( 'queues-ready', 'list' ) ) {
128 $list = $this->cache->get( 'queues-ready', 'list' );
129 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
130 $this->cache->clear( 'queues-ready' );
131 }
132 }
133
134 return $ok;
135 }
136
137 /**
138 * Pop a job off one of the job queues
139 *
140 * This pops a job off a queue as specified by $wgJobTypeConf and
141 * updates the aggregate job queue information cache as needed.
142 *
143 * @param $qtype integer|string JobQueueGroup::TYPE_DEFAULT or type string
144 * @param $flags integer Bitfield of JobQueueGroup::USE_* constants
145 * @return Job|bool Returns false on failure
146 */
147 public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0 ) {
148 if ( is_string( $qtype ) ) { // specific job type
149 $job = $this->get( $qtype )->pop();
150 if ( !$job ) {
151 JobQueueAggregator::singleton()->notifyQueueEmpty( $this->wiki, $qtype );
152 }
153 return $job;
154 } else { // any job in the "default" jobs types
155 if ( $flags & self::USE_CACHE ) {
156 if ( !$this->cache->has( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) {
157 $this->cache->set( 'queues-ready', 'list', $this->getQueuesWithJobs() );
158 }
159 $types = $this->cache->get( 'queues-ready', 'list' );
160 } else {
161 $types = $this->getQueuesWithJobs();
162 }
163
164 if ( $qtype == self::TYPE_DEFAULT ) {
165 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
166 }
167 shuffle( $types ); // avoid starvation
168
169 foreach ( $types as $type ) { // for each queue...
170 $job = $this->get( $type )->pop();
171 if ( $job ) { // found
172 return $job;
173 } else { // not found
174 JobQueueAggregator::singleton()->notifyQueueEmpty( $this->wiki, $type );
175 $this->cache->clear( 'queues-ready' );
176 }
177 }
178
179 return false; // no jobs found
180 }
181 }
182
183 /**
184 * Acknowledge that a job was completed
185 *
186 * @param $job Job
187 * @return bool
188 */
189 public function ack( Job $job ) {
190 return $this->get( $job->getType() )->ack( $job );
191 }
192
193 /**
194 * Register the "root job" of a given job into the queue for de-duplication.
195 * This should only be called right *after* all the new jobs have been inserted.
196 *
197 * @param $job Job
198 * @return bool
199 */
200 public function deduplicateRootJob( Job $job ) {
201 return $this->get( $job->getType() )->deduplicateRootJob( $job );
202 }
203
204 /**
205 * Get the list of queue types
206 *
207 * @return array List of strings
208 */
209 public function getQueueTypes() {
210 return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
211 }
212
213 /**
214 * Get the list of default queue types
215 *
216 * @return array List of strings
217 */
218 public function getDefaultQueueTypes() {
219 global $wgJobTypesExcludedFromDefaultQueue;
220
221 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
222 }
223
224 /**
225 * Get the list of job types that have non-empty queues
226 *
227 * @return Array List of job types that have non-empty queues
228 */
229 public function getQueuesWithJobs() {
230 $types = array();
231 foreach ( $this->getQueueTypes() as $type ) {
232 if ( !$this->get( $type )->isEmpty() ) {
233 $types[] = $type;
234 }
235 }
236 return $types;
237 }
238
239 /**
240 * Check if jobs should not be popped of a queue right now.
241 * This is only used for performance, such as to avoid spamming
242 * the queue with many sub-jobs before they actually get run.
243 *
244 * @param $type string
245 * @return bool
246 */
247 public function isQueueDeprioritized( $type ) {
248 if ( $type === 'refreshLinks2' ) {
249 // Don't keep converting refreshLinks2 => refreshLinks jobs if the
250 // later jobs have not been done yet. This helps throttle queue spam.
251 return !$this->get( 'refreshLinks' )->isEmpty();
252 }
253 return false;
254 }
255
256 /**
257 * Execute any due periodic queue maintenance tasks for all queues.
258 *
259 * A task is "due" if the time ellapsed since the last run is greater than
260 * the defined run period. Concurrent calls to this function will cause tasks
261 * to be attempted twice, so they may need their own methods of mutual exclusion.
262 *
263 * @return integer Number of tasks run
264 */
265 public function executeReadyPeriodicTasks() {
266 global $wgMemc;
267
268 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
269 $key = wfForeignMemcKey( $db, $prefix, 'jobqueuegroup', 'taskruns', 'v1' );
270 $lastRuns = $wgMemc->get( $key ); // (queue => task => UNIX timestamp)
271
272 $count = 0;
273 $tasksRun = array(); // (queue => task => UNIX timestamp)
274 foreach ( $this->getQueueTypes() as $type ) {
275 $queue = $this->get( $type );
276 foreach ( $queue->getPeriodicTasks() as $task => $definition ) {
277 if ( $definition['period'] <= 0 ) {
278 continue; // disabled
279 } elseif ( !isset( $lastRuns[$type][$task] )
280 || $lastRuns[$type][$task] < ( time() - $definition['period'] ) )
281 {
282 if ( call_user_func( $definition['callback'] ) !== null ) {
283 $tasksRun[$type][$task] = time();
284 ++$count;
285 }
286 }
287 }
288 }
289
290 $wgMemc->merge( $key, function( $cache, $key, $lastRuns ) use ( $tasksRun ) {
291 if ( is_array( $lastRuns ) ) {
292 foreach ( $tasksRun as $type => $tasks ) {
293 foreach ( $tasks as $task => $timestamp ) {
294 if ( !isset( $lastRuns[$type][$task] )
295 || $timestamp > $lastRuns[$type][$task] )
296 {
297 $lastRuns[$type][$task] = $timestamp;
298 }
299 }
300 }
301 } else {
302 $lastRuns = $tasksRun;
303 }
304 return $lastRuns;
305 } );
306
307 return $count;
308 }
309
310 /**
311 * @param $name string
312 * @return mixed
313 */
314 private function getCachedConfigVar( $name ) {
315 global $wgConf, $wgMemc;
316
317 if ( $this->wiki === wfWikiID() ) {
318 return $GLOBALS[$name]; // common case
319 } else {
320 list( $db, $prefix ) = wfSplitWikiID( $this->wiki );
321 $key = wfForeignMemcKey( $db, $prefix, 'configvalue', $name );
322 $value = $wgMemc->get( $key ); // ('v' => ...) or false
323 if ( is_array( $value ) ) {
324 return $value['v'];
325 } else {
326 $value = $wgConf->getConfig( $this->wiki, $name );
327 $wgMemc->set( $key, array( 'v' => $value ), 86400 + mt_rand( 0, 86400 ) );
328 return $value;
329 }
330 }
331 }
332 }