Improve sync logic

This commit is contained in:
Andras Schmelczer 2024-12-20 16:19:10 +00:00
commit 359571a2a0
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -1,26 +1,41 @@
import { Database } from "src/database/database";
import { RelativePath } from "src/database/document-metadata";
import { Logger } from "src/logger";
import { SyncService } from "src/services/sync-service";
import type { Database } from "src/database/database";
import type { RelativePath } from "src/database/document-metadata";
import type { SyncService } from "src/services/sync-service";
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({
database,
syncServer,
history,
relativePath,
}: {
database: Database;
syncServer: SyncService;
history: SyncHistory;
relativePath: RelativePath;
}): Promise<void> {
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(relativePath);
if (!metadata) {
Logger.getInstance().warn(
`Document metadata not found for ${relativePath}`
);
history.addHistoryEntry({
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;
}
@ -32,10 +47,22 @@ export async function syncLocallyDeletedFile({
});
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) {
Logger.getInstance().error(
`Failed to sync locally deleted file ${relativePath}: ${e}`
);
history.addHistoryEntry({
status: SyncStatus.ERROR,
relativePath,
message: `Failed to remotely delete locally deleted file: ${e}`,
type: SyncType.DELETE,
});
throw e;
} finally {
unlockDocument(relativePath);
}