Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(ingestion): settle a terminal task's open run before deletion (#21798)

CCLUCKYe7d77288de73
diff --git a/internal/service/ingestion_task_service.go b/internal/service/ingestion_task_service.go
index fcc2f698aa..014deefd1a 100644
--- a/internal/service/ingestion_task_service.go
+++ b/internal/service/ingestion_task_service.go
@@ -457,15 +457,17 @@ func (s *IngestionTaskService) Remove(ctx context.Context, taskID string, userID
}
}
}
- // A queued task becomes STOPPED before its run is settled. If settlement
- // failed, a retry must finish it before the task row is deleted.
- if task.Status == common.STOPPED && task.PipelineLogID != nil && *task.PipelineLogID != "" {
+ // A terminal task normally closes its own run, but that terminal write is
+ // best-effort: when it was lost the ledger stays open with no worker left to
+ // settle it. Settlement is idempotent, so finish it before deleting the row
+ // — the same rule PrepareDocumentTaskForCleanup applies to document cleanup.
+ if runStatus, message, ok := terminalRunSettlement(task.Status); ok && task.PipelineLogID != nil && *task.PipelineLogID != "" {
run, runErr := s.pipelineLogDAO.GetByID(ctx, dao.DB, *task.PipelineLogID)
if runErr != nil && !errors.Is(runErr, gorm.ErrRecordNotFound) {
return nil, runErr
}
if run != nil && run.RunCount != nil && *run.RunCount > 0 {
- if err := s.settleTaskRun(ctx, task, entity.TaskStatusCancel, "Task superseded by a new parse request."); err != nil {
+ if err := s.settleTaskRun(ctx, task, runStatus, message); err != nil {
return nil, err
}
}
@@ -494,6 +496,21 @@ func (s *IngestionTaskService) Remove(ctx context.Context, taskID string, userID
return info, nil
}
+// terminalRunSettlement maps a terminal task to the run status and terminal
+// message its ledger should carry.
+func terminalRunSettlement(taskStatus string) (entity.TaskStatus, string, bool) {
+ switch taskStatus {
+ case common.STOPPED:
+ return entity.TaskStatusCancel, "Task superseded by a new parse request.", true
+ case common.COMPLETED:
+ return entity.TaskStatusDone, taskTerminalMessageComplete, true
+ case common.FAILED:
+ return entity.TaskStatusFail, taskTerminalMessageFailed, true
+ default:
+ return "", "", false
+ }
+}
+
func (s *IngestionTaskService) GetTask(ctx context.Context, taskID string) (*entity.IngestionTask, error) {
if dao.DB == nil {
// Every task-status write funnels through here (MarkFailed/Stopped/
diff --git a/internal/service/ingestion_task_service_test.go b/internal/service/ingestion_task_service_test.go
index f80485b465..349e9e7876 100644
--- a/internal/service/ingestion_task_service_test.go
+++ b/internal/service/ingestion_task_service_test.go
@@ -1152,6 +1152,65 @@ func TestIngestionTaskServiceRemoveRetriesStoppedRunSettlement(t *testing.T) {
}
}
+func TestIngestionTaskServiceRemoveSettlesTerminalTaskRun(t *testing.T) {
+ cases := []struct {
+ name string
+ taskStatus string
+ runStatus entity.TaskStatus
+ terminalText string
+ }{
+ {"failed", common.FAILED, entity.TaskStatusFail, taskTerminalMessageFailed},
+ {"completed", common.COMPLETED, entity.TaskStatusDone, taskTerminalMessageComplete},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ db := setupServiceTestDB(t)
+ pushServiceDB(t, db)
+ insertTestKB(t, "kb-1", "tenant-1", 1, 0, 0)
+ insertTestDoc(t, "doc-1", "kb-1", 0, 0)
+ insertTestIngestionTaskWithStatus(t, "task-1", "user-1", "doc-1", "kb-1", tc.taskStatus)
+
+ runCount := 1
+ if err := db.Create(&entity.PipelineOperationLog{
+ ID: "terminal-open-log",
+ DocumentID: "doc-1",
+ TenantID: "tenant-1",
+ KbID: "kb-1",
+ ParserID: "naive",
+ TaskType: string(entity.PipelineTaskTypeParse),
+ OperationStatus: string(entity.TaskStatusUnstart),
+ RunCount: &runCount,
+ }).Error; err != nil {
+ t.Fatalf("seed numbered pipeline log: %v", err)
+ }
+ if err := db.Model(&entity.IngestionTask{}).Where("id = ?", "task-1").
+ Update("pipeline_log_id", "terminal-open-log").Error; err != nil {
+ t.Fatalf("bind numbered pipeline log: %v", err)
+ }
+
+ if _, err := NewIngestionTaskService().Remove(t.Context(), "task-1", sptr("user-1")); err != nil {
+ t.Fatalf("Remove failed: %v", err)
+ }
+
+ var run entity.PipelineOperationLog
+ if err := db.First(&run, "id = ?", "terminal-open-log").Error; err != nil {
+ t.Fatalf("numbered run was deleted: %v", err)
+ }
+ if run.OperationStatus != string(tc.runStatus) {
+ t.Fatalf("numbered run status = %q, want %q", run.OperationStatus, tc.runStatus)
+ }
+ var terminal entity.IngestionTaskLog
+ if err := db.Where("pipeline_log_id = ? AND event_type = ?", "terminal-open-log", dao.EventTypeTerminal).
+ First(&terminal).Error; err != nil {
+ t.Fatalf("terminal event missing: %v", err)
+ }
+ if terminal.Message != tc.terminalText {
+ t.Fatalf("terminal message = %q, want %q", terminal.Message, tc.terminalText)
+ }
+ })
+ }
+}
+
func TestIngestionTaskServicePrepareDocumentTaskForCleanupRecordsCleanupReason(t *testing.T) {
db := setupServiceTestDB(t)
pushServiceDB(t, db)