Fix: bump db-to-cloud, show detailed LockError (#1361)

* Bump db-to-cloud

* Fix: bump db-to-cloud, show detailed LockError

* Fix: used -> use

* Change: drop our retry code
This commit is contained in:
eight 2021-12-09 00:00:30 +08:00 committed by GitHub
parent e23077a7ea
commit 7e3c6f16e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 22 deletions

View File

@ -1669,6 +1669,14 @@
"message": "Sync failed.\nTry to re-login in Stylus options:\nclick 'disconnect' first, then 'connect'.", "message": "Sync failed.\nTry to re-login in Stylus options:\nclick 'disconnect' first, then 'connect'.",
"description": "Tooltip for the toolbar icon" "description": "Tooltip for the toolbar icon"
}, },
"syncErrorLock": {
"message": "The database is already in use. The lock will expire at $TIME$",
"placeholders": {
"time": {
"content": "$1"
}
}
},
"syncStorageErrorSaving": { "syncStorageErrorSaving": {
"message": "The value cannot be saved. Try reducing the amount of text.", "message": "The value cannot be saved. Try reducing the amount of text.",
"description": "Displayed when trying to save an excessively big value via storage.sync API" "description": "Displayed when trying to save an excessively big value via storage.sync API"

View File

@ -11,7 +11,6 @@ const syncMan = (() => {
const SYNC_DELAY = 1; // minutes const SYNC_DELAY = 1; // minutes
const SYNC_INTERVAL = 30; // minutes const SYNC_INTERVAL = 30; // minutes
const SYNC_LOCK_RETRIES = 10; // number of retries before the error is reported for scheduled sync
const STATES = Object.freeze({ const STATES = Object.freeze({
connected: 'connected', connected: 'connected',
connecting: 'connecting', connecting: 'connecting',
@ -27,7 +26,6 @@ const syncMan = (() => {
currentDriveName: null, currentDriveName: null,
errorMessage: null, errorMessage: null,
login: false, login: false,
lockRetries: 0,
}; };
let lastError = null; let lastError = null;
let ctrl; let ctrl;
@ -44,9 +42,7 @@ const syncMan = (() => {
chrome.alarms.onAlarm.addListener(async ({name}) => { chrome.alarms.onAlarm.addListener(async ({name}) => {
if (name === 'syncNow') { if (name === 'syncNow') {
await syncMan.syncNow({isScheduled: true}); await syncMan.syncNow();
const retrying = status.lockRetries / SYNC_LOCK_RETRIES * Math.random();
schedule(SYNC_DELAY + SYNC_INTERVAL * (retrying || 1));
} }
}); });
@ -143,7 +139,7 @@ const syncMan = (() => {
emitStatusChange(); emitStatusChange();
}, },
async syncNow({isScheduled} = {}) { async syncNow() {
if (ready.then) await ready; if (ready.then) await ready;
if (!currentDrive || !status.login) { if (!currentDrive || !status.login) {
console.warn('cannot sync when disconnected'); console.warn('cannot sync when disconnected');
@ -154,18 +150,13 @@ const syncMan = (() => {
status.errorMessage = null; status.errorMessage = null;
lastError = null; lastError = null;
} catch (err) { } catch (err) {
err.message = translateErrorMessage(err);
status.errorMessage = err.message; status.errorMessage = err.message;
lastError = err; lastError = err;
if (isScheduled &&
err.code === 409 &&
++status.lockRetries <= SYNC_LOCK_RETRIES) {
return;
}
if (isGrantError(err)) { if (isGrantError(err)) {
status.login = false; status.login = false;
} }
} }
status.lockRetries = 0;
emitStatusChange(); emitStatusChange();
}, },
}; };
@ -208,6 +199,9 @@ const syncMan = (() => {
setState(drive, state) { setState(drive, state) {
return chromeLocal.setValue(STORAGE_KEY + drive.name, state); return chromeLocal.setValue(STORAGE_KEY + drive.name, state);
}, },
retryMaxAttempts: 10,
retryExp: 1.2,
retryDelay: 6,
}); });
} }
@ -257,8 +251,16 @@ const syncMan = (() => {
function schedule(delay = SYNC_DELAY) { function schedule(delay = SYNC_DELAY) {
chrome.alarms.create('syncNow', { chrome.alarms.create('syncNow', {
delayInMinutes: delay, // fractional values are supported delayInMinutes: delay, // fractional values are supported
periodInMinutes: SYNC_INTERVAL,
}); });
} }
function translateErrorMessage(err) {
if (err.name === 'LockError') {
return browser.i18n.getMessage('syncErrorLock', new Date(err.expire).toLocaleString([], {timeStyle: 'short'}));
}
return err.message || String(err);
}
//#endregion //#endregion
})(); })();

14
package-lock.json generated
View File

@ -10,7 +10,7 @@
"license": "GPL-3.0-only", "license": "GPL-3.0-only",
"dependencies": { "dependencies": {
"codemirror": "5.63.3", "codemirror": "5.63.3",
"db-to-cloud": "^0.6.0", "db-to-cloud": "^0.7.0",
"jsonlint": "^1.6.3", "jsonlint": "^1.6.3",
"less-bundle": "github:openstyles/less-bundle#v0.1.0", "less-bundle": "github:openstyles/less-bundle#v0.1.0",
"lz-string-unsafe": "^1.4.4-fork-1", "lz-string-unsafe": "^1.4.4-fork-1",
@ -2322,9 +2322,9 @@
} }
}, },
"node_modules/db-to-cloud": { "node_modules/db-to-cloud": {
"version": "0.6.0", "version": "0.7.0",
"resolved": "https://registry.npmjs.org/db-to-cloud/-/db-to-cloud-0.6.0.tgz", "resolved": "https://registry.npmjs.org/db-to-cloud/-/db-to-cloud-0.7.0.tgz",
"integrity": "sha512-AbvxpU+fA3Fsdzu0OxL+cVPS9HwM6DzXFDg00WIQ3YeMkWs5saMXpiXMfISlkpBUwm5Cbr4W7cfhYszu38BzSw==", "integrity": "sha512-fs3FS6vMLk7E6gnuao9lJqTU+ohy6Pa9fqgZSVnoICMsCfqrnyYj+reA81xOmw9kfqVQNdtu+zZv67IJThrMvA==",
"dependencies": { "dependencies": {
"@eight04/read-write-lock": "^0.1.0", "@eight04/read-write-lock": "^0.1.0",
"universal-base64": "^2.1.0" "universal-base64": "^2.1.0"
@ -11501,9 +11501,9 @@
} }
}, },
"db-to-cloud": { "db-to-cloud": {
"version": "0.6.0", "version": "0.7.0",
"resolved": "https://registry.npmjs.org/db-to-cloud/-/db-to-cloud-0.6.0.tgz", "resolved": "https://registry.npmjs.org/db-to-cloud/-/db-to-cloud-0.7.0.tgz",
"integrity": "sha512-AbvxpU+fA3Fsdzu0OxL+cVPS9HwM6DzXFDg00WIQ3YeMkWs5saMXpiXMfISlkpBUwm5Cbr4W7cfhYszu38BzSw==", "integrity": "sha512-fs3FS6vMLk7E6gnuao9lJqTU+ohy6Pa9fqgZSVnoICMsCfqrnyYj+reA81xOmw9kfqVQNdtu+zZv67IJThrMvA==",
"requires": { "requires": {
"@eight04/read-write-lock": "^0.1.0", "@eight04/read-write-lock": "^0.1.0",
"universal-base64": "^2.1.0" "universal-base64": "^2.1.0"

View File

@ -10,7 +10,7 @@
}, },
"dependencies": { "dependencies": {
"codemirror": "5.63.3", "codemirror": "5.63.3",
"db-to-cloud": "^0.6.0", "db-to-cloud": "^0.7.0",
"jsonlint": "^1.6.3", "jsonlint": "^1.6.3",
"less-bundle": "github:openstyles/less-bundle#v0.1.0", "less-bundle": "github:openstyles/less-bundle#v0.1.0",
"lz-string-unsafe": "^1.4.4-fork-1", "lz-string-unsafe": "^1.4.4-fork-1",

View File

@ -1,4 +1,4 @@
## db-to-cloud v0.6.0 ## db-to-cloud v0.7.0
Following files are copied from npm (node_modules): Following files are copied from npm (node_modules):

File diff suppressed because one or more lines are too long