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
44 const PROC_CACHE_TTL
= 15; // integer; seconds
46 const CACHE_VERSION
= 1; // integer; cache version
49 * @param string $wiki Wiki ID
51 protected function __construct( $wiki ) {
53 $this->cache
= new ProcessCacheLRU( 10 );
57 * @param string $wiki Wiki ID
58 * @return JobQueueGroup
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 );
65 return self
::$instances[$wiki];
69 * Destroy the singleton instances
73 public static function destroySingletons() {
74 self
::$instances = array();
78 * Get the job queue object for a given queue type
83 public function get( $type ) {
84 global $wgJobTypeConf;
86 $conf = array( 'wiki' => $this->wiki
, 'type' => $type );
87 if ( isset( $wgJobTypeConf[$type] ) ) {
88 $conf = $conf +
$wgJobTypeConf[$type];
90 $conf = $conf +
$wgJobTypeConf['default'];
93 return JobQueue
::factory( $conf );
97 * Insert jobs into the respective queues of with the belong.
99 * This inserts the jobs into the queue specified by $wgJobTypeConf
100 * and updates the aggregate job queue information cache as needed.
102 * @param $jobs Job|array A single Job or a list of Jobs
103 * @throws MWException
106 public function push( $jobs ) {
107 $jobs = is_array( $jobs ) ?
$jobs : array( $jobs );
109 $jobsByType = array(); // (job type => list of jobs)
110 foreach ( $jobs as $job ) {
111 if ( $job instanceof Job
) {
112 $jobsByType[$job->getType()][] = $job;
114 throw new MWException( "Attempted to push a non-Job object into a queue." );
119 foreach ( $jobsByType as $type => $jobs ) {
120 if ( $this->get( $type )->push( $jobs ) ) {
121 JobQueueAggregator
::singleton()->notifyQueueNonEmpty( $this->wiki
, $type );
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' );
138 * Pop a job off one of the job queues
140 * This pops a job off a queue as specified by $wgJobTypeConf and
141 * updates the aggregate job queue information cache as needed.
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
147 public function pop( $qtype = self
::TYPE_DEFAULT
, $flags = 0 ) {
148 if ( is_string( $qtype ) ) { // specific job type
149 $job = $this->get( $qtype )->pop();
151 JobQueueAggregator
::singleton()->notifyQueueEmpty( $this->wiki
, $qtype );
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() );
159 $types = $this->cache
->get( 'queues-ready', 'list' );
161 $types = $this->getQueuesWithJobs();
164 if ( $qtype == self
::TYPE_DEFAULT
) {
165 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
167 shuffle( $types ); // avoid starvation
169 foreach ( $types as $type ) { // for each queue...
170 $job = $this->get( $type )->pop();
171 if ( $job ) { // found
173 } else { // not found
174 JobQueueAggregator
::singleton()->notifyQueueEmpty( $this->wiki
, $type );
175 $this->cache
->clear( 'queues-ready' );
179 return false; // no jobs found
184 * Acknowledge that a job was completed
189 public function ack( Job
$job ) {
190 return $this->get( $job->getType() )->ack( $job );
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.
200 public function deduplicateRootJob( Job
$job ) {
201 return $this->get( $job->getType() )->deduplicateRootJob( $job );
205 * Wait for any slaves or backup queue servers to catch up.
207 * This does nothing for certain queue classes.
210 * @throws MWException
212 public function waitForBackups() {
213 global $wgJobTypeConf;
215 wfProfileIn( __METHOD__
);
216 // Try to avoid doing this more than once per queue storage medium
217 foreach ( $wgJobTypeConf as $type => $conf ) {
218 $this->get( $type )->waitForBackups();
220 wfProfileOut( __METHOD__
);
224 * Get the list of queue types
226 * @return array List of strings
228 public function getQueueTypes() {
229 return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
233 * Get the list of default queue types
235 * @return array List of strings
237 public function getDefaultQueueTypes() {
238 global $wgJobTypesExcludedFromDefaultQueue;
240 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
244 * Get the list of job types that have non-empty queues
246 * @return Array List of job types that have non-empty queues
248 public function getQueuesWithJobs() {
250 foreach ( $this->getQueueTypes() as $type ) {
251 if ( !$this->get( $type )->isEmpty() ) {
259 * Check if jobs should not be popped of a queue right now.
260 * This is only used for performance, such as to avoid spamming
261 * the queue with many sub-jobs before they actually get run.
263 * @param $type string
266 public function isQueueDeprioritized( $type ) {
267 if ( $type === 'refreshLinks2' ) {
268 // Don't keep converting refreshLinks2 => refreshLinks jobs if the
269 // later jobs have not been done yet. This helps throttle queue spam.
270 return !$this->get( 'refreshLinks' )->isEmpty();
276 * Execute any due periodic queue maintenance tasks for all queues.
278 * A task is "due" if the time ellapsed since the last run is greater than
279 * the defined run period. Concurrent calls to this function will cause tasks
280 * to be attempted twice, so they may need their own methods of mutual exclusion.
282 * @return integer Number of tasks run
284 public function executeReadyPeriodicTasks() {
287 list( $db, $prefix ) = wfSplitWikiID( $this->wiki
);
288 $key = wfForeignMemcKey( $db, $prefix, 'jobqueuegroup', 'taskruns', 'v1' );
289 $lastRuns = $wgMemc->get( $key ); // (queue => task => UNIX timestamp)
292 $tasksRun = array(); // (queue => task => UNIX timestamp)
293 foreach ( $this->getQueueTypes() as $type ) {
294 $queue = $this->get( $type );
295 foreach ( $queue->getPeriodicTasks() as $task => $definition ) {
296 if ( $definition['period'] <= 0 ) {
297 continue; // disabled
298 } elseif ( !isset( $lastRuns[$type][$task] )
299 ||
$lastRuns[$type][$task] < ( time() - $definition['period'] ) )
301 if ( call_user_func( $definition['callback'] ) !== null ) {
302 $tasksRun[$type][$task] = time();
309 $wgMemc->merge( $key, function( $cache, $key, $lastRuns ) use ( $tasksRun ) {
310 if ( is_array( $lastRuns ) ) {
311 foreach ( $tasksRun as $type => $tasks ) {
312 foreach ( $tasks as $task => $timestamp ) {
313 if ( !isset( $lastRuns[$type][$task] )
314 ||
$timestamp > $lastRuns[$type][$task] )
316 $lastRuns[$type][$task] = $timestamp;
321 $lastRuns = $tasksRun;
330 * @param $name string
333 private function getCachedConfigVar( $name ) {
334 global $wgConf, $wgMemc;
336 if ( $this->wiki
=== wfWikiID() ) {
337 return $GLOBALS[$name]; // common case
339 list( $db, $prefix ) = wfSplitWikiID( $this->wiki
);
340 $key = wfForeignMemcKey( $db, $prefix, 'configvalue', $name );
341 $value = $wgMemc->get( $key ); // ('v' => ...) or false
342 if ( is_array( $value ) ) {
345 $value = $wgConf->getConfig( $this->wiki
, $name );
346 $wgMemc->set( $key, array( 'v' => $value ), 86400 +
mt_rand( 0, 86400 ) );