feat: add task cancellation feature (task-management)

- Backend: Add API endpoint to cancel tasks and mark status as cancelled
- Frontend: Add cancel button to task management UI (visible only for cancellable tasks)
- Process: Implement cancellation check during generation to abort in-progress tasks
- UI: Update status display styles with a visual indicator for the cancelled state
This commit is contained in:
p
2026-01-17 18:47:54 +08:00
parent 0b47770683
commit de6fe4b294
3 changed files with 66 additions and 2 deletions
+23
View File
@@ -164,6 +164,11 @@ def generate_storyboard_task(task_id: str, project_id: str, user_input: str):
story_blocks = [b for b in json_blocks if b.get("type") not in ["character_sheet", "comic_config"]]
# --- Missing Character Check & Fix ---
session.refresh(task)
if task.status == "cancelled":
log_task_event(session, task_id, "Task execution cancelled by user.")
return
story_char_names = set()
for block in story_blocks:
chars = block.get("characters", [])
@@ -303,6 +308,12 @@ def generate_all_images_task(task_id: str, project_id: str):
total_chars = len(project.characters)
log_task_event(session, task_id, f"Generating {total_chars} characters...")
for i, char in enumerate(project.characters):
# Check for cancellation
session.refresh(task)
if task.status == "cancelled":
log_task_event(session, task_id, "Task execution cancelled by user.")
return
if char.image_url:
log_task_event(session, task_id, f"Character {char.name} already has image, skipping.")
continue
@@ -353,6 +364,12 @@ def generate_all_images_task(task_id: str, project_id: str):
# For "one click", let's assume we scan all items.
for i, item in enumerate(items):
# Check for cancellation
session.refresh(task)
if task.status == "cancelled":
log_task_event(session, task_id, "Task execution cancelled by user.")
return
# Update progress at start of loop
# task.progress = int((i / total_items) * 100)
# session.add(task)
@@ -479,6 +496,12 @@ def generate_all_characters_task(task_id: str, project_id: str):
log_task_event(session, task_id, f"Generating {total_chars} characters...")
for i, char in enumerate(project.characters):
# Check for cancellation
session.refresh(task)
if task.status == "cancelled":
log_task_event(session, task_id, "Task execution cancelled by user.")
return
# if char.image_url:
# logger.info(f"Character {char.name} already has image, skipping.")
# continue
+16
View File
@@ -21,3 +21,19 @@ def get_project_tasks(project_id: str, session: Session = Depends(get_session)):
# Filter only recent or active tasks if list is too long?
# For now return all, maybe limit 20
return tasks[:20]
@router.post("/{task_id}/cancel", response_model=TaskRead)
def cancel_task(task_id: str, session: Session = Depends(get_session)):
task = session.get(Task, task_id)
if not task:
raise HTTPException(status_code=404, detail="Task not found")
if task.status in ["completed", "failed", "cancelled"]:
return task
task.status = "cancelled"
task.message = "Task cancelled by user"
session.add(task)
session.commit()
session.refresh(task)
return task