#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

@@ -2,6 +2,7 @@ const rp = require('request-promise');
const syncService = require('./sync');
const log = require('./log');
const sqlInit = require('./sql_init');
const repository = require('./repository');
function triggerSync() {
log.info("Triggering sync.");
@@ -27,7 +28,7 @@ async function setupSyncFromSyncServer(syncServerHost, syncProxy, username, pass
// response is expected to contain documentId and documentSecret options
const options = await rp.get({
uri: syncServerHost + '/api/sync/document',
uri: syncServerHost + '/api/setup/sync-seed',
auth: {
'user': username,
'pass': password
@@ -55,7 +56,15 @@ async function setupSyncFromSyncServer(syncServerHost, syncProxy, username, pass
}
}
async function getSyncSeedOptions() {
return [
await repository.getOption('documentId'),
await repository.getOption('documentSecret')
];
}
module.exports = {
setupSyncFromSyncServer,
getSyncSeedOptions,
triggerSync
};

View File

@@ -11,7 +11,7 @@ const dateUtils = require('./date_utils');
const syncUpdateService = require('./sync_update');
const contentHashService = require('./content_hash');
const appInfo = require('./app_info');
const syncSetup = require('./sync_setup');
const syncOptions = require('./sync_options');
const syncMutexService = require('./sync_mutex');
const cls = require('./cls');
@@ -25,7 +25,7 @@ const stats = {
async function sync() {
try {
return await syncMutexService.doExclusively(async () => {
if (!await syncSetup.isSyncSetup()) {
if (!await syncOptions.isSyncSetup()) {
return { success: false, message: 'Sync not configured' };
}
@@ -196,7 +196,7 @@ async function checkContentHash(syncContext) {
}
async function syncRequest(syncContext, method, uri, body) {
const fullUri = await syncSetup.getSyncServer() + uri;
const fullUri = await syncOptions.getSyncServerHost() + uri;
try {
const options = {
@@ -205,10 +205,10 @@ async function syncRequest(syncContext, method, uri, body) {
jar: syncContext.cookieJar,
json: true,
body: body,
timeout: await syncSetup.getSyncTimeout()
timeout: await syncOptions.getSyncTimeout()
};
const syncProxy = await syncSetup.getSyncProxy();
const syncProxy = await syncOptions.getSyncProxy();
if (syncProxy && proxyToggle) {
options.proxy = syncProxy;
@@ -302,10 +302,10 @@ async function updatePushStats() {
}
sqlInit.dbReady.then(async () => {
if (await syncSetup.isSyncSetup()) {
log.info("Setting up sync to " + await syncSetup.getSyncServer() + " with timeout " + await syncSetup.getSyncTimeout());
if (await syncOptions.isSyncSetup()) {
log.info("Setting up sync to " + await syncOptions.getSyncServerHost() + " with timeout " + await syncOptions.getSyncTimeout());
const syncProxy = await syncSetup.getSyncProxy();
const syncProxy = await syncOptions.getSyncProxy();
if (syncProxy) {
log.info("Sync proxy: " + syncProxy);

View File

@@ -3,7 +3,7 @@
const optionService = require('./options');
module.exports = {
getSyncServer: async () => await optionService.getOption('syncServerHost'),
getSyncServerHost: async () => await optionService.getOption('syncServerHost'),
isSyncSetup: async () => !!await optionService.getOption('syncServerHost'),
getSyncTimeout: async () => await optionService.getOption('syncServerTimeout'),
getSyncProxy: async () => await optionService.getOption('syncProxy')

View File

@@ -1,7 +1,7 @@
const sql = require('./sql');
const sourceIdService = require('./source_id');
const dateUtils = require('./date_utils');
const syncSetup = require('./sync_setup');
const syncOptions = require('./sync_options');
const log = require('./log');
const cls = require('./cls');
const eventService = require('./events');
@@ -54,7 +54,7 @@ async function addEntitySync(entityName, entityId, sourceId) {
sourceId: sourceId || cls.getSourceId() || sourceIdService.getCurrentSourceId()
});
if (!await syncSetup.isSyncSetup()) {
if (!await syncOptions.isSyncSetup()) {
// this is because the "server" instances shouldn't have outstanding pushes
// useful when you fork the DB for new "client" instance, it won't try to sync the whole DB
await sql.execute("UPDATE options SET value = (SELECT MAX(id) FROM sync) WHERE name IN('lastSyncedPush', 'lastSyncedPull')");