Notes/src/services/setup.js

119 lines
3.5 KiB
JavaScript
Raw Normal View History

const syncService = require('./sync.js');
2024-02-16 20:27:00 +02:00
const log = require('./log.ts');
const sqlInit = require('./sql_init.js');
const optionService = require('./options.js');
const syncOptions = require('./sync_options.js');
const request = require('./request.js');
const appInfo = require('./app_info.js');
const utils = require('./utils.js');
const becca = require('../becca/becca.js');
2018-09-11 10:01:40 +02:00
async function hasSyncServerSchemaAndSeed() {
const response = await requestToSyncServer('GET', '/api/setup/status');
if (response.syncVersion !== appInfo.syncVersion) {
throw new Error(`Could not setup sync since local sync protocol version is ${appInfo.syncVersion} while remote is ${response.syncVersion}. To fix this issue, use same Trilium version on all instances.`);
}
2018-09-11 10:01:40 +02:00
return response.schemaExists;
}
function triggerSync() {
log.info("Triggering sync.");
// it's ok to not wait for it here
2020-06-20 12:31:38 +02:00
syncService.sync().then(res => {
if (res.success) {
2020-06-20 21:42:41 +02:00
sqlInit.setDbAsInitialized();
}
});
}
2018-09-11 10:01:40 +02:00
async function sendSeedToSyncServer() {
log.info("Initiating sync to server");
await requestToSyncServer('POST', '/api/setup/sync-seed', {
2020-06-20 12:31:38 +02:00
options: getSyncSeedOptions(),
syncVersion: appInfo.syncVersion
});
2023-06-30 11:18:34 +02:00
// this is a completely new sync, need to reset counters. If this was not a new sync,
// the previous request would have failed.
2020-06-20 12:31:38 +02:00
optionService.setOption('lastSyncedPush', 0);
optionService.setOption('lastSyncedPull', 0);
}
async function requestToSyncServer(method, path, body = null) {
2020-06-20 12:31:38 +02:00
const timeout = syncOptions.getSyncTimeout();
2020-06-20 12:31:38 +02:00
return await utils.timeLimit(request.exec({
method,
2020-06-20 12:31:38 +02:00
url: syncOptions.getSyncServerHost() + path,
body,
2020-06-20 12:31:38 +02:00
proxy: syncOptions.getSyncProxy(),
timeout: timeout
}), timeout);
}
2021-12-29 23:37:12 +01:00
async function setupSyncFromSyncServer(syncServerHost, syncProxy, password) {
2020-06-20 12:31:38 +02:00
if (sqlInit.isDbInitialized()) {
return {
result: 'failure',
error: 'DB is already initialized.'
};
}
try {
2020-08-06 00:06:42 +02:00
log.info("Getting document options FROM sync server.");
2023-06-30 11:18:34 +02:00
// the response is expected to contain documentId and documentSecret options
const resp = await request.exec({
method: 'get',
url: `${syncServerHost}/api/setup/sync-seed`,
2021-12-29 23:37:12 +01:00
auth: { password },
proxy: syncProxy,
timeout: 30000 // seed request should not take long
});
if (resp.syncVersion !== appInfo.syncVersion) {
const message = `Could not setup sync since local sync protocol version is ${appInfo.syncVersion} while remote is ${resp.syncVersion}. To fix this issue, use same Trilium version on all instances.`;
log.error(message);
return {
result: 'failure',
error: message
}
}
2020-06-20 12:31:38 +02:00
sqlInit.createDatabaseForSync(resp.options, syncServerHost, syncProxy);
triggerSync();
return { result: 'success' };
}
catch (e) {
2023-09-21 11:16:03 +02:00
log.error(`Sync failed: '${e.message}', stack: ${e.stack}`);
return {
result: 'failure',
error: e.message
};
}
}
2020-06-20 12:31:38 +02:00
function getSyncSeedOptions() {
2018-07-25 09:46:57 +02:00
return [
2021-05-02 11:23:58 +02:00
becca.getOption('documentId'),
becca.getOption('documentSecret')
2018-07-25 09:46:57 +02:00
];
}
module.exports = {
2018-09-11 10:01:40 +02:00
hasSyncServerSchemaAndSeed,
triggerSync,
2018-09-11 10:01:40 +02:00
sendSeedToSyncServer,
setupSyncFromSyncServer,
getSyncSeedOptions
};