Merge "Error Msg for missing db username & password when installing"
[lhc/web/wiklou.git] / includes / poolcounter / PoolCounterWork.php
1 <?php
2 /**
3 * Provides of semaphore semantics for restricting the number
4 * of workers that may be concurrently performing the same task.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24 /**
25 * Class for dealing with PoolCounters using class members
26 */
27 abstract class PoolCounterWork {
28 protected $cacheable = false; //Does this override getCachedWork() ?
29
30 /**
31 * @param string $type The type of PoolCounter to use
32 * @param string $key Key that identifies the queue this work is placed on
33 */
34 public function __construct( $type, $key ) {
35 $this->poolCounter = PoolCounter::factory( $type, $key );
36 }
37
38 /**
39 * Actually perform the work, caching it if needed
40 * @return mixed work result or false
41 */
42 abstract public function doWork();
43
44 /**
45 * Retrieve the work from cache
46 * @return mixed work result or false
47 */
48 public function getCachedWork() {
49 return false;
50 }
51
52 /**
53 * A work not so good (eg. expired one) but better than an error
54 * message.
55 * @return mixed work result or false
56 */
57 public function fallback() {
58 return false;
59 }
60
61 /**
62 * Do something with the error, like showing it to the user.
63 * @return bool
64 */
65 public function error( $status ) {
66 return false;
67 }
68
69 /**
70 * Log an error
71 *
72 * @param $status Status
73 * @return void
74 */
75 public function logError( $status ) {
76 $key = $this->poolCounter->getKey();
77
78 wfDebugLog( 'poolcounter', "Pool key '$key': "
79 . $status->getMessage()->inLanguage( 'en' )->useDatabase( false )->text() );
80 }
81
82 /**
83 * Get the result of the work (whatever it is), or the result of the error() function.
84 * This returns the result of the first applicable method that returns a non-false value,
85 * where the methods are checked in the following order:
86 * - a) doWork() : Applies if the work is exclusive or no another process
87 * is doing it, and on the condition that either this process
88 * successfully entered the pool or the pool counter is down.
89 * - b) doCachedWork() : Applies if the work is cacheable and this blocked on another
90 * process which finished the work.
91 * - c) fallback() : Applies for all remaining cases.
92 * If these all fall through (by returning false), then the result of error() is returned.
93 *
94 * @param $skipcache bool
95 * @return mixed
96 */
97 public function execute( $skipcache = false ) {
98 if ( $this->cacheable && !$skipcache ) {
99 $status = $this->poolCounter->acquireForAnyone();
100 } else {
101 $status = $this->poolCounter->acquireForMe();
102 }
103
104 if ( !$status->isOK() ) {
105 // Respond gracefully to complete server breakage: just log it and do the work
106 $this->logError( $status );
107 return $this->doWork();
108 }
109
110 switch ( $status->value ) {
111 case PoolCounter::LOCKED:
112 $result = $this->doWork();
113 $this->poolCounter->release();
114 return $result;
115
116 case PoolCounter::DONE:
117 $result = $this->getCachedWork();
118 if ( $result === false ) {
119 /* That someone else work didn't serve us.
120 * Acquire the lock for me
121 */
122 return $this->execute( true );
123 }
124 return $result;
125
126 case PoolCounter::QUEUE_FULL:
127 case PoolCounter::TIMEOUT:
128 $result = $this->fallback();
129
130 if ( $result !== false ) {
131 return $result;
132 }
133 /* no break */
134
135 /* These two cases should never be hit... */
136 case PoolCounter::ERROR:
137 default:
138 $errors = array(
139 PoolCounter::QUEUE_FULL => 'pool-queuefull',
140 PoolCounter::TIMEOUT => 'pool-timeout' );
141
142 $status = Status::newFatal( isset( $errors[$status->value] )
143 ? $errors[$status->value]
144 : 'pool-errorunknown' );
145 $this->logError( $status );
146 return $this->error( $status );
147 }
148 }
149 }
150
151 /**
152 * Convenience class for dealing with PoolCounters using callbacks
153 * @since 1.22
154 */
155 class PoolCounterWorkViaCallback extends PoolCounterWork {
156 /** @var callable */
157 protected $doWork;
158 /** @var callable|null */
159 protected $doCachedWork;
160 /** @var callable|null */
161 protected $fallback;
162 /** @var callable|null */
163 protected $error;
164
165 /**
166 * Build a PoolCounterWork class from a type, key, and callback map.
167 *
168 * The callback map must at least have a callback for the 'doWork' method.
169 * Additionally, callbacks can be provided for the 'doCachedWork', 'fallback',
170 * and 'error' methods. Methods without callbacks will be no-ops that return false.
171 * If a 'doCachedWork' callback is provided, then execute() may wait for any prior
172 * process in the pool to finish and reuse its cached result.
173 *
174 * @param string $type
175 * @param string $key
176 * @param array $callbacks Map of callbacks
177 * @throws MWException
178 */
179 public function __construct( $type, $key, array $callbacks ) {
180 parent::__construct( $type, $key );
181 foreach ( array( 'doWork', 'doCachedWork', 'fallback', 'error' ) as $name ) {
182 if ( isset( $callbacks[$name] ) ) {
183 if ( !is_callable( $callbacks[$name] ) ) {
184 throw new MWException( "Invalid callback provided for '$name' function." );
185 }
186 $this->$name = $callbacks[$name];
187 }
188 }
189 if ( !isset( $this->doWork ) ) {
190 throw new MWException( "No callback provided for 'doWork' function." );
191 }
192 $this->cacheable = isset( $this->doCachedWork );
193 }
194
195 public function doWork() {
196 return call_user_func_array( $this->doWork, array() );
197 }
198
199 public function getCachedWork() {
200 if ( $this->doCachedWork ) {
201 return call_user_func_array( $this->doCachedWork, array() );
202 }
203 return false;
204 }
205
206 public function fallback() {
207 if ( $this->fallback ) {
208 return call_user_func_array( $this->fallback, array() );
209 }
210 return false;
211 }
212
213 public function error( $status ) {
214 if ( $this->error ) {
215 return call_user_func_array( $this->error, array( $status ) );
216 }
217 return false;
218 }
219 }