mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 05:28:59 +01:00 
			
		
		
		
	#98 some sync setup refactorings
This commit is contained in:
		
							parent
							
								
									c8253caae9
								
							
						
					
					
						commit
						a4627f2ddb
					
				@ -223,7 +223,7 @@ addTabHandler((function() {
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    $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.");
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,10 @@
 | 
			
		||||
 | 
			
		||||
const sqlInit = require('../../services/sql_init');
 | 
			
		||||
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) {
 | 
			
		||||
    const { username, password } = req.body;
 | 
			
		||||
@ -15,14 +19,51 @@ async function setupSyncFromServer(req) {
 | 
			
		||||
    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;
 | 
			
		||||
 | 
			
		||||
    await sqlInit.createDatabaseForSync(options);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function getSyncSeed() {
 | 
			
		||||
    log.info("Serving sync seed.");
 | 
			
		||||
 | 
			
		||||
    return await setupService.getSyncSeedOptions();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
    setupNewDocument,
 | 
			
		||||
    setupSyncFromServer,
 | 
			
		||||
    setupSyncFromClient
 | 
			
		||||
    setupSyncToSyncServer,
 | 
			
		||||
    getSyncSeed,
 | 
			
		||||
    saveSyncSeed
 | 
			
		||||
};
 | 
			
		||||
@ -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
 | 
			
		||||
};
 | 
			
		||||
@ -156,9 +156,7 @@ function register(app) {
 | 
			
		||||
    apiRoute(POST, '/api/sync/force-note-sync/:noteId', syncApiRoute.forceNoteSync);
 | 
			
		||||
    apiRoute(GET, '/api/sync/changed', syncApiRoute.getChanged);
 | 
			
		||||
    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);
 | 
			
		||||
    apiRoute(POST, '/api/sync/sync-to-server', syncApiRoute.syncToServer);
 | 
			
		||||
    apiRoute(POST, '/api/sync/finished', syncApiRoute.syncFinished);
 | 
			
		||||
 | 
			
		||||
    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/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/anonymization/anonymize', anonymizationRoute.anonymize);
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
};
 | 
			
		||||
@ -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);
 | 
			
		||||
 | 
			
		||||
@ -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')
 | 
			
		||||
@ -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')");
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user