Merge "Move up devunt's name to Developers"
[lhc/web/wiklou.git] / includes / jobqueue / 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 public function __construct( array $params ) {
38 }
39
40 /**
41 * @throws MWException
42 * @return JobQueueAggregator
43 */
44 final public static function singleton() {
45 global $wgJobQueueAggregator;
46
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." );
52 }
53 self::$instance = $obj;
54 }
55
56 return self::$instance;
57 }
58
59 /**
60 * Destroy the singleton instance
61 *
62 * @return void
63 */
64 final public static function destroySingleton() {
65 self::$instance = null;
66 }
67
68 /**
69 * Mark a queue as being empty
70 *
71 * @param string $wiki
72 * @param string $type
73 * @return bool Success
74 */
75 final public function notifyQueueEmpty( $wiki, $type ) {
76 $ok = $this->doNotifyQueueEmpty( $wiki, $type );
77
78 return $ok;
79 }
80
81 /**
82 * @see JobQueueAggregator::notifyQueueEmpty()
83 */
84 abstract protected function doNotifyQueueEmpty( $wiki, $type );
85
86 /**
87 * Mark a queue as being non-empty
88 *
89 * @param string $wiki
90 * @param string $type
91 * @return bool Success
92 */
93 final public function notifyQueueNonEmpty( $wiki, $type ) {
94 $ok = $this->doNotifyQueueNonEmpty( $wiki, $type );
95
96 return $ok;
97 }
98
99 /**
100 * @see JobQueueAggregator::notifyQueueNonEmpty()
101 */
102 abstract protected function doNotifyQueueNonEmpty( $wiki, $type );
103
104 /**
105 * Get the list of all of the queues with jobs
106 *
107 * @return array (job type => (list of wiki IDs))
108 */
109 final public function getAllReadyWikiQueues() {
110 $res = $this->doGetAllReadyWikiQueues();
111
112 return $res;
113 }
114
115 /**
116 * @see JobQueueAggregator::getAllReadyWikiQueues()
117 */
118 abstract protected function doGetAllReadyWikiQueues();
119
120 /**
121 * Purge all of the aggregator information
122 *
123 * @return bool Success
124 */
125 final public function purge() {
126 $res = $this->doPurge();
127
128 return $res;
129 }
130
131 /**
132 * @see JobQueueAggregator::purge()
133 */
134 abstract protected function doPurge();
135
136 /**
137 * Get all databases that have a pending job.
138 * This poll all the queues and is this expensive.
139 *
140 * @return array (job type => (list of wiki IDs))
141 */
142 protected function findPendingWikiQueues() {
143 global $wgLocalDatabases;
144
145 $pendingDBs = []; // (job type => (db list))
146 foreach ( $wgLocalDatabases as $db ) {
147 foreach ( JobQueueGroup::singleton( $db )->getQueuesWithJobs() as $type ) {
148 $pendingDBs[$type][] = $db;
149 }
150 }
151
152 return $pendingDBs;
153 }
154 }
155
156 class JobQueueAggregatorNull extends JobQueueAggregator {
157 protected function doNotifyQueueEmpty( $wiki, $type ) {
158 return true;
159 }
160
161 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
162 return true;
163 }
164
165 protected function doGetAllReadyWikiQueues() {
166 return [];
167 }
168
169 protected function doPurge() {
170 return true;
171 }
172 }