feat(task-execution): implement task cancellation handling and improve UI feedback for canceling tasks
This commit is contained in:
@@ -22,6 +22,7 @@ export class CloudlyTaskManager {
|
||||
private taskRegistry = new Map<string, plugins.taskbuffer.Task>();
|
||||
private taskInfo = new Map<string, ITaskInfo>();
|
||||
private currentExecutions = new Map<string, TaskExecution>();
|
||||
private cancellationRequests = new Set<string>();
|
||||
|
||||
// Database connection helper
|
||||
get db() {
|
||||
@@ -107,9 +108,14 @@ export class CloudlyTaskManager {
|
||||
// Execute the task
|
||||
const result = await task.trigger();
|
||||
|
||||
// Task completed successfully
|
||||
await execution.complete(result);
|
||||
await execution.addLog(`Task completed successfully`, 'success');
|
||||
// If a cancellation was requested during execution, don't mark as completed
|
||||
if (execution.data.status === 'cancelled' || this.cancellationRequests.has(execution.id)) {
|
||||
await execution.addLog('Task cancelled during execution', 'warning');
|
||||
} else {
|
||||
// Task completed successfully
|
||||
await execution.complete(result);
|
||||
await execution.addLog(`Task completed successfully`, 'success');
|
||||
}
|
||||
|
||||
// Update last run time
|
||||
if (info) {
|
||||
@@ -117,13 +123,19 @@ export class CloudlyTaskManager {
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
// Task failed
|
||||
await execution.fail(error);
|
||||
await execution.addLog(`Task failed: ${error.message}`, 'error');
|
||||
logger.log('error', `Task ${taskName} failed: ${error.message}`);
|
||||
// If already cancelled, don't mark as failed
|
||||
if (execution.data.status === 'cancelled' || this.cancellationRequests.has(execution.id)) {
|
||||
await execution.addLog('Task was cancelled', 'warning');
|
||||
} else {
|
||||
// Task failed
|
||||
await execution.fail(error as any);
|
||||
await execution.addLog(`Task failed: ${(error as any).message}`, 'error');
|
||||
logger.log('error', `Task ${taskName} failed: ${(error as any).message}`);
|
||||
}
|
||||
} finally {
|
||||
// Clean up current execution
|
||||
this.currentExecutions.delete(taskName);
|
||||
this.cancellationRequests.delete(execution.id);
|
||||
}
|
||||
|
||||
return execution;
|
||||
@@ -168,12 +180,19 @@ export class CloudlyTaskManager {
|
||||
|
||||
await execution.cancel();
|
||||
await execution.addLog('Task cancelled by user', 'warning');
|
||||
|
||||
// TODO: Implement actual task cancellation in taskbuffer
|
||||
// mark cancellation request so running task functions can react cooperatively
|
||||
this.cancellationRequests.add(execution.id);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if cancellation is requested for an execution
|
||||
*/
|
||||
public isCancellationRequested(executionId: string): boolean {
|
||||
return this.cancellationRequests.has(executionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered tasks
|
||||
*/
|
||||
@@ -327,4 +346,4 @@ export class CloudlyTaskManager {
|
||||
await this.taskBufferManager.stop();
|
||||
logger.log('info', 'Task Manager stopped');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user