mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
yet another attempt at fixing reporting sync changes to client
This commit is contained in:
parent
f54d855f55
commit
333735543e
2
app.js
2
app.js
@ -10,8 +10,6 @@ const FileStore = require('session-file-store')(session);
|
|||||||
const os = require('os');
|
const os = require('os');
|
||||||
const sessionSecret = require('./services/session_secret');
|
const sessionSecret = require('./services/session_secret');
|
||||||
|
|
||||||
require('./services/ping_job');
|
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
// view engine setup
|
// view engine setup
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
const messaging = (function() {
|
const messaging = (function() {
|
||||||
const changesToPushCountEl = $("#changes-to-push-count");
|
const changesToPushCountEl = $("#changes-to-push-count");
|
||||||
let ws = null;
|
|
||||||
|
|
||||||
function logError(message) {
|
function logError(message) {
|
||||||
console.log(now(), message); // needs to be separate from .trace()
|
console.log(now(), message); // needs to be separate from .trace()
|
||||||
@ -21,12 +20,15 @@ const messaging = (function() {
|
|||||||
|
|
||||||
if (message.type === 'sync') {
|
if (message.type === 'sync') {
|
||||||
lastPingTs = new Date().getTime();
|
lastPingTs = new Date().getTime();
|
||||||
const syncData = message.data.filter(sync => sync.source_id !== glob.sourceId);
|
|
||||||
|
|
||||||
if (syncData.length > 0) {
|
if (message.data.length > 0) {
|
||||||
console.log(now(), "Sync data: ", message);
|
console.log(now(), "Sync data: ", message.data);
|
||||||
|
|
||||||
|
lastSyncId = message.data[message.data.length - 1].id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const syncData = message.data.filter(sync => sync.source_id !== glob.sourceId);
|
||||||
|
|
||||||
if (syncData.some(sync => sync.entity_name === 'notes_tree')) {
|
if (syncData.some(sync => sync.entity_name === 'notes_tree')) {
|
||||||
console.log(now(), "Reloading tree because of background changes");
|
console.log(now(), "Reloading tree because of background changes");
|
||||||
|
|
||||||
@ -59,17 +61,20 @@ const messaging = (function() {
|
|||||||
const protocol = document.location.protocol === 'https:' ? 'wss' : 'ws';
|
const protocol = document.location.protocol === 'https:' ? 'wss' : 'ws';
|
||||||
|
|
||||||
// use wss for secure messaging
|
// use wss for secure messaging
|
||||||
ws = new WebSocket(protocol + "://" + location.host);
|
const ws = new WebSocket(protocol + "://" + location.host);
|
||||||
ws.onopen = event => console.log(now(), "Connected to server with WebSocket");
|
ws.onopen = event => console.log(now(), "Connected to server with WebSocket");
|
||||||
ws.onmessage = messageHandler;
|
ws.onmessage = messageHandler;
|
||||||
ws.onclose = function(){
|
ws.onclose = function(){
|
||||||
// Try to reconnect in 5 seconds
|
// Try to reconnect in 5 seconds
|
||||||
setTimeout(() => connectWebSocket(), 5000);
|
setTimeout(() => connectWebSocket(), 5000);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
return ws;
|
||||||
}
|
}
|
||||||
|
|
||||||
connectWebSocket();
|
const ws = connectWebSocket();
|
||||||
|
|
||||||
|
let lastSyncId = glob.maxSyncIdAtLoad;
|
||||||
let lastPingTs = new Date().getTime();
|
let lastPingTs = new Date().getTime();
|
||||||
let connectionBrokenNotification = null;
|
let connectionBrokenNotification = null;
|
||||||
|
|
||||||
@ -92,7 +97,12 @@ const messaging = (function() {
|
|||||||
|
|
||||||
showMessage("Re-connected to server");
|
showMessage("Re-connected to server");
|
||||||
}
|
}
|
||||||
}, 3000);
|
|
||||||
|
ws.send(JSON.stringify({
|
||||||
|
type: 'ping',
|
||||||
|
lastSyncId: lastSyncId
|
||||||
|
}));
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
logError
|
logError
|
||||||
|
@ -4,10 +4,12 @@ const express = require('express');
|
|||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const auth = require('../services/auth');
|
const auth = require('../services/auth');
|
||||||
const source_id = require('../services/source_id');
|
const source_id = require('../services/source_id');
|
||||||
|
const sql = require('../services/sql');
|
||||||
|
|
||||||
router.get('', auth.checkAuth, async (req, res, next) => {
|
router.get('', auth.checkAuth, async (req, res, next) => {
|
||||||
res.render('index', {
|
res.render('index', {
|
||||||
sourceId: await source_id.generateSourceId()
|
sourceId: await source_id.generateSourceId(),
|
||||||
|
maxSyncIdAtLoad: await sql.getSingleValue("SELECT MAX(id) FROM sync")
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ async function runChecks() {
|
|||||||
await runSyncRowChecks("recent_notes", "note_tree_id", errorList);
|
await runSyncRowChecks("recent_notes", "note_tree_id", errorList);
|
||||||
|
|
||||||
if (errorList.length > 0) {
|
if (errorList.length > 0) {
|
||||||
messaging.sendMessage({type: 'consistency-checks-failed'});
|
messaging.sendMessageToAllClients({type: 'consistency-checks-failed'});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
log.info("All consistency checks passed.");
|
log.info("All consistency checks passed.");
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
const WebSocket = require('ws');
|
const WebSocket = require('ws');
|
||||||
const utils = require('./utils');
|
const utils = require('./utils');
|
||||||
const log = require('./log');
|
const log = require('./log');
|
||||||
|
const sql = require('./sql');
|
||||||
|
const options = require('./options');
|
||||||
|
const sync_setup = require('./sync_setup');
|
||||||
|
|
||||||
let webSocketServer;
|
let webSocketServer;
|
||||||
|
|
||||||
@ -29,6 +32,9 @@ function init(httpServer, sessionParser) {
|
|||||||
if (message.type === 'log-error') {
|
if (message.type === 'log-error') {
|
||||||
log.error('JS Error: ' + message.error);
|
log.error('JS Error: ' + message.error);
|
||||||
}
|
}
|
||||||
|
else if (message.type === 'ping') {
|
||||||
|
sendPing(ws, message.lastSyncId);
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
log.error('Unrecognized message: ');
|
log.error('Unrecognized message: ');
|
||||||
log.error(message);
|
log.error(message);
|
||||||
@ -37,7 +43,15 @@ function init(httpServer, sessionParser) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function sendMessage(message) {
|
async function sendMessage(client, message) {
|
||||||
|
const jsonStr = JSON.stringify(message);
|
||||||
|
|
||||||
|
if (client.readyState === WebSocket.OPEN) {
|
||||||
|
client.send(jsonStr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendMessageToAllClients(message) {
|
||||||
const jsonStr = JSON.stringify(message);
|
const jsonStr = JSON.stringify(message);
|
||||||
|
|
||||||
webSocketServer.clients.forEach(function each(client) {
|
webSocketServer.clients.forEach(function each(client) {
|
||||||
@ -47,7 +61,21 @@ async function sendMessage(message) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function sendPing(client, lastSentSyncId) {
|
||||||
|
const syncData = await sql.getResults("SELECT * FROM sync WHERE id > ?", [lastSentSyncId]);
|
||||||
|
|
||||||
|
const lastSyncedPush = await options.getOption('last_synced_push');
|
||||||
|
|
||||||
|
const changesToPushCount = await sql.getSingleValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
|
||||||
|
|
||||||
|
await sendMessage(client, {
|
||||||
|
type: 'sync',
|
||||||
|
data: syncData,
|
||||||
|
changesToPushCount: sync_setup.isSyncSetup ? changesToPushCount : 0
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init,
|
init,
|
||||||
sendMessage
|
sendMessageToAllClients
|
||||||
};
|
};
|
@ -1,30 +0,0 @@
|
|||||||
const sql = require('./sql');
|
|
||||||
const messaging = require('./messaging');
|
|
||||||
const options = require('./options');
|
|
||||||
const sync_setup = require('./sync_setup');
|
|
||||||
|
|
||||||
let lastSentSyncId;
|
|
||||||
|
|
||||||
async function sendPing() {
|
|
||||||
const syncData = await sql.getResults("SELECT * FROM sync WHERE id > ?", [lastSentSyncId]);
|
|
||||||
|
|
||||||
const lastSyncedPush = await options.getOption('last_synced_push');
|
|
||||||
|
|
||||||
const changesToPushCount = await sql.getSingleValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
|
|
||||||
|
|
||||||
messaging.sendMessage({
|
|
||||||
type: 'sync',
|
|
||||||
data: syncData,
|
|
||||||
changesToPushCount: sync_setup.isSyncSetup ? changesToPushCount : 0
|
|
||||||
});
|
|
||||||
|
|
||||||
if (syncData.length > 0) {
|
|
||||||
lastSentSyncId = syncData[syncData.length - 1].id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sql.dbReady.then(async () => {
|
|
||||||
lastSentSyncId = await sql.getSingleValue("SELECT MAX(id) FROM sync");
|
|
||||||
|
|
||||||
setInterval(sendPing, 1000);
|
|
||||||
});
|
|
@ -267,7 +267,7 @@ async function checkContentHash(syncContext) {
|
|||||||
|
|
||||||
if (key !== 'recent_notes') {
|
if (key !== 'recent_notes') {
|
||||||
// let's not get alarmed about recent notes which get updated often and can cause failures in race conditions
|
// let's not get alarmed about recent notes which get updated often and can cause failures in race conditions
|
||||||
await messaging.sendMessage({type: 'sync-hash-check-failed'});
|
await messaging.sendMessageToAllClients({type: 'sync-hash-check-failed'});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -301,7 +301,8 @@
|
|||||||
const baseApiUrl = 'api/';
|
const baseApiUrl = 'api/';
|
||||||
const glob = {
|
const glob = {
|
||||||
activeDialog: null,
|
activeDialog: null,
|
||||||
sourceId: '<%= sourceId %>'
|
sourceId: '<%= sourceId %>',
|
||||||
|
maxSyncIdAtLoad: <%= maxSyncIdAtLoad %>
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user