Merge "Special:Newpages feed now shows first revision instead of latest revision"
[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 */
22
23 /**
24 * Class to handle tracking information about all queues
25 *
26 * @ingroup JobQueue
27 * @since 1.21
28 */
29 abstract class JobQueueAggregator {
30 /** @var JobQueueAggregator */
31 protected static $instance = null;
32
33 /**
34 * @param array $params
35 */
36 public function __construct( array $params ) {
37 }
38
39 /**
40 * @throws MWException
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 $ok = $this->doNotifyQueueEmpty( $wiki, $type );
76
77 return $ok;
78 }
79
80 /**
81 * @see JobQueueAggregator::notifyQueueEmpty()
82 */
83 abstract protected function doNotifyQueueEmpty( $wiki, $type );
84
85 /**
86 * Mark a queue as being non-empty
87 *
88 * @param string $wiki
89 * @param string $type
90 * @return bool Success
91 */
92 final public function notifyQueueNonEmpty( $wiki, $type ) {
93 $ok = $this->doNotifyQueueNonEmpty( $wiki, $type );
94
95 return $ok;
96 }
97
98 /**
99 * @see JobQueueAggregator::notifyQueueNonEmpty()
100 */
101 abstract protected function doNotifyQueueNonEmpty( $wiki, $type );
102
103 /**
104 * Get the list of all of the queues with jobs
105 *
106 * @return array (job type => (list of wiki IDs))
107 */
108 final public function getAllReadyWikiQueues() {
109 $res = $this->doGetAllReadyWikiQueues();
110
111 return $res;
112 }
113
114 /**
115 * @see JobQueueAggregator::getAllReadyWikiQueues()
116 */
117 abstract protected function doGetAllReadyWikiQueues();
118
119 /**
120 * Purge all of the aggregator information
121 *
122 * @return bool Success
123 */
124 final public function purge() {
125 $res = $this->doPurge();
126
127 return $res;
128 }
129
130 /**
131 * @see JobQueueAggregator::purge()
132 */
133 abstract protected function doPurge();
134
135 /**
136 * Get all databases that have a pending job.
137 * This poll all the queues and is this expensive.
138 *
139 * @return array (job type => (list of wiki IDs))
140 */
141 protected function findPendingWikiQueues() {
142 global $wgLocalDatabases;
143
144 $pendingDBs = []; // (job type => (db list))
145 foreach ( $wgLocalDatabases as $db ) {
146 foreach ( JobQueueGroup::singleton( $db )->getQueuesWithJobs() as $type ) {
147 $pendingDBs[$type][] = $db;
148 }
149 }
150
151 return $pendingDBs;
152 }
153 }
154
155 class JobQueueAggregatorNull extends JobQueueAggregator {
156 protected function doNotifyQueueEmpty( $wiki, $type ) {
157 return true;
158 }
159
160 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
161 return true;
162 }
163
164 protected function doGetAllReadyWikiQueues() {
165 return [];
166 }
167
168 protected function doPurge() {
169 return true;
170 }
171 }