Prevent repeatedly launching emergency updates

Emergency update of assets could be repeatedly launched
every 15 seconds if a resource could not be fetched from
a server. A cooldown period has been added to prevent
repeatedly launching emergency updates.
This commit is contained in:
Raymond Hill 2023-03-29 13:02:48 -04:00
parent 8d445e782d
commit 6461393b6a
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 19 additions and 13 deletions

View File

@ -1465,6 +1465,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
{ {
let timer, next = 0; let timer, next = 0;
let lastEmergencyUpdate = 0;
µb.scheduleAssetUpdater = async function(updateDelay) { µb.scheduleAssetUpdater = async function(updateDelay) {
@ -1477,10 +1478,13 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
return; return;
} }
const assetDict = await io.metadata();
const now = Date.now(); const now = Date.now();
let needEmergencyUpdate = false; let needEmergencyUpdate = false;
// Respect cooldown period before launching an emergency update.
const timeSinceLastEmergencyUpdate = (now - lastEmergencyUpdate) / 3600000;
if ( timeSinceLastEmergencyUpdate > 1 ) {
const assetDict = await io.metadata();
for ( const [ assetKey, asset ] of Object.entries(assetDict) ) { for ( const [ assetKey, asset ] of Object.entries(assetDict) ) {
if ( asset.hasRemoteURL !== true ) { continue; } if ( asset.hasRemoteURL !== true ) { continue; }
if ( asset.content === 'filters' ) { if ( asset.content === 'filters' ) {
@ -1493,8 +1497,10 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
const daysSinceVeryObsolete = lastUpdateInDays - 2 * asset.updateAfter; const daysSinceVeryObsolete = lastUpdateInDays - 2 * asset.updateAfter;
if ( daysSinceVeryObsolete < 0 ) { continue; } if ( daysSinceVeryObsolete < 0 ) { continue; }
needEmergencyUpdate = true; needEmergencyUpdate = true;
lastEmergencyUpdate = now;
break; break;
} }
}
// Use the new schedule if and only if it is earlier than the previous // Use the new schedule if and only if it is earlier than the previous
// one. // one.