#98, sync is now configured in the options

This commit is contained in:
azivner
2018-07-22 22:21:16 +02:00
parent 073300bbcd
commit e7460ca3a9
15 changed files with 115 additions and 46 deletions

View File

@@ -3,7 +3,7 @@
const build = require('./build');
const packageJson = require('../../package');
const APP_DB_VERSION = 100;
const APP_DB_VERSION = 101;
const SYNC_VERSION = 1;
module.exports = {

View File

@@ -73,7 +73,7 @@ async function sendPing(client, lastSentSyncId) {
await sendMessage(client, {
type: 'sync',
data: syncData,
changesToPushCount: syncSetup.isSyncSetup ? changesToPushCount : 0
changesToPushCount: await syncSetup.isSyncSetup() ? changesToPushCount : 0
});
}

View File

@@ -85,7 +85,7 @@ async function migrate() {
}
}
if (sqlInit.isDbUpToDate()) {
if (await sqlInit.isDbUpToDate()) {
await sqlInit.initDbConnection();
}

View File

@@ -34,6 +34,10 @@ async function initOptions(startNotePath, username, password) {
await optionService.createOption('encryptedDataKeyIv', '');
await passwordEncryptionService.setDataKey(password, utils.randomSecureToken(16));
await optionService.createOption('syncServerHost', '', false);
await optionService.createOption('syncServerTimeout', 5000, false);
await optionService.createOption('syncProxy', '', false);
}
module.exports = {

View File

@@ -31,8 +31,8 @@ async function initDbConnection() {
await sql.execute("PRAGMA foreign_keys = ON");
if (isDbInitialized()) {
log.info("DB not found, please visit setup page to initialize Trilium.");
if (!await isDbInitialized()) {
log.info("DB not initialized, please visit setup page to initialize Trilium.");
return;
}

View File

@@ -17,7 +17,6 @@ const syncMutexService = require('./sync_mutex');
const cls = require('./cls');
let proxyToggle = true;
let syncServerCertificate = null;
async function sync() {
try {
@@ -169,7 +168,7 @@ async function checkContentHash(syncContext) {
}
async function syncRequest(syncContext, method, uri, body) {
const fullUri = syncSetup.SYNC_SERVER + uri;
const fullUri = await syncSetup.getSyncServer() + uri;
try {
const options = {
@@ -178,15 +177,13 @@ async function syncRequest(syncContext, method, uri, body) {
jar: syncContext.cookieJar,
json: true,
body: body,
timeout: syncSetup.SYNC_TIMEOUT
timeout: await syncSetup.getSyncTimeout()
};
if (syncServerCertificate) {
options.ca = syncServerCertificate;
}
const syncProxy = await syncSetup.getSyncProxy();
if (syncSetup.SYNC_PROXY && proxyToggle) {
options.proxy = syncSetup.SYNC_PROXY;
if (syncProxy && proxyToggle) {
options.proxy = syncProxy;
}
return await rp(options);
@@ -270,18 +267,14 @@ async function setLastSyncedPush(lastSyncedPush) {
await optionService.setOption('lastSyncedPush', lastSyncedPush);
}
sqlInit.dbReady.then(() => {
if (syncSetup.isSyncSetup) {
log.info("Setting up sync to " + syncSetup.SYNC_SERVER + " with timeout " + syncSetup.SYNC_TIMEOUT);
sqlInit.dbReady.then(async () => {
if (await syncSetup.isSyncSetup()) {
log.info("Setting up sync to " + await syncSetup.getSyncServer() + " with timeout " + await syncSetup.getSyncTimeout());
if (syncSetup.SYNC_PROXY) {
log.info("Sync proxy: " + syncSetup.SYNC_PROXY);
}
const syncProxy = await syncSetup.getSyncProxy();
if (syncSetup.SYNC_CERT_PATH) {
log.info('Sync certificate: ' + syncSetup.SYNC_CERT_PATH);
syncServerCertificate = fs.readFileSync(syncSetup.SYNC_CERT_PATH);
if (syncProxy) {
log.info("Sync proxy: " + syncProxy);
}
setInterval(cls.wrap(sync), 60000);

View File

@@ -1,11 +1,11 @@
"use strict";
const config = require('./config');
const optionService = require('./options');
module.exports = {
SYNC_SERVER: config['Sync']['syncServerHost'],
isSyncSetup: !!config['Sync']['syncServerHost'],
SYNC_TIMEOUT: config['Sync']['syncServerTimeout'] || 5000,
SYNC_PROXY: config['Sync']['syncProxy'],
SYNC_CERT_PATH: config['Sync']['syncServerCertificate']
getSyncServer: 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

@@ -54,7 +54,7 @@ async function addEntitySync(entityName, entityId, sourceId) {
sourceId: sourceId || cls.getSourceId() || sourceIdService.getCurrentSourceId()
});
if (!syncSetup.isSyncSetup) {
if (!await syncSetup.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')");