Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

refactor(backend:files): centralize `isActiveTaskStatus` utility and remove redundant implementations

johavenff994c4f304e
diff --git a/backend/src/applications/files/services/files-scheduler.service.ts b/backend/src/applications/files/services/files-scheduler.service.ts
index 64873beb..75f3c806 100644
--- a/backend/src/applications/files/services/files-scheduler.service.ts
+++ b/backend/src/applications/files/services/files-scheduler.service.ts
@@ -12,12 +12,12 @@ import { getTablesWithFileIdColumn } from '../../../infrastructure/database/util
import { USER_PATH, USER_ROLE } from '../../users/constants/user'
import { UserModel } from '../../users/models/user.model'
import { users } from '../../users/schemas/users.schema'
-import { CACHE_TASK_CANCEL_PREFIX, CACHE_TASK_PREFIX, CACHE_TASK_USER_PREFIX } from '../constants/cache'
+import { CACHE_TASK_CANCEL_PREFIX, CACHE_TASK_PREFIX, CACHE_TASK_TTL, CACHE_TASK_USER_PREFIX } from '../constants/cache'
import { FileTask, FileTaskStatus } from '../models/file-task'
import { filesRecents } from '../schemas/files-recents.schema'
import { files } from '../schemas/files.schema'
import { isPathExists, removeFiles } from '../utils/files'
-import { taskTemporaryPrefix } from '../utils/tasks'
+import { isActiveTaskStatus, taskTemporaryPrefix } from '../utils/tasks'
import { FilesContentIndexer } from './files-content-indexer.service'
import { FilesTasksManager } from './tasks/files-tasks-manager.service'
import { FilesQuotaManager } from './files-quota-manager.service'
@@ -220,7 +220,8 @@ export class FilesScheduler {
const protectedFiles = new Set<string>()
const protectedPrefixes: string[] = []
for (const task of tasks) {
- if (!task || !this.isActiveTaskStatus(task.status)) continue
+ if (!task || !isActiveTaskStatus(task.status)) continue
+ // Active task staging files must survive tmp cleanup; the transfer may still publish or rollback them.
// QUEUED tasks are included because they may start after this cache snapshot.
protectedPrefixes.push(taskTemporaryPrefix(FilesTasksManager.getCacheKey(userId, task.id)))
// Exported archives are final results stored beside staging entries and do not use the task prefix.
@@ -270,25 +271,29 @@ export class FilesScheduler {
let nb = 0
let nbCancellationRequests = 0
let nbUserTaskCounters = 0
+ // The in-memory queue and abort watchers are lost on process restart; cached active tasks cannot be resumed safely.
const keys = await this.cache.keys(`${CACHE_TASK_PREFIX}-*`)
for (const key of keys) {
if (key.startsWith(`${CACHE_TASK_CANCEL_PREFIX}-`)) {
+ // Cancellation requests only target live abort watchers, so they are stale after startup.
await this.cache.del(key)
nbCancellationRequests++
continue
}
if (key.startsWith(`${CACHE_TASK_USER_PREFIX}-`)) {
+ // Running counters are runtime state; keeping them would block new tasks from claiming slots.
await this.cache.del(key)
nbUserTaskCounters++
continue
}
const task = await this.cache.get(key)
- if (task && this.isActiveTaskStatus(task.status)) {
+ if (task && isActiveTaskStatus(task.status)) {
+ // Do not requeue filesystem operations here: they are not guaranteed to be idempotent.
task.status = FileTaskStatus.ERROR
task.result = 'Interrupted'
task.endedAt = currentTimeStamp(null, true)
nb++
- this.cache.set(key, task).catch((e: Error) => this.logger.error({ tag: this.cleanupInterruptedTasks.name, msg: `${e}` }))
+ this.cache.set(key, task, CACHE_TASK_TTL).catch((e: Error) => this.logger.error({ tag: this.cleanupInterruptedTasks.name, msg: `${e}` }))
}
}
this.logger.log({
@@ -300,10 +305,6 @@ export class FilesScheduler {
}
}
- private isActiveTaskStatus(status: FileTaskStatus): boolean {
- return status === FileTaskStatus.PENDING || status === FileTaskStatus.QUEUED
- }
-
private async resetContentIndexingState(): Promise<void> {
await this.filesContentIndexer.resetIndexingRuntimeState()
this.logger.log({ tag: this.resetContentIndexingState.name, msg: `done` })
diff --git a/backend/src/applications/files/services/tasks/files-tasks-watcher.service.ts b/backend/src/applications/files/services/tasks/files-tasks-watcher.service.ts
index 77c82db4..fbb1f388 100644
--- a/backend/src/applications/files/services/tasks/files-tasks-watcher.service.ts
+++ b/backend/src/applications/files/services/tasks/files-tasks-watcher.service.ts
@@ -3,9 +3,9 @@ import { Cache } from '../../../../infrastructure/cache/cache.service'
import { SpaceEnv } from '../../../spaces/models/space-env.model'
import { CACHE_TASK_TTL } from '../../constants/cache'
import { FileTaskEvent } from '../../events/file-events'
-import { FileTask, FileTaskProps, FileTaskStatus } from '../../models/file-task'
+import { FileTask, FileTaskProps } from '../../models/file-task'
import { dirName, fileName, fileSize, isPathIsDir } from '../../utils/files'
-import { countDirEntriesAndSize } from '../../utils/tasks'
+import { countDirEntriesAndSize, isActiveTaskStatus } from '../../utils/tasks'
@Injectable()
export class FilesTasksWatcher implements OnModuleDestroy {
@@ -89,7 +89,7 @@ export class FilesTasksWatcher implements OnModuleDestroy {
this.stopWatch(cacheKey)
return
}
- if (!this.isActiveStatus(fileTask.status) || this.finalizingTasks.has(cacheKey)) {
+ if (!isActiveTaskStatus(fileTask.status) || this.finalizingTasks.has(cacheKey)) {
this.stopWatch(cacheKey)
return
}
@@ -102,8 +102,4 @@ export class FilesTasksWatcher implements OnModuleDestroy {
if (this.pendingTaskUpdates.get(cacheKey) === update) this.pendingTaskUpdates.delete(cacheKey)
})
}
-
- private isActiveStatus(status: FileTaskStatus): boolean {
- return status === FileTaskStatus.PENDING || status === FileTaskStatus.QUEUED
- }
}
diff --git a/backend/src/applications/files/utils/tasks.ts b/backend/src/applications/files/utils/tasks.ts
index d1cc10a4..03013c0d 100644
--- a/backend/src/applications/files/utils/tasks.ts
+++ b/backend/src/applications/files/utils/tasks.ts
@@ -3,7 +3,7 @@ import fs from 'node:fs/promises'
import path from 'node:path'
import { FILE_OPERATION } from '../constants/operations'
import { isCrossDevice, walkDir } from './files'
-import type { FileTaskProps } from '../models/file-task'
+import { FileTaskProps, FileTaskStatus } from '../models/file-task'
export function taskTemporaryPrefix(cacheKey: string): string {
return `.${cacheKey}-`
@@ -40,6 +40,10 @@ export async function isTaskCancellable(type: FILE_OPERATION, srcPath: string, d
}
}
+export function isActiveTaskStatus(status: FileTaskStatus): boolean {
+ return status === FileTaskStatus.PENDING || status === FileTaskStatus.QUEUED
+}
+
export async function countDirEntriesAndSize(rPath: string): Promise<Pick<FileTaskProps, 'files' | 'directories' | 'size'>> {
const entriesCount = { files: 0, directories: 0, size: 0 }
const ignoredErrors: Record<string, string> = {}