Improve sync logic

This commit is contained in:
Andras Schmelczer 2024-12-20 16:19:10 +00:00
parent ee76a6e26e
commit 359571a2a0
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
8 changed files with 487 additions and 304 deletions

View file

@ -23,6 +23,15 @@
- `cargo install sqlx-cli` - `cargo install sqlx-cli`
## cut new version
```sh
cd plugin
npm version patch
git tag -a 0.0.2 -m "0.0.2"
git push origin 0.0.2
```
@ -34,7 +43,7 @@
- e2e tests - e2e tests
- add clap - add clap
- add auth middleware - add auth middleware
- add request logs - run eslint in ci
- CI for: - CI for:
- publish reconcile - publish reconcile
@ -55,4 +64,11 @@ missing_docs_in_private_items = { level = "allow", priority = 1 }
question_mark_used = { level = "allow", priority = 1 } question_mark_used = { level = "allow", priority = 1 }
implicit_return = { level = "allow", priority = 1 } implicit_return = { level = "allow", priority = 1 }
pedantic = { level = "warn", priority = 0 } pedantic = { level = "warn", priority = 0 }
cargo = { level = "warn", priority = 0 } cargo = { level = "warn", priority = 0 }
reset should reset counters
access logs
retry
mem usage

View file

@ -1,41 +1,36 @@
import { Editor, MarkdownView, Plugin, WorkspaceLeaf } from "obsidian"; import type { WorkspaceLeaf } from "obsidian";
import { Plugin } from "obsidian";
import * as lib from "../../backend/sync_lib/pkg/sync_lib.js"; import * as lib from "../../backend/sync_lib/pkg/sync_lib.js";
import * as wasmBin from "../../backend/sync_lib/pkg/sync_lib_bg.wasm"; import * as wasmBin from "../../backend/sync_lib/pkg/sync_lib_bg.wasm";
import { SyncSettingsTab } from "./views/settings-tab"; import { SyncSettingsTab } from "./views/settings-tab";
import { SyncView } from "./views/sync-view"; import { SyncView } from "./views/sync-view";
import { Logger } from "./logger"; import { ObsidianFileEventHandler } from "./events/obisidan-event-handler.js";
import { SyncEventHandler } from "./events/sync-event-handler";
import { SyncService } from "./services/sync-service"; import { SyncService } from "./services/sync-service";
import { Database } from "./database/database"; import { Database } from "./database/database";
import { applyRemoteChangesLocally } from "./sync-operations/apply-remote-changes-locally"; import { applyRemoteChangesLocally } from "./sync-operations/apply-remote-changes-locally";
import { ObsidianFileOperations } from "./file-operations/obsidian-file-operations"; import { ObsidianFileOperations } from "./file-operations/obsidian-file-operations";
import { applyLocalChangesRemotely } from "./sync-operations/apply-local-changes-remotely"; import { applyLocalChangesRemotely } from "./sync-operations/apply-local-changes-remotely";
import { StatusBar } from "./views/status-bar"; import { StatusBar } from "./views/status-bar";
import { Logger } from "./tracing/logger.js";
import { SyncHistory } from "./tracing/sync-history.js";
export default class SyncPlugin extends Plugin { export default class SyncPlugin extends Plugin {
private remoteListenerIntervalId: number | null = null; private remoteListenerIntervalId: number | null = null;
private operations = new ObsidianFileOperations(this.app.vault); private readonly operations = new ObsidianFileOperations(this.app.vault);
private readonly history = new SyncHistory();
async onload() { public async onload(): Promise<void> {
Logger.getInstance().info('Starting plugin "Sample Plugin"'); Logger.getInstance().info("Starting plugin");
await lib.default( await lib.default(
Promise.resolve( Promise.resolve(
(wasmBin as any).default // eslint-disable-line @typescript-eslint/no-explicit-any // eslint-disable-next-line
(wasmBin as any).default
) )
); );
this.addCommand({
id: "sample-editor-command",
name: "Sample editor command",
editorCallback: (editor: Editor, _view: MarkdownView) => {
console.log(editor.getSelection());
editor.replaceSelection("Sample Editor Command");
},
});
const database = new Database( const database = new Database(
await this.loadData(), await this.loadData(),
this.saveData.bind(this) this.saveData.bind(this)
@ -43,19 +38,28 @@ export default class SyncPlugin extends Plugin {
const syncServer = new SyncService(database); const syncServer = new SyncService(database);
new StatusBar(this, syncServer); new StatusBar(this, this.history);
this.addSettingTab( this.addSettingTab(
new SyncSettingsTab(this.app, this, database, syncServer) new SyncSettingsTab(
this.app,
this,
database,
syncServer,
this.history
)
); );
const eventHandler = new SyncEventHandler( const eventHandler = new ObsidianFileEventHandler(
database, database,
syncServer, syncServer,
this.operations this.operations,
this.history
); );
this.app.workspace.onLayoutReady(() => { this.app.workspace.onLayoutReady(async () => {
Logger.getInstance().info("Initialising sync handlers");
[ [
this.app.vault.on( this.app.vault.on(
"create", "create",
@ -73,9 +77,18 @@ export default class SyncPlugin extends Plugin {
"rename", "rename",
eventHandler.onRename.bind(eventHandler) eventHandler.onRename.bind(eventHandler)
), ),
].forEach((event) => this.registerEvent(event)); ].forEach((event) => {
this.registerEvent(event);
});
applyLocalChangesRemotely(database, syncServer, this.operations); await applyLocalChangesRemotely({
database,
syncServer,
operations: this.operations,
history: this.history,
});
Logger.getInstance().info("Sync handlers initialised");
}); });
this.registerRemoteEventListener( this.registerRemoteEventListener(
@ -83,7 +96,9 @@ export default class SyncPlugin extends Plugin {
syncServer, syncServer,
database.getSettings().fetchChangesUpdateIntervalMs database.getSettings().fetchChangesUpdateIntervalMs
); );
database.addOnSettingsChangeHandlers((settings, oldSettings) => {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
database.addOnSettingsChangeHandlers(async (settings, oldSettings) => {
this.registerRemoteEventListener( this.registerRemoteEventListener(
database, database,
syncServer, syncServer,
@ -91,11 +106,12 @@ export default class SyncPlugin extends Plugin {
); );
if (!oldSettings.isSyncEnabled && settings.isSyncEnabled) { if (!oldSettings.isSyncEnabled && settings.isSyncEnabled) {
applyLocalChangesRemotely( await applyLocalChangesRemotely({
database, database: database,
syncServer, syncServer,
this.operations operations: this.operations,
); history: this.history,
});
} }
}); });
@ -104,12 +120,20 @@ export default class SyncPlugin extends Plugin {
const ribbonIconEl = this.addRibbonIcon( const ribbonIconEl = this.addRibbonIcon(
"dice", "dice",
"Sample Plugin", "Sample Plugin",
(_: MouseEvent) => this.activateView() async (_: MouseEvent) => this.activateView()
); );
ribbonIconEl.addClass("my-plugin-ribbon-class"); ribbonIconEl.addClass("my-plugin-ribbon-class");
Logger.getInstance().info("Plugin loaded");
} }
async activateView() { public onunload(): void {
if (this.remoteListenerIntervalId !== null) {
window.clearInterval(this.remoteListenerIntervalId);
}
}
private async activateView(): Promise<void> {
const { workspace } = this.app; const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = null; let leaf: WorkspaceLeaf | null = null;
@ -117,21 +141,17 @@ export default class SyncPlugin extends Plugin {
if (leaves.length > 0) { if (leaves.length > 0) {
// A leaf with our view already exists, use that // A leaf with our view already exists, use that
leaf = leaves[0]; [leaf] = leaves;
} else { } else {
// Our view could not be found in the workspace, create a new leaf // Our view could not be found in the workspace, create a new leaf
// in the right sidebar for it // In the right sidebar for it
leaf = workspace.getRightLeaf(false); leaf = workspace.getRightLeaf(false);
await leaf?.setViewState({ type: SyncView.TYPE, active: true }); await leaf?.setViewState({ type: SyncView.TYPE, active: true });
} }
// "Reveal" the leaf in case it is in a collapsed sidebar // "Reveal" the leaf in case it is in a collapsed sidebar
workspace.revealLeaf(leaf!); if (leaf) {
} await workspace.revealLeaf(leaf);
onunload(): void {
if (this.remoteListenerIntervalId) {
window.clearInterval(this.remoteListenerIntervalId);
} }
} }
@ -139,18 +159,20 @@ export default class SyncPlugin extends Plugin {
database: Database, database: Database,
syncServer: SyncService, syncServer: SyncService,
intervalMs: number intervalMs: number
) { ): void {
if (this.remoteListenerIntervalId) { if (this.remoteListenerIntervalId !== null) {
window.clearInterval(this.remoteListenerIntervalId); window.clearInterval(this.remoteListenerIntervalId);
} }
this.remoteListenerIntervalId = window.setInterval( this.remoteListenerIntervalId = window.setInterval(
() => // eslint-disable-next-line @typescript-eslint/no-misused-promises
applyRemoteChangesLocally( async () =>
applyRemoteChangesLocally({
database, database,
syncServer, syncServer,
this.operations operations: this.operations,
), history: this.history,
}),
intervalMs intervalMs
); );
} }

View file

@ -1,131 +1,130 @@
import { Database } from "../database/database"; import type { Database } from "../database/database";
import { SyncService } from "../services/sync-service"; import type { SyncService } from "../services/sync-service";
import { Logger } from "../logger"; import type { FileOperations } from "../file-operations/file-operations";
import { FileOperations } from "../file-operations/file-operations";
import { syncLocallyCreatedFile } from "./sync-locally-created-file"; import { syncLocallyCreatedFile } from "./sync-locally-created-file";
import { EMPTY_HASH, hash } from "src/utils/hash"; import { EMPTY_HASH, hash } from "src/utils/hash";
import { syncLocallyUpdatedFile } from "./sync-locally-updated-file"; import { syncLocallyUpdatedFile } from "./sync-locally-updated-file";
import { syncLocallyDeletedFile } from "./sync-locally-deleted-file"; import { syncLocallyDeletedFile } from "./sync-locally-deleted-file";
import { Notice } from "obsidian"; import { Logger } from "src/tracing/logger";
import PQueue from "p-queue"; import type { SyncHistory } from "src/tracing/sync-history";
let isRunning = false; let isRunning = false;
export interface Progress { export async function applyLocalChangesRemotely({
processedFiles: number; database,
totalFiles: number; syncServer,
} operations,
history,
export async function applyLocalChangesRemotely( }: {
database: Database, database: Database;
syncServer: SyncService, syncServer: SyncService;
operations: FileOperations operations: FileOperations;
) { history: SyncHistory;
console.log("applyLocalChangesRemotely"); }): Promise<void> {
if (isRunning) { if (isRunning) {
Logger.getInstance().info("Push sync already in progress, skipping"); Logger.getInstance().debug(
"Uploading local changes is already in progress, skipping"
);
return; return;
} }
let tasks: Promise<void>[] = []; isRunning = true;
try {
const tasks: Promise<void>[] = [];
const allLocalFiles = await operations.listAllFiles(); const allLocalFiles = await operations.listAllFiles();
console.log(allLocalFiles); const locallyDeletedFiles = [
const deletedFiles = [...database.getDocuments().entries()].filter( ...database.getDocuments().entries(),
([path, _]) => !allLocalFiles.includes(path) ].filter(([path, _]) => !allLocalFiles.includes(path));
);
console.log(deletedFiles); await Promise.all(
allLocalFiles.map(async (path) => {
const promiseQueue = new PQueue({ const metadata = database.getDocument(path);
concurrency: 1, if (!metadata) {
});
await Promise.all(
allLocalFiles.map((path) =>
promiseQueue.add(async () => {
const syncedState = database.getDocument(path);
if (!syncedState) {
Logger.getInstance().info(
`Document ${path} not found in database`
);
const contentHash = hash(await operations.read(path)); const contentHash = hash(await operations.read(path));
if (contentHash != EMPTY_HASH) { const match = locallyDeletedFiles.find(
const match = deletedFiles.find( ([_, document]) => document.hash === contentHash
([path, doc]) => doc.hash === contentHash );
if (contentHash != EMPTY_HASH && match) {
locallyDeletedFiles.remove(match);
Logger.getInstance().debug(
`Document ${path} not found in database but found under a different path ${match[0]}, scheduling sync to update it`
); );
if (match) { return syncLocallyUpdatedFile({
const oldPath = match[0];
Logger.getInstance().info(
`Document ${path} found remotely under a different path (${oldPath}), moving`
);
tasks.push(
syncLocallyUpdatedFile({
database,
syncServer,
operations,
oldPath,
filePath: path,
updateTime:
await operations.getModificationTime(
path
),
})
);
deletedFiles.remove(match);
return;
}
}
tasks.push(
syncLocallyCreatedFile({
database, database,
syncServer, syncServer,
operations, operations,
history,
oldPath: match[0],
relativePath: path,
updateTime: await operations.getModificationTime( updateTime: await operations.getModificationTime(
path path
), ),
filePath: path, });
}) }
Logger.getInstance().debug(
`Document ${path} not found in database, scheduling sync to create it`
); );
return; return syncLocallyCreatedFile({
database,
syncServer,
operations,
history,
updateTime: await operations.getModificationTime(path),
relativePath: path,
});
} }
const content = await operations.read(path); const content = await operations.read(path);
if (syncedState.hash !== hash(content)) { if (metadata.hash !== hash(content)) {
Logger.getInstance().info( Logger.getInstance().debug(
`Document ${path} has local changes, updating` `Document ${path} has been updated locally, scheduling sync to update it`
); );
tasks.push( return syncLocallyUpdatedFile({
syncLocallyUpdatedFile({ database,
database, syncServer,
syncServer, operations,
operations, history,
filePath: path, relativePath: path,
updateTime: await operations.getModificationTime( updateTime: await operations.getModificationTime(path),
path });
),
})
);
return;
} }
})
)
);
deletedFiles.forEach(([relativePath, _]) => { return Promise.resolve();
Logger.getInstance().info( })
`Document ${relativePath} deleted locally, deleting`
); );
tasks.push( tasks.push(
syncLocallyDeletedFile({ ...locallyDeletedFiles.map(async ([relativePath, _]) => {
database, Logger.getInstance().debug(
syncServer, `Document ${relativePath} has been deleted locally, scheduling sync to delete it`
relativePath, );
return syncLocallyDeletedFile({
database,
syncServer,
history,
relativePath,
});
}) })
); );
});
await Promise.all(tasks); try {
await Promise.all(tasks);
new Notice("Local changes synced remotely"); Logger.getInstance().info(
`All local changes have been applied remotely`
);
return;
} catch {
await Promise.allSettled(tasks);
Logger.getInstance().error(
`Not all local changes have been applied remotely`
);
}
} finally {
isRunning = false;
}
} }

View file

@ -1,29 +1,37 @@
import { Database } from "src/database/database"; import type { Database } from "src/database/database";
import { FileOperations } from "src/file-operations/file-operations"; import type { FileOperations } from "src/file-operations/file-operations";
import { Logger } from "src/logger"; import type { SyncService } from "src/services/sync-service";
import { SyncService } from "src/services/sync-service";
import { syncRemotelyUpdatedFile } from "./sync-remotely-updated-file"; import { syncRemotelyUpdatedFile } from "./sync-remotely-updated-file";
import { Logger } from "src/tracing/logger";
import type { SyncHistory } from "src/tracing/sync-history";
let isRunning = false; let isRunning = false;
export async function applyRemoteChangesLocally( export async function applyRemoteChangesLocally({
database: Database, database,
syncServer: SyncService, syncServer,
operations: FileOperations operations,
) { history,
if (isRunning) { }: {
Logger.getInstance().info("Pull sync already in progress, skipping"); database: Database;
syncServer: SyncService;
operations: FileOperations;
history: SyncHistory;
}): Promise<void> {
if (!database.getSettings().isSyncEnabled) {
Logger.getInstance().debug(
`Syncing is disabled, not fetching remote changes`
);
return;
} else if (isRunning) {
Logger.getInstance().debug(
"Applying remote changes locally is already in progress, skipping invocation"
);
return; return;
} else {
Logger.getInstance().info("Starting pull sync");
} }
isRunning = true; isRunning = true;
try { try {
if (!database.getSettings().isSyncEnabled) {
return;
}
const remote = await syncServer.getAll(database.getLastSeenUpdateId()); const remote = await syncServer.getAll(database.getLastSeenUpdateId());
if (remote.latestDocuments.length === 0) { if (remote.latestDocuments.length === 0) {
@ -34,11 +42,12 @@ export async function applyRemoteChangesLocally(
Logger.getInstance().info("Applying remote changes locally"); Logger.getInstance().info("Applying remote changes locally");
await Promise.all( await Promise.all(
remote.latestDocuments.map((remoteDocument) => remote.latestDocuments.map(async (remoteDocument) =>
syncRemotelyUpdatedFile({ syncRemotelyUpdatedFile({
database, database,
syncServer, syncServer,
operations: operations, history,
operations,
remoteVersion: remoteDocument, remoteVersion: remoteDocument,
}) })
) )

View file

@ -1,57 +1,91 @@
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js"; import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
import { Database } from "src/database/database"; import type { Database } from "src/database/database";
import { Logger } from "src/logger"; import type { SyncService } from "src/services/sync-service";
import { SyncService } from "src/services/sync-service";
import { hash } from "src/utils/hash"; import { hash } from "src/utils/hash";
import { unlockDocument, waitForDocumentLock } from "./locks"; import { unlockDocument, waitForDocumentLock } from "./locks";
import { FileOperations } from "src/file-operations/file-operations"; import type { FileOperations } from "src/file-operations/file-operations";
import { RelativePath } from "src/database/document-metadata"; import type { RelativePath } from "src/database/document-metadata";
import type { SyncHistory } from "src/tracing/sync-history.js";
import { SyncSource, SyncStatus, SyncType } from "src/tracing/sync-history.js";
import { Logger } from "src/tracing/logger.js";
/// This can be used when updating a files content and/or path.
export async function syncLocallyCreatedFile({ export async function syncLocallyCreatedFile({
database, database,
syncServer, syncServer,
operations, operations,
history,
updateTime, updateTime,
filePath, relativePath,
}: { }: {
database: Database; database: Database;
syncServer: SyncService; syncServer: SyncService;
operations: FileOperations; operations: FileOperations;
history: SyncHistory;
updateTime: Date; updateTime: Date;
filePath: RelativePath; relativePath: RelativePath;
}): Promise<void> { }): Promise<void> {
await waitForDocumentLock(filePath); if (!database.getSettings().isSyncEnabled) {
Logger.getInstance().info(
`Syncing is disabled, not syncing ${relativePath}`
);
return;
}
Logger.getInstance().debug(`Syncing ${relativePath}`);
await waitForDocumentLock(relativePath);
try { try {
const metadata = database.getDocument(filePath); const metadata = database.getDocument(relativePath);
if (metadata) { if (metadata) {
throw new Error( throw new Error(
`Document metadata found for ${filePath}, this is unexpected` `Document metadata found for ${relativePath}, this is unexpected. Consider resetting the plugin's sync history.`
); );
} }
const contentBytes = await operations.read(filePath); const contentBytes = await operations.read(relativePath),
contentHash = hash(contentBytes),
const response = await syncServer.create({ response = await syncServer.create({
relativePath: filePath, relativePath,
contentBytes, contentBytes,
createdDate: updateTime, createdDate: updateTime,
});
history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PUSH,
relativePath,
message: `Successfully uploaded locally created file`,
type: SyncType.CREATE,
}); });
const responseBytes = lib.base64_to_bytes(response.contentBase64); const responseBytes = lib.base64_to_bytes(response.contentBase64),
await operations.write(filePath, contentBytes, responseBytes); responseHash = hash(responseBytes);
if (contentHash !== responseHash) {
await operations.write(relativePath, contentBytes, responseBytes);
history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PULL,
relativePath,
message: `The file we created locally has already existed remotely, so we have merged them`,
type: SyncType.UPDATE,
});
}
await database.setDocument({ await database.setDocument({
documentId: response.documentId, documentId: response.documentId,
relativePath: response.relativePath, relativePath: response.relativePath,
parentVersionId: response.vaultUpdateId, parentVersionId: response.vaultUpdateId,
hash: hash(responseBytes), hash: responseHash,
}); });
} catch (e) { } catch (e) {
Logger.getInstance().error( history.addHistoryEntry({
`Failed to sync locally updated file ${filePath}: ${e}` status: SyncStatus.ERROR,
); relativePath,
message: `Failed to reconcile locally created file: ${e}`,
type: SyncType.CREATE,
});
throw e;
} finally { } finally {
unlockDocument(filePath); unlockDocument(relativePath);
} }
} }

View file

@ -1,26 +1,41 @@
import { Database } from "src/database/database"; import type { Database } from "src/database/database";
import { RelativePath } from "src/database/document-metadata"; import type { RelativePath } from "src/database/document-metadata";
import { Logger } from "src/logger"; import type { SyncService } from "src/services/sync-service";
import { SyncService } from "src/services/sync-service";
import { unlockDocument, waitForDocumentLock } from "./locks"; import { unlockDocument, waitForDocumentLock } from "./locks";
import { Logger } from "src/tracing/logger";
import type { SyncHistory } from "src/tracing/sync-history";
import { SyncSource, SyncStatus, SyncType } from "src/tracing/sync-history";
export async function syncLocallyDeletedFile({ export async function syncLocallyDeletedFile({
database, database,
syncServer, syncServer,
history,
relativePath, relativePath,
}: { }: {
database: Database; database: Database;
syncServer: SyncService; syncServer: SyncService;
history: SyncHistory;
relativePath: RelativePath; relativePath: RelativePath;
}): Promise<void> { }): Promise<void> {
if (!database.getSettings().isSyncEnabled) {
Logger.getInstance().info(
`Syncing is disabled, not syncing ${relativePath}`
);
return;
}
Logger.getInstance().debug(`Syncing ${relativePath}`);
await waitForDocumentLock(relativePath); await waitForDocumentLock(relativePath);
try { try {
const metadata = database.getDocument(relativePath); const metadata = database.getDocument(relativePath);
if (!metadata) { if (!metadata) {
Logger.getInstance().warn( history.addHistoryEntry({
`Document metadata not found for ${relativePath}` status: SyncStatus.NO_OP,
); relativePath,
message: `Locally deleted file hasn't been uploaded yet, so there's no need to delete it on the remote server`,
type: SyncType.DELETE,
});
return; return;
} }
@ -32,10 +47,22 @@ export async function syncLocallyDeletedFile({
}); });
await database.removeDocument(relativePath); await database.removeDocument(relativePath);
history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PUSH,
relativePath,
message: `Successfully deleted locally deleted file on the remote server`,
type: SyncType.DELETE,
});
} catch (e) { } catch (e) {
Logger.getInstance().error( history.addHistoryEntry({
`Failed to sync locally deleted file ${relativePath}: ${e}` status: SyncStatus.ERROR,
); relativePath,
message: `Failed to remotely delete locally deleted file: ${e}`,
type: SyncType.DELETE,
});
throw e;
} finally { } finally {
unlockDocument(relativePath); unlockDocument(relativePath);
} }

View file

@ -1,71 +1,103 @@
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js"; import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
import { Database } from "src/database/database"; import type { Database } from "src/database/database";
import { Logger } from "src/logger"; import type { SyncService } from "src/services/sync-service";
import { SyncService } from "src/services/sync-service";
import { hash } from "src/utils/hash"; import { hash } from "src/utils/hash";
import { unlockDocument, waitForDocumentLock } from "./locks"; import { unlockDocument, waitForDocumentLock } from "./locks";
import { FileOperations } from "src/file-operations/file-operations"; import type { FileOperations } from "src/file-operations/file-operations";
import { RelativePath } from "src/database/document-metadata"; import type { RelativePath } from "src/database/document-metadata";
import { Logger } from "src/tracing/logger.js";
import type { SyncHistory } from "src/tracing/sync-history.js";
import { SyncSource, SyncStatus, SyncType } from "src/tracing/sync-history.js";
/// This can be used when updating a files content and/or path. /// This can be used when updating a file's content and/or path.
export async function syncLocallyUpdatedFile({ export async function syncLocallyUpdatedFile({
database, database,
syncServer, syncServer,
operations, operations,
history,
updateTime, updateTime,
filePath, relativePath,
oldPath, oldPath,
}: { }: {
database: Database; database: Database;
syncServer: SyncService; syncServer: SyncService;
operations: FileOperations; operations: FileOperations;
history: SyncHistory;
updateTime: Date; updateTime: Date;
filePath: RelativePath; relativePath: RelativePath;
oldPath?: RelativePath; oldPath?: RelativePath;
}): Promise<void> { }): Promise<void> {
await waitForDocumentLock(filePath); if (!database.getSettings().isSyncEnabled) {
Logger.getInstance().info(
`Syncing is disabled, not syncing ${relativePath}`
);
return;
}
Logger.getInstance().debug(`Syncing ${relativePath}`);
await waitForDocumentLock(relativePath);
try { try {
const metadata = database.getDocument(oldPath || filePath); const metadata = database.getDocument(oldPath ?? relativePath);
if (!metadata) { if (!metadata) {
throw new Error(`Document metadata not found for ${filePath}`); throw new Error(
`Document metadata not found for ${relativePath}. Consider resetting the plugin's sync history.`
);
} }
const contentBytes = await operations.read(filePath); const contentBytes = await operations.read(relativePath),
const contentHash = hash(contentBytes); contentHash = hash(contentBytes);
if (metadata.hash === contentHash && !oldPath) { if (metadata.hash === contentHash && oldPath !== undefined) {
Logger.getInstance().info( history.addHistoryEntry({
`Document hash matches, no need to sync ${filePath}` status: SyncStatus.NO_OP,
); relativePath,
message: `File hash matches with last synced version, no need to sync`,
type: SyncType.UPDATE,
});
return; return;
} }
const response = await syncServer.put({ const response = await syncServer.put({
documentId: metadata.documentId, documentId: metadata.documentId,
parentVersionId: metadata.parentVersionId, parentVersionId: metadata.parentVersionId,
relativePath: filePath, relativePath,
contentBytes, contentBytes,
createdDate: updateTime, createdDate: updateTime,
}); });
if (response.isDeleted) { history.addHistoryEntry({
await operations.remove(oldPath || filePath); status: SyncStatus.SUCCESS,
source: SyncSource.PUSH,
relativePath,
message: `Successfully uploaded locally updated file to the remote server`,
type: SyncType.UPDATE,
});
if (metadata) { if (response.isDeleted) {
await database.removeDocument(oldPath || filePath); await operations.remove(oldPath ?? relativePath);
} await database.removeDocument(oldPath ?? relativePath);
history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PULL,
relativePath,
message:
"The file we tried to update had been deleted remotely, therefore, we have deleted it locally",
type: SyncType.DELETE,
});
return; return;
} }
const responseBytes = lib.base64_to_bytes(response.contentBase64); const responseBytes = lib.base64_to_bytes(response.contentBase64);
if (response.relativePath != filePath) { if (response.relativePath != relativePath) {
await waitForDocumentLock(response.relativePath); await waitForDocumentLock(response.relativePath);
try { try {
await operations.move( await operations.move(
oldPath || filePath, oldPath ?? relativePath,
response.relativePath response.relativePath
); );
await operations.write( await operations.write(
@ -73,25 +105,37 @@ export async function syncLocallyUpdatedFile({
contentBytes, contentBytes,
responseBytes responseBytes
); );
history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PULL,
relativePath,
message:
"The file we updated had been moved remotely, therefore, we have moved it locally as well",
type: SyncType.UPDATE,
});
} finally { } finally {
unlockDocument(response.relativePath); unlockDocument(response.relativePath);
} }
} else { } else {
await operations.write(filePath, contentBytes, responseBytes); await operations.write(relativePath, contentBytes, responseBytes);
} }
await database.moveDocument({ await database.moveDocument({
documentId: metadata.documentId, documentId: metadata.documentId,
oldRelativePath: oldPath || filePath, oldRelativePath: oldPath ?? relativePath,
relativePath: response.relativePath, relativePath: response.relativePath,
parentVersionId: response.vaultUpdateId, parentVersionId: response.vaultUpdateId,
hash: contentHash, hash: contentHash,
}); });
} catch (e) { } catch (e) {
Logger.getInstance().error( history.addHistoryEntry({
`Failed to sync locally updated file ${filePath}: ${e}` status: SyncStatus.ERROR,
); relativePath,
message: `Failed to reconcile locally updated file: ${e}`,
type: SyncType.UPDATE,
});
throw e;
} finally { } finally {
unlockDocument(filePath); unlockDocument(relativePath);
} }
} }

View file

@ -1,110 +1,142 @@
import { Database } from "src/database/database"; import type { Database } from "src/database/database";
import { unlockDocument, waitForDocumentLock } from "./locks"; import { unlockDocument, waitForDocumentLock } from "./locks";
import { SyncService } from "src/services/sync-service"; import type { SyncService } from "src/services/sync-service";
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js"; import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
import { hash } from "src/utils/hash"; import { hash } from "src/utils/hash";
import { Logger } from "src/logger"; import type { components } from "src/services/types";
import { components } from "src/services/types"; import type { FileOperations } from "src/file-operations/file-operations";
import { FileOperations } from "src/file-operations/file-operations"; import { Logger } from "src/tracing/logger";
import type { SyncHistory } from "src/tracing/sync-history";
import { SyncSource, SyncStatus, SyncType } from "src/tracing/sync-history";
export async function syncRemotelyUpdatedFile({ export async function syncRemotelyUpdatedFile({
database, database,
syncServer, syncServer,
operations, operations,
history,
remoteVersion, remoteVersion,
}: { }: {
database: Database; database: Database;
syncServer: SyncService; syncServer: SyncService;
operations: FileOperations; operations: FileOperations;
history: SyncHistory;
remoteVersion: components["schemas"]["DocumentVersionWithoutContent"]; remoteVersion: components["schemas"]["DocumentVersionWithoutContent"];
}): Promise<void> { }): Promise<void> {
Logger.getInstance().info( Logger.getInstance().debug(
`Syncing remotely updated file ${remoteVersion.relativePath}` `Syncing remotely updated file ${remoteVersion.relativePath}`
); );
const content = (
await syncServer.get({
documentId: remoteVersion.documentId,
})
).contentBase64;
const currentVersion = database.getDocumentByDocumentId( try {
remoteVersion.documentId const content = (
); await syncServer.get({
documentId: remoteVersion.documentId,
})
).contentBase64,
currentVersion = database.getDocumentByDocumentId(
remoteVersion.documentId
);
if (!currentVersion) { if (!currentVersion) {
if (remoteVersion.isDeleted) { if (remoteVersion.isDeleted) {
history.addHistoryEntry({
status: SyncStatus.NO_OP,
source: SyncSource.PULL,
relativePath: remoteVersion.relativePath,
message: `Remotely deleted file hasn't been synced yet, so there's no need to delete it locally`,
type: SyncType.DELETE,
});
return;
}
await waitForDocumentLock(remoteVersion.relativePath);
try {
const contentBytes = lib.base64_to_bytes(content);
await operations.create(
remoteVersion.relativePath,
contentBytes
);
await database.setDocument({
documentId: remoteVersion.documentId,
relativePath: remoteVersion.relativePath,
parentVersionId: remoteVersion.vaultUpdateId,
hash: hash(contentBytes),
});
history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PULL,
relativePath: remoteVersion.relativePath,
message: `Successfully downloaded remote file which hasn't existed locally`,
type: SyncType.CREATE,
});
} finally {
unlockDocument(remoteVersion.relativePath);
}
return; return;
} }
Logger.getInstance().info( const [relativePath, metadata] = currentVersion;
`Document metadata not found for ${remoteVersion.relativePath}, it must be new` await waitForDocumentLock(relativePath);
);
await waitForDocumentLock(remoteVersion.relativePath);
try { try {
const contentBytes = lib.base64_to_bytes(content); if (remoteVersion.isDeleted) {
operations.create(remoteVersion.relativePath, contentBytes); await operations.remove(relativePath);
await database.setDocument({
documentId: remoteVersion.documentId,
relativePath: remoteVersion.relativePath,
parentVersionId: remoteVersion.vaultUpdateId,
hash: hash(contentBytes),
});
} finally {
unlockDocument(remoteVersion.relativePath);
}
return;
}
const [relativePath, metadata] = currentVersion;
await waitForDocumentLock(relativePath);
try {
if (remoteVersion.isDeleted) {
Logger.getInstance().info(
`Document ${relativePath} has been deleted remotely`
);
await operations.remove(relativePath);
if (metadata) {
await database.removeDocument(relativePath); await database.removeDocument(relativePath);
}
} else {
const currentContent = await operations.read(relativePath);
const currentHash = hash(currentContent);
if (currentHash !== metadata.hash) {
Logger.getInstance().info(
`Document ${relativePath} has been updated both remotely and locally, skipping`
);
return;
} else {
if (relativePath !== remoteVersion.relativePath) {
await operations.move(
relativePath,
remoteVersion.relativePath
);
}
const contentBytes = lib.base64_to_bytes(content); history.addHistoryEntry({
await operations.write( status: SyncStatus.SUCCESS,
remoteVersion.relativePath, source: SyncSource.PULL,
currentContent,
contentBytes
);
await database.moveDocument({
documentId: remoteVersion.documentId,
oldRelativePath: relativePath,
relativePath: remoteVersion.relativePath, relativePath: remoteVersion.relativePath,
parentVersionId: remoteVersion.vaultUpdateId, message: `Successfully deleted remotely deleted file locally`,
hash: metadata.hash, type: SyncType.DELETE,
}); });
} else {
const currentContent = await operations.read(relativePath),
currentHash = hash(currentContent);
if (currentHash !== metadata.hash) {
Logger.getInstance().info(
`Document ${relativePath} has been updated both remotely and locally, skipping until the event is processed`
);
} else {
if (relativePath !== remoteVersion.relativePath) {
await operations.move(
relativePath,
remoteVersion.relativePath
);
}
const contentBytes = lib.base64_to_bytes(content);
await operations.write(
remoteVersion.relativePath,
currentContent,
contentBytes
);
await database.moveDocument({
documentId: remoteVersion.documentId,
oldRelativePath: relativePath,
relativePath: remoteVersion.relativePath,
parentVersionId: remoteVersion.vaultUpdateId,
hash: metadata.hash,
});
history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PULL,
relativePath: remoteVersion.relativePath,
message: `Successfully updated remotely updated file locally`,
type: SyncType.UPDATE,
});
}
} }
} finally {
unlockDocument(relativePath);
} }
} catch (e) { } catch (e) {
Logger.getInstance().error( history.addHistoryEntry({
`Failed to sync remotely updated file ${remoteVersion.relativePath}: ${e}` status: SyncStatus.ERROR,
); source: SyncSource.PULL,
} finally { relativePath: remoteVersion.relativePath,
unlockDocument(relativePath); message: `Failed to reconcile remotely updated file: ${e}`,
});
throw e;
} }
} }