mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 05:28:59 +01:00 
			
		
		
		
	small sync refactorings
This commit is contained in:
		
							parent
							
								
									542e82ee5d
								
							
						
					
					
						commit
						8d8ee2a87a
					
				@ -10,8 +10,8 @@ const log = require('../../services/log');
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
async function checkSync() {
 | 
					async function checkSync() {
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
        'hashes': await contentHashService.getHashes(),
 | 
					        hashes: await contentHashService.getHashes(),
 | 
				
			||||||
        'maxSyncId': await sql.getValue('SELECT MAX(id) FROM sync')
 | 
					        maxSyncId: await sql.getValue('SELECT MAX(id) FROM sync')
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -89,18 +89,8 @@ async function login() {
 | 
				
			|||||||
    return syncContext;
 | 
					    return syncContext;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function getLastSyncedPull() {
 | 
					 | 
				
			||||||
    return parseInt(await optionService.getOption('lastSyncedPull'));
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
async function setLastSyncedPull(syncId) {
 | 
					 | 
				
			||||||
    await optionService.setOption('lastSyncedPull', syncId);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
async function pullSync(syncContext) {
 | 
					async function pullSync(syncContext) {
 | 
				
			||||||
    const lastSyncedPull = await getLastSyncedPull();
 | 
					    const changesUri = '/api/sync/changed?lastSyncId=' + await getLastSyncedPull();
 | 
				
			||||||
 | 
					 | 
				
			||||||
    const changesUri = '/api/sync/changed?lastSyncId=' + lastSyncedPull;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const rows = await syncRequest(syncContext, 'GET', changesUri);
 | 
					    const rows = await syncRequest(syncContext, 'GET', changesUri);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -109,14 +99,6 @@ async function pullSync(syncContext) {
 | 
				
			|||||||
    for (const {sync, entity} of rows) {
 | 
					    for (const {sync, entity} of rows) {
 | 
				
			||||||
        if (sourceIdService.isLocalSourceId(sync.sourceId)) {
 | 
					        if (sourceIdService.isLocalSourceId(sync.sourceId)) {
 | 
				
			||||||
            log.info(`Skipping pull #${sync.id} ${sync.entityName} ${sync.entityId} because ${sync.sourceId} is a local source id.`);
 | 
					            log.info(`Skipping pull #${sync.id} ${sync.entityName} ${sync.entityId} because ${sync.sourceId} is a local source id.`);
 | 
				
			||||||
 | 
					 | 
				
			||||||
            await setLastSyncedPull(sync.id);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            continue;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (!entity) {
 | 
					 | 
				
			||||||
            log.error(`Empty response to pull for sync #${sync.id} ${sync.entityName}, id=${sync.entityId}`);
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else {
 | 
					        else {
 | 
				
			||||||
            await syncUpdateService.updateEntity(sync.entityName, entity, syncContext.sourceId);
 | 
					            await syncUpdateService.updateEntity(sync.entityName, entity, syncContext.sourceId);
 | 
				
			||||||
@ -128,14 +110,6 @@ async function pullSync(syncContext) {
 | 
				
			|||||||
    log.info("Finished pull");
 | 
					    log.info("Finished pull");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function getLastSyncedPush() {
 | 
					 | 
				
			||||||
    return parseInt(await optionService.getOption('lastSyncedPush'));
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
async function setLastSyncedPush(lastSyncedPush) {
 | 
					 | 
				
			||||||
    await optionService.setOption('lastSyncedPush', lastSyncedPush);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
async function pushSync(syncContext) {
 | 
					async function pushSync(syncContext) {
 | 
				
			||||||
    let lastSyncedPush = await getLastSyncedPush();
 | 
					    let lastSyncedPush = await getLastSyncedPush();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -159,8 +133,6 @@ async function pushSync(syncContext) {
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (filteredSyncs.length === 0) {
 | 
					        if (filteredSyncs.length === 0) {
 | 
				
			||||||
            // nothing to sync
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            log.info("Nothing to push");
 | 
					            log.info("Nothing to push");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            await setLastSyncedPush(lastSyncedPush);
 | 
					            await setLastSyncedPush(lastSyncedPush);
 | 
				
			||||||
@ -170,6 +142,8 @@ async function pushSync(syncContext) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        const syncRecords = await getSyncRecords(filteredSyncs);
 | 
					        const syncRecords = await getSyncRecords(filteredSyncs);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        log.info(`Pushing ${syncRecords.length} syncs.`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        await syncRequest(syncContext, 'PUT', '/api/sync/update', {
 | 
					        await syncRequest(syncContext, 'PUT', '/api/sync/update', {
 | 
				
			||||||
            sourceId: sourceIdService.getCurrentSourceId(),
 | 
					            sourceId: sourceIdService.getCurrentSourceId(),
 | 
				
			||||||
            entities: syncRecords
 | 
					            entities: syncRecords
 | 
				
			||||||
@ -190,11 +164,10 @@ async function checkContentHash(syncContext) {
 | 
				
			|||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const lastSyncedPush = await getLastSyncedPush();
 | 
					    const notPushedSyncs = await sql.getValue("SELECT COUNT(*) FROM sync WHERE id > ?", [await getLastSyncedPush()]);
 | 
				
			||||||
    const notPushedSyncs = await sql.getValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (notPushedSyncs > 0) {
 | 
					    if (notPushedSyncs > 0) {
 | 
				
			||||||
        log.info("There's " + notPushedSyncs + " outstanding pushes, skipping content check.");
 | 
					        log.info(`There's ${notPushedSyncs} outstanding pushes, skipping content check.`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -288,6 +261,22 @@ async function getSyncRecords(syncs) {
 | 
				
			|||||||
    return records;
 | 
					    return records;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function getLastSyncedPull() {
 | 
				
			||||||
 | 
					    return parseInt(await optionService.getOption('lastSyncedPull'));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function setLastSyncedPull(syncId) {
 | 
				
			||||||
 | 
					    await optionService.setOption('lastSyncedPull', syncId);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function getLastSyncedPush() {
 | 
				
			||||||
 | 
					    return parseInt(await optionService.getOption('lastSyncedPush'));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function setLastSyncedPush(lastSyncedPush) {
 | 
				
			||||||
 | 
					    await optionService.setOption('lastSyncedPush', lastSyncedPush);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
sqlInit.dbReady.then(() => {
 | 
					sqlInit.dbReady.then(() => {
 | 
				
			||||||
    if (syncSetup.isSyncSetup) {
 | 
					    if (syncSetup.isSyncSetup) {
 | 
				
			||||||
        log.info("Setting up sync to " + syncSetup.SYNC_SERVER + " with timeout " + syncSetup.SYNC_TIMEOUT);
 | 
					        log.info("Setting up sync to " + syncSetup.SYNC_SERVER + " with timeout " + syncSetup.SYNC_TIMEOUT);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user