#98 some sync setup refactorings

This commit is contained in:
azivner
2018-07-25 09:46:57 +02:00
parent c8253caae9
commit a4627f2ddb
8 changed files with 73 additions and 64 deletions

View File

@@ -8,8 +8,6 @@ const sqlInit = require('../../services/sql_init');
const optionService = require('../../services/options');
const contentHashService = require('../../services/content_hash');
const log = require('../../services/log');
const repository = require('../../services/repository');
const rp = require('request-promise');
async function testSync() {
try {
@@ -26,6 +24,11 @@ async function testSync() {
}
async function getStats() {
if (!await sqlInit.schemaExists()) {
// fail silently but prevent errors from not existing options table
return {};
}
return {
initialized: await optionService.getOption('initialized') === 'true',
stats: syncService.stats
@@ -99,48 +102,6 @@ async function update(req) {
}
}
async function getDocumentOptions() {
return [
await repository.getOption('documentId'),
await repository.getOption('documentSecret')
];
}
async function getDocument() {
log.info("Serving document options.");
return await getDocumentOptions();
}
async function syncToServer() {
log.info("Initiating sync to server");
const syncServerHost = await optionService.getOption('syncServerHost');
const syncProxy = await optionService.getOption('syncProxy');
const rpOpts = {
uri: syncServerHost + '/api/setup/sync-from-client',
method: 'POST',
json: true,
body: {
options: await getDocumentOptions()
}
};
if (syncProxy) {
rpOpts.proxy = syncProxy;
}
await rp(rpOpts);
// this is completely new sync, need to reset counters. If this would not be new sync,
// the previous request would have failed.
await optionService.setOption('lastSyncedPush', 0);
await optionService.setOption('lastSyncedPull', 0);
syncService.sync();
}
async function syncFinished() {
// after first sync finishes, the application is ready to be used
// this is meaningless but at the same time harmless (idempotent) for further syncs
@@ -156,8 +117,6 @@ module.exports = {
forceNoteSync,
getChanged,
update,
getDocument,
getStats,
syncToServer,
syncFinished
};