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 * Interface that deferrable updates should implement. Basically required so we
25 * can validate input on DeferredUpdates::addUpdate()
29 interface DeferrableUpdate
{
31 * Perform the actual work
37 * Class for managing the deferred updates
39 * Deferred updates can be run at the end of the request,
40 * after the HTTP response has been sent. In CLI mode, updates
41 * are only deferred until there is no local master DB transaction.
42 * When updates are deferred, they go into a simple FIFO queue.
46 class DeferredUpdates
{
47 /** @var DeferrableUpdate[] Updates to be deferred until the end of the request */
48 private static $updates = array();
49 /** @var bool Defer updates fully even in CLI mode */
50 private static $forceDeferral = false;
53 * Add an update to the deferred list
54 * @param DeferrableUpdate $update Some object that implements doUpdate()
56 public static function addUpdate( DeferrableUpdate
$update ) {
57 global $wgCommandLineMode;
59 array_push( self
::$updates, $update );
60 if ( self
::$forceDeferral ) {
64 // CLI scripts may forget to periodically flush these updates,
65 // so try to handle that rather than OOMing and losing them.
66 // Try to run the updates as soon as there is no local transaction.
67 static $waitingOnTrx = false; // de-duplicate callback
68 if ( $wgCommandLineMode && !$waitingOnTrx ) {
70 $dbw = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
71 // Do the update as soon as there is no transaction
72 if ( $dbw && $dbw->trxLevel() ) {
74 $dbw->onTransactionIdle( function() use ( &$waitingOnTrx ) {
75 DeferredUpdates
::doUpdates();
76 $waitingOnTrx = false;
85 * Add a callable update. In a lot of cases, we just need a callback/closure,
86 * defining a new DeferrableUpdate object is not necessary
87 * @see MWCallableUpdate::__construct()
88 * @param callable $callable
90 public static function addCallableUpdate( $callable ) {
91 self
::addUpdate( new MWCallableUpdate( $callable ) );
95 * Do any deferred updates and clear the list
97 * @param string $commit Set to 'commit' to commit after every update to
98 * @param string $mode Use "enqueue" to use the job queue when possible [Default: run]
99 * prevent lock contention
101 public static function doUpdates( $commit = '', $mode = 'run' ) {
102 $updates = self
::$updates;
104 while ( count( $updates ) ) {
105 self
::clearPendingUpdates();
106 /** @var DataUpdate[] $dataUpdates */
107 $dataUpdates = array();
108 /** @var DeferrableUpdate[] $otherUpdates */
109 $otherUpdates = array();
110 foreach ( $updates as $update ) {
111 if ( $update instanceof DataUpdate
) {
112 $dataUpdates[] = $update;
114 $otherUpdates[] = $update;
118 // Delegate DataUpdate execution to the DataUpdate class
119 DataUpdate
::runUpdates( $dataUpdates, $mode );
120 // Execute the non-DataUpdate tasks
121 foreach ( $otherUpdates as $update ) {
124 if ( $commit === 'commit' ) {
125 wfGetLBFactory()->commitMasterChanges();
127 } catch ( Exception
$e ) {
128 // We don't want exceptions thrown during deferred updates to
129 // be reported to the user since the output is already sent.
130 // Instead we just log them.
131 if ( !$e instanceof ErrorPageError
) {
132 MWExceptionHandler
::logException( $e );
137 $updates = self
::$updates;
142 * Clear all pending updates without performing them. Generally, you don't
143 * want or need to call this. Unit tests need it though.
145 public static function clearPendingUpdates() {
146 self
::$updates = array();
150 * @note This method is intended for testing purposes
151 * @param bool $value Whether to *always* defer updates, even in CLI mode
154 public static function forceDeferral( $value ) {
155 self
::$forceDeferral = $value;