reddit plugin configuration from file, not from options now. Scheduling, refactoring of sync mutex

This commit is contained in:
azivner
2018-01-13 22:51:39 -05:00
parent fbfaff6ab8
commit 9839ea019e
5 changed files with 63 additions and 49 deletions

View File

@@ -23,9 +23,8 @@ async function regularBackup() {
async function backupNow() {
// we don't want to backup DB in the middle of sync with potentially inconsistent DB state
const releaseMutex = await sync_mutex.acquire();
try {
await sync_mutex.doExclusively(async () => {
const now = utils.nowDate();
const backupFile = dataDir.BACKUP_DIR + "/" + "backup-" + utils.getDateTimeForFile() + ".db";
@@ -37,10 +36,7 @@ async function backupNow() {
await sql.doInTransaction(async () => {
await options.setOption('last_backup_date', now);
});
}
finally {
releaseMutex();
}
});
}
async function cleanupOldBackups() {

View File

@@ -217,18 +217,14 @@ async function runAllChecks() {
async function runChecks() {
let errorList;
let elapsedTimeMs;
const releaseMutex = await sync_mutex.acquire();
try {
await sync_mutex.doExclusively(async () => {
const startTime = new Date();
errorList = await runAllChecks();
elapsedTimeMs = new Date().getTime() - startTime.getTime();
}
finally {
releaseMutex();
}
});
if (errorList.length > 0) {
log.info(`Consistency checks failed (took ${elapsedTimeMs}ms) with these errors: ` + JSON.stringify(errorList));

View File

@@ -20,25 +20,25 @@ let proxyToggle = true;
let syncServerCertificate = null;
async function sync() {
const releaseMutex = await sync_mutex.acquire();
try {
if (!await sql.isDbUpToDate()) {
return {
success: false,
message: "DB not up to date"
};
}
await sync_mutex.doExclusively(async () => {
if (!await sql.isDbUpToDate()) {
return {
success: false,
message: "DB not up to date"
};
}
const syncContext = await login();
const syncContext = await login();
await pushSync(syncContext);
await pushSync(syncContext);
await pullSync(syncContext);
await pullSync(syncContext);
await pushSync(syncContext);
await pushSync(syncContext);
await checkContentHash(syncContext);
await checkContentHash(syncContext);
});
return {
success: true
@@ -64,9 +64,6 @@ async function sync() {
}
}
}
finally {
releaseMutex();
}
}
async function login() {

View File

@@ -4,5 +4,20 @@
*/
const Mutex = require('async-mutex').Mutex;
const instance = new Mutex();
module.exports = new Mutex();
async function doExclusively(func) {
const releaseMutex = await instance.acquire();
try {
await func();
}
finally {
releaseMutex();
}
}
module.exports = {
doExclusively
};