#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

@ -223,7 +223,7 @@ addTabHandler((function() {
}); });
$syncToServerButton.click(async () => { $syncToServerButton.click(async () => {
await server.post("sync/sync-to-server"); await server.post("setup/sync-to-server");
infoService.showMessage("Sync has been established to the server instance. It will take some time to finish."); infoService.showMessage("Sync has been established to the server instance. It will take some time to finish.");
}); });

View File

@ -2,6 +2,10 @@
const sqlInit = require('../../services/sql_init'); const sqlInit = require('../../services/sql_init');
const setupService = require('../../services/setup'); const setupService = require('../../services/setup');
const optionService = require('../../services/options');
const syncService = require('../../services/sync');
const log = require('../../services/log');
const rp = require('request-promise');
async function setupNewDocument(req) { async function setupNewDocument(req) {
const { username, password } = req.body; const { username, password } = req.body;
@ -15,14 +19,51 @@ async function setupSyncFromServer(req) {
return await setupService.setupSyncFromSyncServer(syncServerHost, syncProxy, username, password); return await setupService.setupSyncFromSyncServer(syncServerHost, syncProxy, username, password);
} }
async function setupSyncFromClient(req) { async function setupSyncToSyncServer() {
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-seed',
method: 'POST',
json: true,
body: {
options: await setupService.getSyncSeedOptions()
}
};
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 saveSyncSeed(req) {
const options = req.body.options; const options = req.body.options;
await sqlInit.createDatabaseForSync(options); await sqlInit.createDatabaseForSync(options);
} }
async function getSyncSeed() {
log.info("Serving sync seed.");
return await setupService.getSyncSeedOptions();
}
module.exports = { module.exports = {
setupNewDocument, setupNewDocument,
setupSyncFromServer, setupSyncFromServer,
setupSyncFromClient setupSyncToSyncServer,
getSyncSeed,
saveSyncSeed
}; };

View File

@ -8,8 +8,6 @@ const sqlInit = require('../../services/sql_init');
const optionService = require('../../services/options'); const optionService = require('../../services/options');
const contentHashService = require('../../services/content_hash'); const contentHashService = require('../../services/content_hash');
const log = require('../../services/log'); const log = require('../../services/log');
const repository = require('../../services/repository');
const rp = require('request-promise');
async function testSync() { async function testSync() {
try { try {
@ -26,6 +24,11 @@ async function testSync() {
} }
async function getStats() { async function getStats() {
if (!await sqlInit.schemaExists()) {
// fail silently but prevent errors from not existing options table
return {};
}
return { return {
initialized: await optionService.getOption('initialized') === 'true', initialized: await optionService.getOption('initialized') === 'true',
stats: syncService.stats 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() { async function syncFinished() {
// after first sync finishes, the application is ready to be used // after first sync finishes, the application is ready to be used
// this is meaningless but at the same time harmless (idempotent) for further syncs // this is meaningless but at the same time harmless (idempotent) for further syncs
@ -156,8 +117,6 @@ module.exports = {
forceNoteSync, forceNoteSync,
getChanged, getChanged,
update, update,
getDocument,
getStats, getStats,
syncToServer,
syncFinished syncFinished
}; };

View File

@ -156,9 +156,7 @@ function register(app) {
apiRoute(POST, '/api/sync/force-note-sync/:noteId', syncApiRoute.forceNoteSync); apiRoute(POST, '/api/sync/force-note-sync/:noteId', syncApiRoute.forceNoteSync);
apiRoute(GET, '/api/sync/changed', syncApiRoute.getChanged); apiRoute(GET, '/api/sync/changed', syncApiRoute.getChanged);
apiRoute(PUT, '/api/sync/update', syncApiRoute.update); apiRoute(PUT, '/api/sync/update', syncApiRoute.update);
route(GET, '/api/sync/document', [auth.checkBasicAuth], syncApiRoute.getDocument, apiResultHandler);
route(GET, '/api/sync/stats', [], syncApiRoute.getStats, apiResultHandler); route(GET, '/api/sync/stats', [], syncApiRoute.getStats, apiResultHandler);
apiRoute(POST, '/api/sync/sync-to-server', syncApiRoute.syncToServer);
apiRoute(POST, '/api/sync/finished', syncApiRoute.syncFinished); apiRoute(POST, '/api/sync/finished', syncApiRoute.syncFinished);
apiRoute(GET, '/api/event-log', eventLogRoute.getEventLog); apiRoute(GET, '/api/event-log', eventLogRoute.getEventLog);
@ -169,7 +167,9 @@ function register(app) {
route(POST, '/api/setup/new-document', [auth.checkAppNotInitialized], setupApiRoute.setupNewDocument, apiResultHandler); route(POST, '/api/setup/new-document', [auth.checkAppNotInitialized], setupApiRoute.setupNewDocument, apiResultHandler);
route(POST, '/api/setup/sync-from-server', [auth.checkAppNotInitialized], setupApiRoute.setupSyncFromServer, apiResultHandler, false); route(POST, '/api/setup/sync-from-server', [auth.checkAppNotInitialized], setupApiRoute.setupSyncFromServer, apiResultHandler, false);
route(POST, '/api/setup/sync-from-client', [auth.checkAppNotInitialized], setupApiRoute.setupSyncFromClient, apiResultHandler, false); apiRoute(POST, '/api/setup/sync-to-server', setupApiRoute.setupSyncToSyncServer);
route(GET, '/api/setup/sync-seed', [auth.checkBasicAuth], setupApiRoute.getSyncSeed, apiResultHandler);
route(POST, '/api/setup/sync-seed', [auth.checkAppNotInitialized], setupApiRoute.saveSyncSeed, apiResultHandler, false);
apiRoute(POST, '/api/sql/execute', sqlRoute.execute); apiRoute(POST, '/api/sql/execute', sqlRoute.execute);
apiRoute(POST, '/api/anonymization/anonymize', anonymizationRoute.anonymize); apiRoute(POST, '/api/anonymization/anonymize', anonymizationRoute.anonymize);

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
const sql = require('./sql'); const sql = require('./sql');
const sourceIdService = require('./source_id'); const sourceIdService = require('./source_id');
const dateUtils = require('./date_utils'); const dateUtils = require('./date_utils');
const syncSetup = require('./sync_setup'); const syncOptions = require('./sync_options');
const log = require('./log'); const log = require('./log');
const cls = require('./cls'); const cls = require('./cls');
const eventService = require('./events'); const eventService = require('./events');
@ -54,7 +54,7 @@ async function addEntitySync(entityName, entityId, sourceId) {
sourceId: sourceId || cls.getSourceId() || sourceIdService.getCurrentSourceId() 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 // 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 // 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')"); await sql.execute("UPDATE options SET value = (SELECT MAX(id) FROM sync) WHERE name IN('lastSyncedPush', 'lastSyncedPull')");