From 34dd248e285abbfab09f124ab7803b40101c5505 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Mon, 30 Nov 2015 14:02:53 -0800 Subject: [PATCH] Add pre-send update support to DeferredUpdates * PRESEND/POSTSEND constants can now be used in addUpdate() and addCallableUpdate() to control when the update runs. This is useful for updates that may report errors the client should see or to just get a head start on queued or pubsub based updates like CDN purges. The OutputPage::output() method can easily take a few 100ms. * Removed some argument b/c code from doUpdates(). * Also moved DeferrableUpdate to a separate file. Change-Id: I9831fe890f9f68f9ad8c4f4bba6921a8f29ba666 --- autoload.php | 4 +- includes/MediaWiki.php | 4 +- includes/deferred/DeferrableUpdate.php | 14 ++ includes/deferred/DeferredUpdates.php | 122 ++++++++++-------- .../includes/deferred/DeferredUpdatesTest.php | 28 ++++ 5 files changed, 117 insertions(+), 55 deletions(-) create mode 100644 includes/deferred/DeferrableUpdate.php diff --git a/autoload.php b/autoload.php index 84bfe0b562..f79e58b4c2 100644 --- a/autoload.php +++ b/autoload.php @@ -164,8 +164,8 @@ $wgAutoloadLocalClasses = array( 'BenchIfSwitch' => __DIR__ . '/maintenance/benchmarks/bench_if_switch.php', 'BenchStrtrStrReplace' => __DIR__ . '/maintenance/benchmarks/bench_strtr_str_replace.php', 'BenchUtf8TitleCheck' => __DIR__ . '/maintenance/benchmarks/bench_utf8_title_check.php', - 'BenchWikimediaBaseConvert' => __DIR__ . '/maintenance/benchmarks/bench_Wikimedia_base_convert.php', 'BenchWfIsWindows' => __DIR__ . '/maintenance/benchmarks/bench_wfIsWindows.php', + 'BenchWikimediaBaseConvert' => __DIR__ . '/maintenance/benchmarks/bench_Wikimedia_base_convert.php', 'BenchmarkDeleteTruncate' => __DIR__ . '/maintenance/benchmarks/bench_delete_truncate.php', 'BenchmarkHooks' => __DIR__ . '/maintenance/benchmarks/benchmarkHooks.php', 'BenchmarkParse' => __DIR__ . '/maintenance/benchmarks/benchmarkParse.php', @@ -307,7 +307,7 @@ $wgAutoloadLocalClasses = array( 'DateFormats' => __DIR__ . '/maintenance/language/date-formats.php', 'DateFormatter' => __DIR__ . '/includes/parser/DateFormatter.php', 'DeadendPagesPage' => __DIR__ . '/includes/specials/SpecialDeadendpages.php', - 'DeferrableUpdate' => __DIR__ . '/includes/deferred/DeferredUpdates.php', + 'DeferrableUpdate' => __DIR__ . '/includes/deferred/DeferrableUpdate.php', 'DeferredStringifier' => __DIR__ . '/includes/libs/DeferredStringifier.php', 'DeferredUpdates' => __DIR__ . '/includes/deferred/DeferredUpdates.php', 'DeleteAction' => __DIR__ . '/includes/actions/DeleteAction.php', diff --git a/includes/MediaWiki.php b/includes/MediaWiki.php index 38d9e476f4..ecfd8f87aa 100644 --- a/includes/MediaWiki.php +++ b/includes/MediaWiki.php @@ -509,8 +509,10 @@ class MediaWiki { $factory = wfGetLBFactory(); $factory->commitMasterChanges(); $factory->shutdown(); + wfDebug( __METHOD__ . ': all transactions committed' ); - wfDebug( __METHOD__ . ' completed; all transactions committed' ); + DeferredUpdates::doUpdates( 'enqueue', DeferredUpdates::PRESEND ); + wfDebug( __METHOD__ . ': pre-send deferred updates completed' ); // Set a cookie to tell all CDN edge nodes to "stick" the user to the // DC that handles this POST request (e.g. the "master" data center) diff --git a/includes/deferred/DeferrableUpdate.php b/includes/deferred/DeferrableUpdate.php new file mode 100644 index 0000000000..5f4d821060 --- /dev/null +++ b/includes/deferred/DeferrableUpdate.php @@ -0,0 +1,14 @@ +setMwGlobals( 'wgCommandLineMode', false ); @@ -34,6 +37,31 @@ class DeferredUpdatesTest extends MediaWikiTestCase { $this->expectOutputString( implode( '', $updates ) ); DeferredUpdates::doUpdates(); + + $x = null; + $y = null; + DeferredUpdates::addCallableUpdate( + function () use ( &$x ) { + $x = 'Sherity'; + }, + DeferredUpdates::PRESEND + ); + DeferredUpdates::addCallableUpdate( + function () use ( &$y ) { + $y = 'Marychu'; + }, + DeferredUpdates::POSTSEND + ); + + $this->assertNull( $x, "Update not run yet" ); + $this->assertNull( $y, "Update not run yet" ); + + DeferredUpdates::doUpdates( 'run', DeferredUpdates::PRESEND ); + $this->assertEquals( "Sherity", $x, "PRESEND update ran" ); + $this->assertNull( $y, "POSTSEND update not run yet" ); + + DeferredUpdates::doUpdates( 'run', DeferredUpdates::POSTSEND ); + $this->assertEquals( "Marychu", $y, "POSTSEND update ran" ); } public function testDoUpdatesCLI() { -- 2.20.1