MediaWiki master
|
Defer callable updates to run later in the PHP process. More...
Static Public Member Functions | |
static | addCallableUpdate ( $callable, $stage=self::POSTSEND, $dependeeDbws=[]) |
Add an update to the pending update queue that invokes the specified callback when run. | |
static | addUpdate (DeferrableUpdate $update, $stage=self::POSTSEND) |
Add an update to the pending update queue for execution at the appropriate time. | |
static | attemptUpdate (DeferrableUpdate $update) |
Attempt to run an update with the appropriate transaction round state if needed. | |
static | clearPendingUpdates () |
Cancel all pending updates for the current execution context. | |
static | doUpdates ( $stage=self::ALL) |
Consume and execute all pending updates. | |
static | getPendingUpdates ( $stage=self::ALL) |
Get a list of the pending updates for the current execution context. | |
static | getRecursiveExecutionStackDepth () |
Get the number of in-progress calls to DeferredUpdates::doUpdates() | |
static | pendingUpdatesCount () |
Get the number of pending updates for the current execution context. | |
static | preventOpportunisticUpdates () |
Prevent opportunistic updates until the returned ScopedCallback is consumed. | |
static | setScopeStack (DeferredUpdatesScopeStack $scopeStack) |
static | tryOpportunisticExecute () |
Consume and execute pending updates now if possible, instead of waiting. | |
Defer callable updates to run later in the PHP process.
This is a performance feature that enables MediaWiki to produce faster web responses. It allows you to postpone non-blocking work (e.g. work that does not change the web response) to after the HTTP response has been sent to the client (i.e. web browser).
Once the response is finalized and sent to the browser, the webserver process stays for a little while longer (detached from the web request) to run your POSTSEND tasks.
There is also a PRESEND option, which runs your task right before the finalized response is sent to the browser. This is for critical tasks that does need to block the response, but where you'd like to benefit from other DeferredUpdates features. Such as:
When scheduling a POSTSEND via the DeferredUpdates system you can generally expect it to complete well before the client makes their next request. Updates runs directly after the web response is sent, from the same process on the same server. This unlike the JobQueue, where jobs may need to wait in line for some minutes or hours.
If your update fails, this failure is not known to the client and gets no retry. For updates that need re-tries for system consistency or data integrity, it is recommended to implement it as a job instead and use JobQueueGroup::lazyPush. This has the caveat of being delayed by default, the same as any other job.
A hybrid solution is available via the EnqueueableDataUpdate interface. By implementing this interface, you can queue your update via the DeferredUpdates first, and if it fails, the system will automatically catch this and queue it as a job instead.
In CLI mode, no distinction is made between PRESEND and POSTSEND deferred updates, and the queue is periodically executed throughout the process.
Each update is added via DeferredUpdates::addUpdate and stored in either the PRESEND or POSTSEND queue. If an update gets queued while another update is already running, then we store in a "sub"-queue associated with the current update. This allows nested updates to be completed before other updates, which improves ordering for process caching.
Definition at line 99 of file DeferredUpdates.php.
|
static |
Add an update to the pending update queue that invokes the specified callback when run.
callable | $callable | One of the following:
|
int | $stage | One of (DeferredUpdates::PRESEND, DeferredUpdates::POSTSEND) |
IDatabase | IDatabase[] | $dependeeDbws | DB handles which might have pending writes upon which this update depends. If any of the handles already has an open transaction, a rollback thereof will cause this update to be cancelled (if it has not already run). [optional] (since 1.28) |
Definition at line 177 of file DeferredUpdates.php.
References wfGetCaller().
Referenced by MediaWiki\Deferred\LinksUpdate\LinksDeletionUpdate\doIncrementalUpdate().
|
static |
Add an update to the pending update queue for execution at the appropriate time.
In CLI mode, callback magic will also be used to run updates when safe
If an update is already in progress, then what happens to this update is as follows:
DeferrableUpdate | $update | Some object that implements doUpdate() |
int | $stage | One of (DeferredUpdates::PRESEND, DeferredUpdates::POSTSEND) |
Definition at line 157 of file DeferredUpdates.php.
Referenced by MediaWiki\Deferred\LinksUpdate\LinksUpdate\doUpdate().
|
static |
Attempt to run an update with the appropriate transaction round state if needed.
It is allowed for a DeferredUpdate to directly execute one or more other DeferredUpdate instances without queueing them by calling this method. In that case, the outer update must use TransactionRoundAwareUpdate::TRX_ROUND_ABSENT, e.g. by extending TransactionRoundDefiningUpdate, so that this method can give each update its own transaction round.
DeferrableUpdate | $update |
Definition at line 457 of file DeferredUpdates.php.
References MediaWiki\Deferred\DeferrableUpdate\doUpdate().
Referenced by MediaWiki\Deferred\RefreshSecondaryDataUpdate\doUpdate().
|
static |
Cancel all pending updates for the current execution context.
If an update is in progress, then this operates on the sub-queues of the innermost in-progress update. Otherwise, it acts on the top-queues.
Definition at line 431 of file DeferredUpdates.php.
|
static |
Consume and execute all pending updates.
Note that it is rarely the case that this method should be called outside of a few select entry points. For simplicity, that kind of recursion is discouraged. Recursion cannot happen if an explicit transaction round is active, which limits usage to updates with TRX_ROUND_ABSENT that do not leave open any transactions round of their own during the call to this method.
In the less-common case of this being called within an in-progress DeferrableUpdate, this will not see any top-queue updates (since they were consumed and are being run inside an outer execution loop). In that case, it will instead operate on the sub-queue of the innermost in-progress update on the stack.
int | $stage | Which updates to process. One of (DeferredUpdates::PRESEND, DeferredUpdates::POSTSEND, DeferredUpdates::ALL) |
Definition at line 261 of file DeferredUpdates.php.
References run().
|
static |
Get a list of the pending updates for the current execution context.
If an update is in progress, then this operates on the sub-queues of the innermost in-progress update. Otherwise, it acts on the top-queues.
int | $stage | Look for updates with this "defer until" stage. One of (DeferredUpdates::PRESEND, DeferredUpdates::POSTSEND, DeferredUpdates::ALL) |
Definition at line 419 of file DeferredUpdates.php.
|
static |
Get the number of in-progress calls to DeferredUpdates::doUpdates()
Definition at line 441 of file DeferredUpdates.php.
|
static |
Get the number of pending updates for the current execution context.
If an update is in progress, then this operates on the sub-queues of the innermost in-progress update. Otherwise, it acts on the top-queues.
Definition at line 403 of file DeferredUpdates.php.
|
static |
Prevent opportunistic updates until the returned ScopedCallback is consumed.
Definition at line 387 of file DeferredUpdates.php.
|
static |
DeferredUpdatesScopeStack | $scopeStack |
Definition at line 130 of file DeferredUpdates.php.
|
static |
Consume and execute pending updates now if possible, instead of waiting.
In web requests, updates are always deferred until the end of the request.
In CLI mode, updates run earlier and more often. This is important for long-running Maintenance scripts that would otherwise grow an excessively large queue, which increases memory use, and risks losing all updates if the script ends early or crashes.
The folllowing conditions are required for updates to run early in CLI mode:
How this works:
Note that this method runs both PRESEND and POSTSEND updates and thus should not be called during web requests. It is only intended for long-running Maintenance scripts.
Definition at line 364 of file DeferredUpdates.php.