Skip to content

Commit

Permalink
fix: runOnce state saving
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Nov 12, 2024
1 parent d5623d3 commit 086179d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 35 deletions.
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.77",
"version": "0.1.78",
"description": "Filen Sync",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ export class SyncWorker {
this.syncs[syncUUID]!.removed = removed

if (removed) {
await this.syncs[syncUUID]!.cleanup()
await this.syncs[syncUUID]!.cleanup({
deleteLocalDbFiles: true
})

this.syncs[syncUUID]!.localFileSystem.ignoredCache.clear()
this.syncs[syncUUID]!.remoteFileSystem.ignoredCache.clear()
Expand Down
2 changes: 2 additions & 0 deletions src/lib/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ export class State {

const tmpDestination = pathModule.join(pathModule.dirname(destination), `${uuidv4()}.tmp`)

console.log({ destination, size: Object.keys(record).length, tmpDestination })

try {
// eslint-disable-next-line no-async-promise-executor
await new Promise<void>(async (resolve, reject) => {
Expand Down
50 changes: 17 additions & 33 deletions src/lib/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class Sync {
public excludeDotFiles: boolean
public readonly worker: SyncWorker
public removed: boolean = false
public saveStateOnNoChanges = true
public readonly lock: Lock
public taskErrors: TaskError[] = []
public localTrashDisabled: boolean
Expand Down Expand Up @@ -201,9 +200,12 @@ export class Sync {
}
}

public async cleanup(): Promise<void> {
public async cleanup({ deleteLocalDbFiles = false }: { deleteLocalDbFiles?: boolean }): Promise<void> {
try {
await Promise.all([this.localFileSystem.stopDirectoryWatcher(), this.deleteLocalSyncDbFiles()])
await Promise.all([
this.localFileSystem.stopDirectoryWatcher(),
deleteLocalDbFiles ? this.deleteLocalSyncDbFiles() : Promise.resolve()
])

this.worker.logger.log("info", "Cleanup done", this.syncPair.localPath)
} catch (e) {
Expand All @@ -226,15 +228,19 @@ export class Sync {

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

return
}

try {
if (this.taskErrors.length > 0 || this.localTreeErrors.length > 0) {
if (this.worker.runOnce) {
await this.cleanup()
await this.cleanup({
deleteLocalDbFiles: false
})

return
}
Expand Down Expand Up @@ -281,7 +287,9 @@ export class Sync {

if (this.paused) {
if (this.worker.runOnce) {
await this.cleanup()
await this.cleanup({
deleteLocalDbFiles: false
})

return
}
Expand Down Expand Up @@ -382,31 +390,6 @@ export class Sync {
})

if (!currentLocalTree.changed && !currentRemoteTree.changed) {
/*if (this.taskErrors.length === 0) {
postMessageToMain({
type: "cycleSavingStateStarted",
syncPair: this.syncPair
})
this.previousLocalTree = currentLocalTree.result
this.previousRemoteTree = currentRemoteTree.result
// We only save the state once if there are no changes.
// This helps reducing the cpu and disk footprint when there are continuously no changes.
if (this.saveStateOnNoChanges) {
this.saveStateOnNoChanges = false
await this.state.save()
}
runGC()
postMessageToMain({
type: "cycleSavingStateDone",
syncPair: this.syncPair
})
}*/

postMessageToMain({
type: "cycleSuccess",
syncPair: this.syncPair
Expand Down Expand Up @@ -557,7 +540,6 @@ export class Sync {

this.previousLocalTree = currentLocalTree.result
this.previousRemoteTree = currentRemoteTree.result
this.saveStateOnNoChanges = true

await this.state.save()

Expand Down Expand Up @@ -601,7 +583,9 @@ export class Sync {
}
} finally {
if (this.worker.runOnce || this.removed) {
await this.cleanup()
await this.cleanup({
deleteLocalDbFiles: this.removed
})
} else {
postMessageToMain({
type: "cycleRestarting",
Expand Down

0 comments on commit 086179d

Please sign in to comment.