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 cache
44 const PROC_CACHE_TTL
= 15; // integer; seconds
47 * @param $wiki string Wiki ID
49 protected function __construct( $wiki ) {
51 $this->cache
= new ProcessCacheLRU( 1 );
55 * @param $wiki string Wiki ID
56 * @return JobQueueGroup
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 );
63 return self
::$instances[$wiki];
67 * Destroy the singleton instances
71 public static function destroySingletons() {
72 self
::$instances = array();
77 * @return JobQueue Job queue object for a given queue type
79 public function get( $type ) {
80 global $wgJobTypeConf;
82 $conf = array( 'wiki' => $this->wiki
, 'type' => $type );
83 if ( isset( $wgJobTypeConf[$type] ) ) {
84 $conf = $conf +
$wgJobTypeConf[$type];
86 $conf = $conf +
$wgJobTypeConf['default'];
89 return JobQueue
::factory( $conf );
93 * Insert jobs into the respective queues of with the belong.
94 * This inserts the jobs into the queue specified by $wgJobTypeConf.
96 * @param $jobs Job|array A single Job or a list of Jobs
100 public function push( $jobs ) {
101 $jobs = is_array( $jobs ) ?
$jobs : array( $jobs );
103 $jobsByType = array(); // (job type => list of jobs)
104 foreach ( $jobs as $job ) {
105 if ( $job instanceof Job
) {
106 $jobsByType[$job->getType()][] = $job;
108 throw new MWException( "Attempted to push a non-Job object into a queue." );
113 foreach ( $jobsByType as $type => $jobs ) {
114 if ( !$this->get( $type )->push( $jobs ) ) {
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' );
130 * Pop a job off one of the job queues
132 * @param $queueType integer JobQueueGroup::TYPE_* constant
133 * @param $flags integer Bitfield of JobQueueGroup::USE_* constants
134 * @return Job|bool Returns false on failure
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() );
141 $types = $this->cache
->get( 'queues-ready', 'list' );
143 $types = $this->getQueuesWithJobs();
146 if ( $queueType == self
::TYPE_DEFAULT
) {
147 $types = array_intersect( $types, $this->getDefaultQueueTypes() );
149 shuffle( $types ); // avoid starvation
151 foreach ( $types as $type ) { // for each queue...
152 $job = $this->get( $type )->pop();
153 if ( $job ) { // found
155 } else { // not found
156 $this->cache
->clear( 'queues-ready' );
160 return false; // no jobs found
164 * Acknowledge that a job was completed
169 public function ack( Job
$job ) {
170 return $this->get( $job->getType() )->ack( $job );
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.
180 public function deduplicateRootJob( Job
$job ) {
181 return $this->get( $job->getType() )->deduplicateRootJob( $job );
185 * Get the list of queue types
187 * @return array List of strings
189 public function getQueueTypes() {
190 global $wgJobClasses;
192 return array_keys( $wgJobClasses );
196 * Get the list of default queue types
198 * @return array List of strings
200 public function getDefaultQueueTypes() {
201 global $wgJobTypesExcludedFromDefaultQueue;
203 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
207 * @return Array List of job types that have non-empty queues
209 public function getQueuesWithJobs() {
211 foreach ( $this->getQueueTypes() as $type ) {
212 if ( !$this->get( $type )->isEmpty() ) {
220 * @return Array List of default job types that have non-empty queues
222 public function getDefaultQueuesWithJobs() {
224 foreach ( $this->getDefaultQueueTypes() as $type ) {
225 if ( !$this->get( $type )->isEmpty() ) {
233 * Execute any due periodic queue maintenance tasks for all queues.
235 * A task is "due" if the time ellapsed since the last run is greater than
236 * the defined run period. Concurrent calls to this function will cause tasks
237 * to be attempted twice, so they may need their own methods of mutual exclusion.
239 * @return integer Number of tasks run
241 public function executeReadyPeriodicTasks() {
244 list( $db, $prefix ) = wfSplitWikiID( $this->wiki
);
245 $key = wfForeignMemcKey( $db, $prefix, 'jobqueuegroup', 'taskruns', 'v1' );
246 $lastRuns = $wgMemc->get( $key ); // (queue => task => UNIX timestamp)
249 $tasksRun = array(); // (queue => task => UNIX timestamp)
250 foreach ( $this->getQueueTypes() as $type ) {
251 $queue = $this->get( $type );
252 foreach ( $queue->getPeriodicTasks() as $task => $definition ) {
253 if ( $definition['period'] <= 0 ) {
254 continue; // disabled
255 } elseif ( !isset( $lastRuns[$type][$task] )
256 ||
$lastRuns[$type][$task] < ( time() - $definition['period'] ) )
258 if ( call_user_func( $definition['callback'] ) !== null ) {
259 $tasksRun[$type][$task] = time();
266 $wgMemc->merge( $key, function( $cache, $key, $lastRuns ) use ( $tasksRun ) {
267 if ( is_array( $lastRuns ) ) {
268 foreach ( $tasksRun as $type => $tasks ) {
269 foreach ( $tasks as $task => $timestamp ) {
270 if ( !isset( $lastRuns[$type][$task] )
271 ||
$timestamp > $lastRuns[$type][$task] )
273 $lastRuns[$type][$task] = $timestamp;
278 $lastRuns = $tasksRun;