Skip to content

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

feat(files): expose task cancellation capability

johaven14e5b9e81d77
diff --git a/backend/src/applications/files/models/file-task.ts b/backend/src/applications/files/models/file-task.ts
index d38ae6c0..97314df4 100644
--- a/backend/src/applications/files/models/file-task.ts
+++ b/backend/src/applications/files/models/file-task.ts
@@ -21,6 +21,7 @@ export interface FileTaskProps {
export class FileTask {
id: string
type: FILE_OPERATION
+ cancellable: boolean
status: FileTaskStatus
path: string
name: string
@@ -29,9 +30,10 @@ export class FileTask {
startedAt: number
endedAt: number
- constructor(id: string, type: FILE_OPERATION, path: string, name: string) {
+ constructor(id: string, type: FILE_OPERATION, path: string, name: string, cancellable = false) {
this.id = id
this.type = type
+ this.cancellable = cancellable
this.path = path
this.name = name
if (type === FILE_OPERATION.COPY || type === FILE_OPERATION.MOVE || type === FILE_OPERATION.DOWNLOAD) {
diff --git a/backend/src/applications/files/services/files-tasks-manager.service.spec.ts b/backend/src/applications/files/services/files-tasks-manager.service.spec.ts
index f9854927..09b2e9fb 100644
--- a/backend/src/applications/files/services/files-tasks-manager.service.spec.ts
+++ b/backend/src/applications/files/services/files-tasks-manager.service.spec.ts
@@ -112,6 +112,7 @@ describe(FilesTasksManager.name, () => {
cacheStore.set(`${CACHE_TASK_PREFIX}-15-task-1`, {
id: 'task-1',
type: FILE_OPERATION.DOWNLOAD,
+ cancellable: true,
status: FileTaskStatus.PENDING
})
@@ -120,6 +121,19 @@ describe(FilesTasksManager.name, () => {
expect(cache.set).toHaveBeenCalledWith(`${CACHE_TASK_CANCEL_PREFIX}-15-task-1`, true, CACHE_TASK_TTL)
})
+ it('should reject cancellation for an active non-cancellable task', async () => {
+ cacheStore.set(`${CACHE_TASK_PREFIX}-15-task-2`, {
+ id: 'task-2',
+ type: FILE_OPERATION.DELETE,
+ cancellable: false,
+ status: FileTaskStatus.PENDING
+ })
+
+ await expect(filesTasksManager.cancelTask(15, 'task-2')).rejects.toEqual(new HttpException('Not applicable', HttpStatus.BAD_REQUEST))
+
+ expect(cache.set).not.toHaveBeenCalledWith(`${CACHE_TASK_CANCEL_PREFIX}-15-task-2`, true, CACHE_TASK_TTL)
+ })
+
it('should abort a cancellable task and store a cancelled status when cancellation is requested', async () => {
vi.useFakeTimers()
try {
@@ -139,7 +153,8 @@ describe(FilesTasksManager.name, () => {
})
})
- await filesTasksManager.createTask(FILE_OPERATION.DOWNLOAD, user, space, null, 'doWork')
+ const task = await filesTasksManager.createTask(FILE_OPERATION.DOWNLOAD, user, space, null, 'doWork')
+ expect(task.cancellable).toBe(true)
cacheStore.set(cancellationCacheKey, true)
await vi.advanceTimersByTimeAsync(1000)
@@ -264,6 +279,7 @@ describe(FilesTasksManager.name, () => {
await flushPromises()
expect(task.id).toBe('11111111-1111-4111-8111-111111111111')
+ expect(task.cancellable).toBe(false)
expect(task.status).toBe(FileTaskStatus.SUCCESS)
expect(space.task.cacheKey).toBe(`${CACHE_TASK_PREFIX}-7-11111111-1111-4111-8111-111111111111`)
expect(filesMethods.doWork).toHaveBeenCalledWith(user, space, { foo: 'bar' })
@@ -362,6 +378,7 @@ describe(FilesTasksManager.name, () => {
cacheStore.set(`${CACHE_TASK_PREFIX}-8-task-queued`, {
id: 'task-queued',
type: FILE_OPERATION.DOWNLOAD,
+ cancellable: true,
status: FileTaskStatus.QUEUED,
props: {}
})
diff --git a/backend/src/applications/files/services/files-tasks-manager.service.ts b/backend/src/applications/files/services/files-tasks-manager.service.ts
index 9f057e02..a11f9f7f 100644
--- a/backend/src/applications/files/services/files-tasks-manager.service.ts
+++ b/backend/src/applications/files/services/files-tasks-manager.service.ts
@@ -55,7 +55,7 @@ export class FilesTasksManager implements OnModuleDestroy {
async createTask(type: FILE_OPERATION, user: UserModel, space: SpaceEnv, dto: any, method: string): Promise<FileTask> {
const taskId: string = crypto.randomUUID()
const cacheKey = FilesTasksManager.getCacheKey(user.id, taskId)
- const newTask = new FileTask(taskId, type, dirName(space.url), fileName(space.url))
+ const newTask = new FileTask(taskId, type, dirName(space.url), fileName(space.url), this.isCancellableType(type))
await this.storeTask(cacheKey, newTask, FileTaskStatus.QUEUED)
space.task = { cacheKey, props: {} }
await this.filesTasksQueue.enqueue(user.id, { cacheKey, dto, method, space, task: newTask, user }, (task) => this.startQueuedTask(task))
@@ -116,7 +116,7 @@ export class FilesTasksManager implements OnModuleDestroy {
if (!task) {
throw new HttpException('Task not found', HttpStatus.NOT_FOUND)
}
- if (!this.isActiveStatus(task.status) || !this.canCancel(task.type)) {
+ if (!this.isActiveStatus(task.status) || !task.cancellable) {
throw new HttpException('Not applicable', HttpStatus.BAD_REQUEST)
}
if (task.status === FileTaskStatus.QUEUED) {
@@ -285,7 +285,7 @@ export class FilesTasksManager implements OnModuleDestroy {
}
private watchCancellation(type: FILE_OPERATION, userId: number, taskId: string, cacheKey: string): AbortController | undefined {
- if (!this.canCancel(type)) return
+ if (!this.isCancellableType(type)) return
const controller = new AbortController()
const cancellationCacheKey = FilesTasksManager.getCancellationCacheKey(userId, taskId)
const watcher = setInterval(() => void this.abortTaskIfRequested(cacheKey, cancellationCacheKey, controller), this.watchInterval)
@@ -311,7 +311,7 @@ export class FilesTasksManager implements OnModuleDestroy {
delete this.tasksCancellationWatcher[cacheKey]
}
- private canCancel(type: FILE_OPERATION): boolean {
+ private isCancellableType(type: FILE_OPERATION): boolean {
return type === FILE_OPERATION.DOWNLOAD || type === FILE_OPERATION.COMPRESS || type === FILE_OPERATION.DECOMPRESS
}
diff --git a/frontend/src/app/applications/files/services/files-tasks.service.ts b/frontend/src/app/applications/files/services/files-tasks.service.ts
index 1a21c272..d207d27f 100644
--- a/frontend/src/app/applications/files/services/files-tasks.service.ts
+++ b/frontend/src/app/applications/files/services/files-tasks.service.ts
@@ -89,6 +89,7 @@ export class FilesTasksService {
createUploadTask(path: string, name: string, totalSize: number): FileTask {
const task = new FileTask(genRandomUUID(), FILE_OPERATION.UPLOAD, path, name)
task.status = FileTaskStatus.QUEUED
+ task.cancellable = false
task.startedAt = currentTimeStamp(null, true)
task.props = { progress: 1, size: 0, totalSize }
this.store.filesActiveTasks.next(this.prependUniqueTasks(this.store.filesActiveTasks.getValue(), [task]))
@@ -133,21 +134,27 @@ export class FilesTasksService {
canCancel(task: FileTask): boolean {
return (
this.isActiveStatus(task.status) &&
+ task.cancellable &&
!this.cancellingTasks.has(task.id) &&
- (task.type === FILE_OPERATION.DOWNLOAD ||
- task.type === FILE_OPERATION.COMPRESS ||
- task.type === FILE_OPERATION.DECOMPRESS ||
- (task.type === FILE_OPERATION.UPLOAD && this.uploadCancellationHandlers.has(task.id)))
+ (task.type !== FILE_OPERATION.UPLOAD || this.uploadCancellationHandlers.has(task.id))
)
}
registerUploadCancellation(taskId: string, cancel: () => void) {
this.uploadCancellationHandlers.set(taskId, cancel)
+ const task = this.store.filesActiveTasks.getValue().find((activeTask: FileTask) => activeTask.id === taskId)
+ if (task) {
+ task.cancellable = true
+ }
this.store.filesActiveTasks.next([...this.store.filesActiveTasks.getValue()])
}
unregisterUploadCancellation(taskId: string) {
this.uploadCancellationHandlers.delete(taskId)
+ const task = this.store.filesActiveTasks.getValue().find((activeTask: FileTask) => activeTask.id === taskId)
+ if (task) {
+ task.cancellable = false
+ }
}
updateTask(task: FileTask) {