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,23 +1478,28 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
return; return;
} }
const assetDict = await io.metadata();
const now = Date.now(); const now = Date.now();
let needEmergencyUpdate = false; let needEmergencyUpdate = false;
for ( const [ assetKey, asset ] of Object.entries(assetDict) ) {
if ( asset.hasRemoteURL !== true ) { continue; } // Respect cooldown period before launching an emergency update.
if ( asset.content === 'filters' ) { const timeSinceLastEmergencyUpdate = (now - lastEmergencyUpdate) / 3600000;
if ( µb.selectedFilterLists.includes(assetKey) === false ) { if ( timeSinceLastEmergencyUpdate > 1 ) {
continue; const assetDict = await io.metadata();
for ( const [ assetKey, asset ] of Object.entries(assetDict) ) {
if ( asset.hasRemoteURL !== true ) { continue; }
if ( asset.content === 'filters' ) {
if ( µb.selectedFilterLists.includes(assetKey) === false ) {
continue;
}
} }
if ( asset.obsolete !== true ) { continue; }
const lastUpdateInDays = (now - asset.writeTime) / 86400000;
const daysSinceVeryObsolete = lastUpdateInDays - 2 * asset.updateAfter;
if ( daysSinceVeryObsolete < 0 ) { continue; }
needEmergencyUpdate = true;
lastEmergencyUpdate = now;
break;
} }
if ( asset.obsolete !== true ) { continue; }
const lastUpdateInDays = (now - asset.writeTime) / 86400000;
const daysSinceVeryObsolete = lastUpdateInDays - 2 * asset.updateAfter;
if ( daysSinceVeryObsolete < 0 ) { continue; }
needEmergencyUpdate = true;
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