better transaction handling with rollback on exception

This commit is contained in:
azivner
2017-10-29 18:50:28 -04:00
parent afadd6ec06
commit de3d1b3e39
8 changed files with 145 additions and 150 deletions

View File

@@ -34,41 +34,37 @@ async function pullSync(cookieJar, syncLog) {
}
try {
await sql.beginTransaction();
await sql.doInTransaction(async () => {
await putChanged(resp, syncLog);
await putChanged(resp, syncLog);
for (const noteId of resp.notes) {
let note;
for (const noteId of resp.notes) {
let note;
try {
note = await rp({
uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSyncedPull,
headers: {
auth: 'sync'
},
json: true,
jar: cookieJar
});
}
catch (e) {
throw new Error("Can't pull note " + noteId + ", inner exception: " + e.stack);
}
try {
note = await rp({
uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSyncedPull,
headers: {
auth: 'sync'
},
json: true,
jar: cookieJar
});
}
catch (e) {
throw new Error("Can't pull note " + noteId + ", inner exception: " + e.stack);
await putNote(note, syncLog);
}
await putNote(note, syncLog);
}
if (resp.notes.length > 0) {
await sql.addAudit(audit_category.SYNC);
}
if (resp.notes.length > 0) {
await sql.addAudit(audit_category.SYNC);
}
await sql.setOption('last_synced_pull', resp.syncTimestamp);
await sql.commit();
await sql.setOption('last_synced_pull', resp.syncTimestamp);
});
}
catch (e) {
await sql.rollback();
throw e;
}
}