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.
45 class DeferredUpdates
{
47 * @var array Updates to be deferred until the end of the request.
49 private static $updates = array();
52 * Add an update to the deferred list
53 * @param DeferrableUpdate $update Some object that implements doUpdate()
55 public static function addUpdate( DeferrableUpdate
$update ) {
56 global $wgCommandLineMode;
58 array_push( self
::$updates, $update );
60 // CLI scripts may forget to periodically flush these updates,
61 // so try to handle that rather than OOMing and losing them.
62 // Try to run the updates as soon as there is no local transaction.
63 static $waitingOnTrx = false; // de-duplicate callback
64 if ( $wgCommandLineMode && !$waitingOnTrx ) {
66 $dbw = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
67 // Do the update as soon as there is no transaction
68 if ( $dbw && $dbw->trxLevel() ) {
70 $dbw->onTransactionIdle( function() use ( &$waitingOnTrx ) {
71 DeferredUpdates
::doUpdates();
72 $waitingOnTrx = false;
81 * HTMLCacheUpdates are the most common deferred update people use. This
82 * is a shortcut method for that.
83 * @see HTMLCacheUpdate::__construct()
85 * @param string $table
87 public static function addHTMLCacheUpdate( $title, $table ) {
88 self
::addUpdate( new HTMLCacheUpdate( $title, $table ) );
92 * Add a callable update. In a lot of cases, we just need a callback/closure,
93 * defining a new DeferrableUpdate object is not necessary
94 * @see MWCallableUpdate::__construct()
95 * @param callable $callable
97 public static function addCallableUpdate( $callable ) {
98 self
::addUpdate( new MWCallableUpdate( $callable ) );
102 * Do any deferred updates and clear the list
104 * @param string $commit Set to 'commit' to commit after every update to
105 * prevent lock contention
107 public static function doUpdates( $commit = '' ) {
108 global $wgDeferredUpdateList;
110 $updates = array_merge( $wgDeferredUpdateList, self
::$updates );
112 while ( count( $updates ) ) {
113 self
::clearPendingUpdates();
115 /** @var DeferrableUpdate $update */
116 foreach ( $updates as $update ) {
120 if ( $commit === 'commit' ) {
121 wfGetLBFactory()->commitMasterChanges();
123 } catch ( Exception
$e ) {
124 // We don't want exceptions thrown during deferred updates to
125 // be reported to the user since the output is already sent.
126 // Instead we just log them.
127 if ( !$e instanceof ErrorPageError
) {
128 MWExceptionHandler
::logException( $e );
133 $updates = array_merge( $wgDeferredUpdateList, self
::$updates );
138 * Clear all pending updates without performing them. Generally, you don't
139 * want or need to call this. Unit tests need it though.
141 public static function clearPendingUpdates() {
142 global $wgDeferredUpdateList;
143 $wgDeferredUpdateList = self
::$updates = array();