Manual:Hooks/ApiMaxLagInfo
ApiMaxLagInfo | |
---|---|
Available from version 1.32.0 Called right before giving out information about max lag in API | |
Define function: | public static function onApiMaxLagInfo( array &$lagInfo ) { ... }
|
Attach hook: | In extension.json:
{
"Hooks": {
"ApiMaxLagInfo": "MediaWiki\\Extension\\MyExtension\\Hooks::onApiMaxLagInfo"
}
}
|
Called from: | File(s): api/ApiMain.phpCategory:MediaWiki hooks included in ApiMain.php#ApiMaxLagInfo |
Interface: | ApiMaxLagInfoHook.php |
For more information about attaching hooks, see Manual:Hooks .
For examples of extensions using this hook, see Category:ApiMaxLagInfo extensions.
Details
Use this to override lag information. Generally a hook function should only replace $lagInfo
if the new $lagInfo['lag']
is greater than the current $lagInfo['lag']
.
&$lagInfo
: Maximum lag information array. Fields in the array are:'lag'
is the number of seconds of lag.'host'
is the host name on which the lag exists.'type'
is an indication of the type of lag, e.g. "db" for database replication lag or "jobqueue" for job queue size converted to pseudo-seconds.- It is possible to add more fields and they will be returned to the user in the API response.
Example
To make bots back off if the job queue grows too large, by translating jobs into maxlag “seconds” (with an arbitrary factor, here 1000, that you’d want to adjust for your own wiki), you might use:
$wgHooks['ApiMaxLagInfo'][] = function ( &$lagInfo ): void {
$jobs = \MediaWiki\SiteStats\SiteStats::jobs();
if ( $jobs <= 0 ) {
return;
}
$lag = (float)$jobs / 1000.0;
if ( $lag <= $lagInfo['lag'] ) {
return;
}
$server = \MediaWiki\MediaWikiServices::getInstance()->getMainConfig()->get( \MediaWiki\MainConfigNames::Server );
$host = wfParseUrl( $server )['host'];
$lagInfo = [
'lag' => $lag,
'host' => $host,
'type' => 'jobqueue',
'jobs' => $jobs,
];
};