Merge "PHPUnit/Framework.php was removed in 2010"
[lhc/web/wiklou.git] / includes / job / 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 * @return JobQueueAggregator
41 */
42 final public static function singleton() {
43 global $wgJobQueueAggregator;
44
45 if ( !isset( self::$instance ) ) {
46 $class = $wgJobQueueAggregator['class'];
47 $obj = new $class( $wgJobQueueAggregator );
48 if ( !( $obj instanceof JobQueueAggregator ) ) {
49 throw new MWException( "Class '$class' is not a JobQueueAggregator class." );
50 }
51 self::$instance = $obj;
52 }
53
54 return self::$instance;
55 }
56
57 /**
58 * Destroy the singleton instance
59 *
60 * @return void
61 */
62 final public static function destroySingleton() {
63 self::$instance = null;
64 }
65
66 /**
67 * Mark a queue as being empty
68 *
69 * @param string $wiki
70 * @param string $type
71 * @return bool Success
72 */
73 final public function notifyQueueEmpty( $wiki, $type ) {
74 wfProfileIn( __METHOD__ );
75 $ok = $this->doNotifyQueueEmpty( $wiki, $type );
76 wfProfileOut( __METHOD__ );
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 wfProfileIn( __METHOD__ );
94 $ok = $this->doNotifyQueueNonEmpty( $wiki, $type );
95 wfProfileOut( __METHOD__ );
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 wfProfileIn( __METHOD__ );
111 $res = $this->doGetAllReadyWikiQueues();
112 wfProfileOut( __METHOD__ );
113 return $res;
114 }
115
116 /**
117 * @see JobQueueAggregator::getAllReadyWikiQueues()
118 */
119 abstract protected function doGetAllReadyWikiQueues();
120
121 /**
122 * Get all databases that have a pending job.
123 * This poll all the queues and is this expensive.
124 *
125 * @return Array (job type => (list of wiki IDs))
126 */
127 protected function findPendingWikiQueues() {
128 global $wgLocalDatabases;
129
130 $pendingDBs = array(); // (job type => (db list))
131 foreach ( $wgLocalDatabases as $db ) {
132 foreach ( JobQueueGroup::singleton( $db )->getQueuesWithJobs() as $type ) {
133 $pendingDBs[$type][] = $db;
134 }
135 }
136
137 return $pendingDBs;
138 }
139 }