3 * Interface and manager for deferred updates.
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.
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.
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
24 * Class for managing the deferred updates
26 * In web request mode, deferred updates can be run at the end of the request, either before or
27 * after the HTTP response has been sent. In either case, they run after the DB commit step. If
28 * an update runs after the response is sent, it will not block clients. If sent before, it will
29 * run synchronously. If such an update works via queueing, it will be more likely to complete by
30 * the time the client makes their next request after this one.
32 * In CLI mode, updates are only deferred until the current wiki has no DB write transaction
33 * active within this request.
35 * When updates are deferred, they use a FIFO queue (one for pre-send and one for post-send).
39 class DeferredUpdates
{
40 /** @var DeferrableUpdate[] Updates to be deferred until before request end */
41 private static $preSendUpdates = array();
42 /** @var DeferrableUpdate[] Updates to be deferred until after request end */
43 private static $postSendUpdates = array();
45 /** @var bool Defer updates fully even in CLI mode */
46 private static $forceDeferral = false;
48 const ALL
= 0; // all updates
49 const PRESEND
= 1; // for updates that should run before flushing output buffer
50 const POSTSEND
= 2; // for updates that should run after flushing output buffer
53 * Add an update to the deferred list
55 * @param DeferrableUpdate $update Some object that implements doUpdate()
56 * @param integer $type DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27)
58 public static function addUpdate( DeferrableUpdate
$update, $type = self
::POSTSEND
) {
59 if ( $type === self
::PRESEND
) {
60 self
::push( self
::$preSendUpdates, $update );
62 self
::push( self
::$postSendUpdates, $update );
67 * Add a callable update. In a lot of cases, we just need a callback/closure,
68 * defining a new DeferrableUpdate object is not necessary
70 * @see MWCallableUpdate::__construct()
72 * @param callable $callable
73 * @param integer $type DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27)
75 public static function addCallableUpdate( $callable, $type = self
::POSTSEND
) {
76 self
::addUpdate( new MWCallableUpdate( $callable ), $type );
80 * Do any deferred updates and clear the list
82 * @param string $mode Use "enqueue" to use the job queue when possible [Default: "run"]
83 * @param integer $type DeferredUpdates constant (PRESEND, POSTSEND, or ALL) (since 1.27)
85 public static function doUpdates( $mode = 'run', $type = self
::ALL
) {
86 if ( $type === self
::ALL ||
$type == self
::PRESEND
) {
87 self
::execute( self
::$preSendUpdates, $mode );
90 if ( $type === self
::ALL ||
$type == self
::POSTSEND
) {
91 self
::execute( self
::$postSendUpdates, $mode );
95 private static function push( array &$queue, DeferrableUpdate
$update ) {
96 global $wgCommandLineMode;
98 if ( $update instanceof MergeableUpdate
) {
99 $class = get_class( $update ); // fully-qualified class
100 if ( isset( $queue[$class] ) ) {
101 /** @var $existingUpdate MergeableUpdate */
102 $existingUpdate = $queue[$class];
103 $existingUpdate->merge( $update );
105 $queue[$class] = $update;
111 if ( self
::$forceDeferral ) {
112 return; // do not run
115 // CLI scripts may forget to periodically flush these updates,
116 // so try to handle that rather than OOMing and losing them entirely.
117 // Try to run the updates as soon as there is no current wiki transaction.
118 static $waitingOnTrx = false; // de-duplicate callback
119 if ( $wgCommandLineMode && !$waitingOnTrx ) {
121 $dbw = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
122 // Do the update as soon as there is no transaction
123 if ( $dbw && $dbw->trxLevel() ) {
124 $waitingOnTrx = true;
125 $dbw->onTransactionIdle( function() use ( &$waitingOnTrx ) {
126 DeferredUpdates
::doUpdates();
127 $waitingOnTrx = false;
135 public static function execute( array &$queue, $mode ) {
136 $updates = $queue; // snapshot of queue
138 // Keep doing rounds of updates until none get enqueued
139 while ( count( $updates ) ) {
140 $queue = array(); // clear the queue
141 /** @var DataUpdate[] $dataUpdates */
142 $dataUpdates = array();
143 /** @var DeferrableUpdate[] $otherUpdates */
144 $otherUpdates = array();
145 foreach ( $updates as $update ) {
146 if ( $update instanceof DataUpdate
) {
147 $dataUpdates[] = $update;
149 $otherUpdates[] = $update;
153 // Delegate DataUpdate execution to the DataUpdate class
154 DataUpdate
::runUpdates( $dataUpdates, $mode );
155 // Execute the non-DataUpdate tasks
156 foreach ( $otherUpdates as $update ) {
159 wfGetLBFactory()->commitMasterChanges();
160 } catch ( Exception
$e ) {
161 // We don't want exceptions thrown during deferred updates to
162 // be reported to the user since the output is already sent
163 if ( !$e instanceof ErrorPageError
) {
164 MWExceptionHandler
::logException( $e );
166 // Make sure incomplete transactions are not committed and end any
167 // open atomic sections so that other DB updates have a chance to run
168 wfGetLBFactory()->rollbackMasterChanges();
172 $updates = $queue; // new snapshot of queue (check for new entries)
177 * Clear all pending updates without performing them. Generally, you don't
178 * want or need to call this. Unit tests need it though.
180 public static function clearPendingUpdates() {
181 self
::$preSendUpdates = array();
182 self
::$postSendUpdates = array();
186 * @note This method is intended for testing purposes
187 * @param bool $value Whether to *always* defer updates, even in CLI mode
190 public static function forceDeferral( $value ) {
191 self
::$forceDeferral = $value;