3 * Job queue aggregator code.
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 tracking information about all queues
30 abstract class JobQueueAggregator
{
31 /** @var JobQueueAggregator */
32 protected static $instance = null;
35 * @param array $params
37 protected function __construct( array $params ) {
42 * @return JobQueueAggregator
44 final public static function singleton() {
45 global $wgJobQueueAggregator;
47 if ( !isset( self
::$instance ) ) {
48 $class = $wgJobQueueAggregator['class'];
49 $obj = new $class( $wgJobQueueAggregator );
50 if ( !( $obj instanceof JobQueueAggregator
) ) {
51 throw new MWException( "Class '$class' is not a JobQueueAggregator class." );
53 self
::$instance = $obj;
56 return self
::$instance;
60 * Destroy the singleton instance
64 final public static function destroySingleton() {
65 self
::$instance = null;
69 * Mark a queue as being empty
73 * @return bool Success
75 final public function notifyQueueEmpty( $wiki, $type ) {
76 wfProfileIn( __METHOD__
);
77 $ok = $this->doNotifyQueueEmpty( $wiki, $type );
78 wfProfileOut( __METHOD__
);
84 * @see JobQueueAggregator::notifyQueueEmpty()
86 abstract protected function doNotifyQueueEmpty( $wiki, $type );
89 * Mark a queue as being non-empty
93 * @return bool Success
95 final public function notifyQueueNonEmpty( $wiki, $type ) {
96 wfProfileIn( __METHOD__
);
97 $ok = $this->doNotifyQueueNonEmpty( $wiki, $type );
98 wfProfileOut( __METHOD__
);
104 * @see JobQueueAggregator::notifyQueueNonEmpty()
106 abstract protected function doNotifyQueueNonEmpty( $wiki, $type );
109 * Get the list of all of the queues with jobs
111 * @return array (job type => (list of wiki IDs))
113 final public function getAllReadyWikiQueues() {
114 wfProfileIn( __METHOD__
);
115 $res = $this->doGetAllReadyWikiQueues();
116 wfProfileOut( __METHOD__
);
122 * @see JobQueueAggregator::getAllReadyWikiQueues()
124 abstract protected function doGetAllReadyWikiQueues();
127 * Purge all of the aggregator information
129 * @return bool Success
131 final public function purge() {
132 wfProfileIn( __METHOD__
);
133 $res = $this->doPurge();
134 wfProfileOut( __METHOD__
);
140 * @see JobQueueAggregator::purge()
142 abstract protected function doPurge();
145 * Get all databases that have a pending job.
146 * This poll all the queues and is this expensive.
148 * @return array (job type => (list of wiki IDs))
150 protected function findPendingWikiQueues() {
151 global $wgLocalDatabases;
153 $pendingDBs = array(); // (job type => (db list))
154 foreach ( $wgLocalDatabases as $db ) {
155 foreach ( JobQueueGroup
::singleton( $db )->getQueuesWithJobs() as $type ) {
156 $pendingDBs[$type][] = $db;