Skip to content

Commit

Permalink
fix: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Sep 6, 2024
1 parent 862038b commit 2042440
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@filen/sync",
"version": "0.1.56",
"version": "0.1.57",
"description": "Filen Sync",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
8 changes: 7 additions & 1 deletion src/ignorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ export class Ignorer {
const filePath = pathModule.join(this.sync.dbPath, this.name, `v${IGNORER_VERSION}`, this.sync.syncPair.uuid, "filenIgnore")

await fs.ensureDir(pathModule.dirname(filePath))
await fs.unlink(filePath)

await fs.rm(filePath, {
force: true,
maxRetries: 60 * 10,
recursive: true,
retryDelay: 100
})
}

public ignores(path: string): boolean {
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ export class SyncWorker {
* @returns {Promise<void>}
*/
public async updateSyncPairs(pairs: SyncPair[]): Promise<void> {
if (pairs.length === 0) {
return
}

await this.updateSyncPairsMutex.acquire()

try {
Expand Down Expand Up @@ -192,11 +196,13 @@ export class SyncWorker {
}
}

public updateRemoved(uuid: string, removed: boolean): void {
public async updateRemoved(uuid: string, removed: boolean): Promise<void> {
for (const syncUUID in this.syncs) {
if (syncUUID === uuid) {
this.syncs[syncUUID]!.removed = removed

await this.syncs[syncUUID]!.cleanup()

const abortControllers = this.syncs[syncUUID]!.abortControllers

for (const controller in abortControllers) {
Expand Down
8 changes: 7 additions & 1 deletion src/lib/filesystems/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ export class RemoteFileSystem {
const deviceIdFile = pathModule.join(this.sync.dbPath, "deviceId", `v${DEVICE_ID_VERSION}`, this.sync.syncPair.uuid)

await fs.ensureDir(pathModule.dirname(deviceIdFile))
await fs.unlink(deviceIdFile)

await fs.rm(deviceIdFile, {
force: true,
maxRetries: 60 * 10,
recursive: true,
retryDelay: 100
})
}

public async getDirectoryTree(skipCache: boolean = false): Promise<{
Expand Down
49 changes: 42 additions & 7 deletions src/lib/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,12 @@ export class State {
})
} finally {
if (await fs.pathExists(tmpDestination)) {
await fs.unlink(tmpDestination)
await fs.rm(tmpDestination, {
force: true,
maxRetries: 60 * 10,
recursive: true,
retryDelay: 100
})
}
}
}
Expand Down Expand Up @@ -445,7 +450,12 @@ export class State {

for (const file of dir) {
if (file.endsWith(".tmp")) {
await fs.unlink(pathModule.join(this.statePath, file))
await fs.rm(pathModule.join(this.statePath, file), {
force: true,
maxRetries: 60 * 10,
recursive: true,
retryDelay: 100
})
}
}

Expand Down Expand Up @@ -477,11 +487,36 @@ export class State {
await fs.ensureDir(this.statePath)

await Promise.all([
fs.unlink(this.previousLocalTreePath),
fs.unlink(this.previousLocalINodesPath),
fs.unlink(this.previousRemoteTreePath),
fs.unlink(this.previousRemoteUUIDsPath),
fs.unlink(this.localFileHashesPath)
fs.rm(this.previousLocalTreePath, {
force: true,
maxRetries: 60 * 10,
recursive: true,
retryDelay: 100
}),
fs.rm(this.previousLocalINodesPath, {
force: true,
maxRetries: 60 * 10,
recursive: true,
retryDelay: 100
}),
fs.rm(this.previousRemoteTreePath, {
force: true,
maxRetries: 60 * 10,
recursive: true,
retryDelay: 100
}),
fs.rm(this.previousRemoteUUIDsPath, {
force: true,
maxRetries: 60 * 10,
recursive: true,
retryDelay: 100
}),
fs.rm(this.localFileHashesPath, {
force: true,
maxRetries: 60 * 10,
recursive: true,
retryDelay: 100
})
])
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/lib/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,17 @@ export class Sync {
}

public async cleanup(): Promise<void> {
await this.localFileSystem.stopDirectoryWatcher()
try {
await Promise.all([this.localFileSystem.stopDirectoryWatcher(), this.deleteLocalSyncDbFiles()])

this.isInitialized = false
this.worker.logger.log("info", "Cleanup done", this.syncPair.localPath)
} catch (e) {
this.worker.logger.log("error", e, "sync.cleanup")
this.worker.logger.log("error", e)
}

this.worker.logger.log("info", "Cleanup done", this.syncPair.localPath)
this.isInitialized = false
this.removed = true

postMessageToMain({
type: "cycleExited",
Expand All @@ -212,7 +218,6 @@ export class Sync {

private async run(): Promise<void> {
if (this.removed) {
await this.deleteLocalSyncDbFiles()
await this.cleanup()

return
Expand Down

0 comments on commit 2042440

Please sign in to comment.