add syncmanager

This commit is contained in:
Nystik
2026-03-28 18:50:32 +01:00
parent d8804daf2b
commit acb700a82b
3 changed files with 456 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
const path = require("path");
const obCli = require("./ob-cli");
const auth = require("./auth");
const { SyncManager } = require("./sync-manager");
module.exports = {
id: "headless-sync",
@@ -11,6 +12,7 @@ module.exports = {
_ctx: null,
_obStatus: null,
_syncManager: null,
async register(ctx) {
this._ctx = ctx;
@@ -29,11 +31,37 @@ module.exports = {
ctx.log("Auth token loaded");
}
this._syncManager = new SyncManager(ctx);
// Load saved sync states for enabled vaults
const enabledVaults = ctx.getEnabledVaults();
const vaultMap = {};
for (const vaultId of enabledVaults) {
const vaultPath = ctx.config.getVaultPath(vaultId);
if (vaultPath) {
vaultMap[vaultId] = vaultPath;
}
}
this._syncManager.loadStates(vaultMap);
// Auto-start syncs that were running before shutdown
if (this._obStatus.installed && auth.isAuthenticated(ctx.dataDir)) {
this._syncManager.autoStartAll();
}
const { mountRoutes } = require("./routes");
mountRoutes(ctx.router, this);
},
async shutdown() {
if (this._syncManager) {
await this._syncManager.shutdown();
this._syncManager = null;
}
this._ctx = null;
},
@@ -44,8 +72,19 @@ module.exports = {
},
async onVaultDisabled(vaultId, vaultPath) {
if (this._ctx) {
this._ctx.log(`Vault disabled: ${vaultId}`);
if (!this._ctx) {
return;
}
this._ctx.log(`Vault disabled: ${vaultId}`);
// Stop sync if running, but keep the config
if (this._syncManager) {
const state = this._syncManager.getState(vaultId);
if (state && state.status === "running") {
this._syncManager.stopSync(vaultId);
}
}
},
@@ -56,4 +95,8 @@ module.exports = {
getCtx() {
return this._ctx;
},
getSyncManager() {
return this._syncManager;
},
};