Update formatting of job related files
[lhc/web/wiklou.git] / includes / job / aggregator / JobQueueAggregator.php
1 <?php
2 /**
3 * Job queue aggregator 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 tracking information about all queues
26 *
27 * @ingroup JobQueue
28 * @since 1.21
29 */
30 abstract class JobQueueAggregator {
31 /** @var JobQueueAggregator */
32 protected static $instance = null;
33
34 /**
35 * @param array $params
36 */
37 protected function __construct( array $params ) {
38 }
39
40 /**
41 * @return JobQueueAggregator
42 */
43 final public static function singleton() {
44 global $wgJobQueueAggregator;
45
46 if ( !isset( self::$instance ) ) {
47 $class = $wgJobQueueAggregator['class'];
48 $obj = new $class( $wgJobQueueAggregator );
49 if ( !( $obj instanceof JobQueueAggregator ) ) {
50 throw new MWException( "Class '$class' is not a JobQueueAggregator class." );
51 }
52 self::$instance = $obj;
53 }
54
55 return self::$instance;
56 }
57
58 /**
59 * Destroy the singleton instance
60 *
61 * @return void
62 */
63 final public static function destroySingleton() {
64 self::$instance = null;
65 }
66
67 /**
68 * Mark a queue as being empty
69 *
70 * @param string $wiki
71 * @param string $type
72 * @return bool Success
73 */
74 final public function notifyQueueEmpty( $wiki, $type ) {
75 wfProfileIn( __METHOD__ );
76 $ok = $this->doNotifyQueueEmpty( $wiki, $type );
77 wfProfileOut( __METHOD__ );
78
79 return $ok;
80 }
81
82 /**
83 * @see JobQueueAggregator::notifyQueueEmpty()
84 */
85 abstract protected function doNotifyQueueEmpty( $wiki, $type );
86
87 /**
88 * Mark a queue as being non-empty
89 *
90 * @param string $wiki
91 * @param string $type
92 * @return bool Success
93 */
94 final public function notifyQueueNonEmpty( $wiki, $type ) {
95 wfProfileIn( __METHOD__ );
96 $ok = $this->doNotifyQueueNonEmpty( $wiki, $type );
97 wfProfileOut( __METHOD__ );
98
99 return $ok;
100 }
101
102 /**
103 * @see JobQueueAggregator::notifyQueueNonEmpty()
104 */
105 abstract protected function doNotifyQueueNonEmpty( $wiki, $type );
106
107 /**
108 * Get the list of all of the queues with jobs
109 *
110 * @return Array (job type => (list of wiki IDs))
111 */
112 final public function getAllReadyWikiQueues() {
113 wfProfileIn( __METHOD__ );
114 $res = $this->doGetAllReadyWikiQueues();
115 wfProfileOut( __METHOD__ );
116
117 return $res;
118 }
119
120 /**
121 * @see JobQueueAggregator::getAllReadyWikiQueues()
122 */
123 abstract protected function doGetAllReadyWikiQueues();
124
125 /**
126 * Purge all of the aggregator information
127 *
128 * @return bool Success
129 */
130 final public function purge() {
131 wfProfileIn( __METHOD__ );
132 $res = $this->doPurge();
133 wfProfileOut( __METHOD__ );
134
135 return $res;
136 }
137
138 /**
139 * @see JobQueueAggregator::purge()
140 */
141 abstract protected function doPurge();
142
143 /**
144 * Get all databases that have a pending job.
145 * This poll all the queues and is this expensive.
146 *
147 * @return Array (job type => (list of wiki IDs))
148 */
149 protected function findPendingWikiQueues() {
150 global $wgLocalDatabases;
151
152 $pendingDBs = array(); // (job type => (db list))
153 foreach ( $wgLocalDatabases as $db ) {
154 foreach ( JobQueueGroup::singleton( $db )->getQueuesWithJobs() as $type ) {
155 $pendingDBs[$type][] = $db;
156 }
157 }
158
159 return $pendingDBs;
160 }
161 }