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

@ -1,71 +1,103 @@
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
import { Database } from "src/database/database";
import { Logger } from "src/logger";
import { SyncService } from "src/services/sync-service";
import type { Database } from "src/database/database";
import type { SyncService } from "src/services/sync-service";
import { hash } from "src/utils/hash";
import { unlockDocument, waitForDocumentLock } from "./locks";
import { FileOperations } from "src/file-operations/file-operations";
import { RelativePath } from "src/database/document-metadata";
import type { FileOperations } from "src/file-operations/file-operations";
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({
database,
syncServer,
operations,
history,
updateTime,
filePath,
relativePath,
oldPath,
}: {
database: Database;
syncServer: SyncService;
operations: FileOperations;
history: SyncHistory;
updateTime: Date;
filePath: RelativePath;
relativePath: RelativePath;
oldPath?: RelativePath;
}): 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 {
const metadata = database.getDocument(oldPath || filePath);
const metadata = database.getDocument(oldPath ?? relativePath);
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 contentHash = hash(contentBytes);
const contentBytes = await operations.read(relativePath),
contentHash = hash(contentBytes);
if (metadata.hash === contentHash && !oldPath) {
Logger.getInstance().info(
`Document hash matches, no need to sync ${filePath}`
);
if (metadata.hash === contentHash && oldPath !== undefined) {
history.addHistoryEntry({
status: SyncStatus.NO_OP,
relativePath,
message: `File hash matches with last synced version, no need to sync`,
type: SyncType.UPDATE,
});
return;
}
const response = await syncServer.put({
documentId: metadata.documentId,
parentVersionId: metadata.parentVersionId,
relativePath: filePath,
relativePath,
contentBytes,
createdDate: updateTime,
});
if (response.isDeleted) {
await operations.remove(oldPath || filePath);
history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PUSH,
relativePath,
message: `Successfully uploaded locally updated file to the remote server`,
type: SyncType.UPDATE,
});
if (metadata) {
await database.removeDocument(oldPath || filePath);
}
if (response.isDeleted) {
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;
}
const responseBytes = lib.base64_to_bytes(response.contentBase64);
if (response.relativePath != filePath) {
if (response.relativePath != relativePath) {
await waitForDocumentLock(response.relativePath);
try {
await operations.move(
oldPath || filePath,
oldPath ?? relativePath,
response.relativePath
);
await operations.write(
@ -73,25 +105,37 @@ export async function syncLocallyUpdatedFile({
contentBytes,
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 {
unlockDocument(response.relativePath);
}
} else {
await operations.write(filePath, contentBytes, responseBytes);
await operations.write(relativePath, contentBytes, responseBytes);
}
await database.moveDocument({
documentId: metadata.documentId,
oldRelativePath: oldPath || filePath,
oldRelativePath: oldPath ?? relativePath,
relativePath: response.relativePath,
parentVersionId: response.vaultUpdateId,
hash: contentHash,
});
} catch (e) {
Logger.getInstance().error(
`Failed to sync locally updated file ${filePath}: ${e}`
);
history.addHistoryEntry({
status: SyncStatus.ERROR,
relativePath,
message: `Failed to reconcile locally updated file: ${e}`,
type: SyncType.UPDATE,
});
throw e;
} finally {
unlockDocument(filePath);
unlockDocument(relativePath);
}
}