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 * @author Aaron Schulz
25 * Class to handle enqueueing of background jobs
32 protected static $instances = array();
34 /** @var ProcessCacheLRU */
37 protected $wiki; // string; wiki ID
39 const TYPE_DEFAULT
= 1; // integer; jobs popped by default
40 const TYPE_ANY
= 2; // integer; any job
42 const USE_CACHE
= 1; // integer; use process or persistent cache
43 const USE_PRIORITY
= 2; // integer; respect deprioritization
45 const PROC_CACHE_TTL
= 15; // integer; seconds
47 const CACHE_VERSION
= 1; // integer; cache version
50 * @param string $wiki Wiki ID
52 protected function __construct( $wiki ) {
54 $this->cache
= new ProcessCacheLRU( 10 );
58 * @param string $wiki Wiki ID
59 * @return JobQueueGroup
61 public static function singleton( $wiki = false ) {
62 $wiki = ( $wiki === false ) ?
wfWikiID() : $wiki;
63 if ( !isset( self
::$instances[$wiki] ) ) {
64 self
::$instances[$wiki] = new self( $wiki );
66 return self
::$instances[$wiki];
70 * Destroy the singleton instances
74 public static function destroySingletons() {
75 self
::$instances = array();
79 * Get the job queue object for a given queue type
84 public function get( $type ) {
85 global $wgJobTypeConf;
87 $conf = array( 'wiki' => $this->wiki
, 'type' => $type );
88 if ( isset( $wgJobTypeConf[$type] ) ) {
89 $conf = $conf +
$wgJobTypeConf[$type];
91 $conf = $conf +
$wgJobTypeConf['default'];
94 return JobQueue
::factory( $conf );
98 * Insert jobs into the respective queues of with the belong.
100 * This inserts the jobs into the queue specified by $wgJobTypeConf
101 * and updates the aggregate job queue information cache as needed.
103 * @param $jobs Job|array A single Job or a list of Jobs
104 * @throws MWException
107 public function push( $jobs ) {
108 $jobs = is_array( $jobs ) ?
$jobs : array( $jobs );
110 $jobsByType = array(); // (job type => list of jobs)
111 foreach ( $jobs as $job ) {
112 if ( $job instanceof Job
) {
113 $jobsByType[$job->getType()][] = $job;
115 throw new MWException( "Attempted to push a non-Job object into a queue." );
120 foreach ( $jobsByType as $type => $jobs ) {
121 if ( $this->get( $type )->push( $jobs ) ) {
122 JobQueueAggregator
::singleton()->notifyQueueNonEmpty( $this->wiki
, $type );
128 if ( $this->cache
->has( 'queues-ready', 'list' ) ) {
129 $list = $this->cache
->get( 'queues-ready', 'list' );
130 if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
131 $this->cache
->clear( 'queues-ready' );
139 * Pop a job off one of the job queues
141 * This pops a job off a queue as specified by $wgJobTypeConf and
142 * updates the aggregate job queue information cache as needed.
144 * @param $qtype integer|string JobQueueGroup::TYPE_DEFAULT or type string
145 * @param $flags integer Bitfield of JobQueueGroup::USE_* constants
146 * @return Job|bool Returns false on failure
148 public function pop( $qtype = self
::TYPE_DEFAULT
, $flags = 0 ) {
149 if ( is_string( $qtype ) ) { // specific job type
150 if ( ( $flags & self
::USE_PRIORITY
) && $this->isQueueDeprioritized( $qtype ) ) {
151 return false; // back off
153 $job = $this->get( $qtype )->pop();
155 JobQueueAggregator
::singleton()->notifyQueueEmpty( $this->wiki
, $qtype );
158 } else { // any job in the "default" jobs types
159 if ( $flags & self
::USE_CACHE
) {
160 if ( !$this->cache
->has( 'queues-ready', 'list', self
::PROC_CACHE_TTL
) ) {
161 $this->cache
->set( 'queues-ready', 'list', $this->getQueuesWithJobs() );
163 $types = $this->cache
->get( 'queues-ready', 'list' );
165 $types = $this->getQueuesWithJobs();
168 if ( $qtype == self
::TYPE_DEFAULT
) {
169 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
171 shuffle( $types ); // avoid starvation
173 foreach ( $types as $type ) { // for each queue...
174 if ( ( $flags & self
::USE_PRIORITY
) && $this->isQueueDeprioritized( $type ) ) {
175 continue; // back off
177 $job = $this->get( $type )->pop();
178 if ( $job ) { // found
180 } else { // not found
181 JobQueueAggregator
::singleton()->notifyQueueEmpty( $this->wiki
, $type );
182 $this->cache
->clear( 'queues-ready' );
186 return false; // no jobs found
191 * Acknowledge that a job was completed
196 public function ack( Job
$job ) {
197 return $this->get( $job->getType() )->ack( $job );
201 * Register the "root job" of a given job into the queue for de-duplication.
202 * This should only be called right *after* all the new jobs have been inserted.
207 public function deduplicateRootJob( Job
$job ) {
208 return $this->get( $job->getType() )->deduplicateRootJob( $job );
212 * Wait for any slaves or backup queue servers to catch up.
214 * This does nothing for certain queue classes.
217 * @throws MWException
219 public function waitForBackups() {
220 global $wgJobTypeConf;
222 wfProfileIn( __METHOD__
);
223 // Try to avoid doing this more than once per queue storage medium
224 foreach ( $wgJobTypeConf as $type => $conf ) {
225 $this->get( $type )->waitForBackups();
227 wfProfileOut( __METHOD__
);
231 * Get the list of queue types
233 * @return array List of strings
235 public function getQueueTypes() {
236 return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
240 * Get the list of default queue types
242 * @return array List of strings
244 public function getDefaultQueueTypes() {
245 global $wgJobTypesExcludedFromDefaultQueue;
247 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
251 * Get the list of job types that have non-empty queues
253 * @return Array List of job types that have non-empty queues
255 public function getQueuesWithJobs() {
257 foreach ( $this->getQueueTypes() as $type ) {
258 if ( !$this->get( $type )->isEmpty() ) {
266 * Check if jobs should not be popped of a queue right now.
267 * This is only used for performance, such as to avoid spamming
268 * the queue with many sub-jobs before they actually get run.
270 * @param $type string
273 public function isQueueDeprioritized( $type ) {
274 if ( $this->cache
->has( 'isDeprioritized', $type, 5 ) ) {
275 return $this->cache
->get( 'isDeprioritized', $type );
277 if ( $type === 'refreshLinks2' ) {
278 // Don't keep converting refreshLinks2 => refreshLinks jobs if the
279 // later jobs have not been done yet. This helps throttle queue spam.
280 $deprioritized = !$this->get( 'refreshLinks' )->isEmpty();
281 $this->cache
->set( 'isDeprioritized', $type, $deprioritized );
282 return $deprioritized;
288 * Execute any due periodic queue maintenance tasks for all queues.
290 * A task is "due" if the time ellapsed since the last run is greater than
291 * the defined run period. Concurrent calls to this function will cause tasks
292 * to be attempted twice, so they may need their own methods of mutual exclusion.
294 * @return integer Number of tasks run
296 public function executeReadyPeriodicTasks() {
299 list( $db, $prefix ) = wfSplitWikiID( $this->wiki
);
300 $key = wfForeignMemcKey( $db, $prefix, 'jobqueuegroup', 'taskruns', 'v1' );
301 $lastRuns = $wgMemc->get( $key ); // (queue => task => UNIX timestamp)
304 $tasksRun = array(); // (queue => task => UNIX timestamp)
305 foreach ( $this->getQueueTypes() as $type ) {
306 $queue = $this->get( $type );
307 foreach ( $queue->getPeriodicTasks() as $task => $definition ) {
308 if ( $definition['period'] <= 0 ) {
309 continue; // disabled
310 } elseif ( !isset( $lastRuns[$type][$task] )
311 ||
$lastRuns[$type][$task] < ( time() - $definition['period'] ) )
313 if ( call_user_func( $definition['callback'] ) !== null ) {
314 $tasksRun[$type][$task] = time();
321 $wgMemc->merge( $key, function( $cache, $key, $lastRuns ) use ( $tasksRun ) {
322 if ( is_array( $lastRuns ) ) {
323 foreach ( $tasksRun as $type => $tasks ) {
324 foreach ( $tasks as $task => $timestamp ) {
325 if ( !isset( $lastRuns[$type][$task] )
326 ||
$timestamp > $lastRuns[$type][$task] )
328 $lastRuns[$type][$task] = $timestamp;
333 $lastRuns = $tasksRun;
342 * @param $name string
345 private function getCachedConfigVar( $name ) {
346 global $wgConf, $wgMemc;
348 if ( $this->wiki
=== wfWikiID() ) {
349 return $GLOBALS[$name]; // common case
351 list( $db, $prefix ) = wfSplitWikiID( $this->wiki
);
352 $key = wfForeignMemcKey( $db, $prefix, 'configvalue', $name );
353 $value = $wgMemc->get( $key ); // ('v' => ...) or false
354 if ( is_array( $value ) ) {
357 $value = $wgConf->getConfig( $this->wiki
, $name );
358 $wgMemc->set( $key, array( 'v' => $value ), 86400 +
mt_rand( 0, 86400 ) );