fix(container): correct resetPendingTasks SQL on startup
Split knowledge list/update queries to avoid GORM UPDATE...FROM duplicate-table errors after Find, and use sync_logs started_at/ finished_at column names instead of start_time/end_time.
This commit is contained in:
@@ -635,110 +635,6 @@ func syncSequences(db *gorm.DB) {
|
||||
}
|
||||
}
|
||||
|
||||
// resetPendingTasks resets the state of any knowledge items or sync logs stuck in processing
|
||||
// due to an unexpected application restart.
|
||||
//
|
||||
// In Lite mode (no REDIS_ADDR) every queued task lived in process memory, so
|
||||
// any "processing" row at startup is definitively orphaned and must be marked
|
||||
// failed wholesale. In distributed mode (Asynq on Redis) the active queue
|
||||
// survives restart, but the *currently executing* task on the dead instance
|
||||
// is lost — Asynq won't reschedule it until at-least-once retry kicks in,
|
||||
// which can take minutes or never (e.g. if the deadline has passed). To bound
|
||||
// the worst case we mark only "long-stale" rows failed: anything that hasn't
|
||||
// been touched for 30 minutes is well past any reasonable in-flight window.
|
||||
// Newer rows are left alone so we don't race a peer instance that's mid-process.
|
||||
func resetPendingTasks(db *gorm.DB) {
|
||||
distributed := os.Getenv("REDIS_ADDR") != ""
|
||||
ctx := context.Background()
|
||||
spanRepo := repository.NewKnowledgeSpanRepository(db)
|
||||
|
||||
knowledgeQuery := db.Model(&types.Knowledge{}).
|
||||
Where("parse_status IN ?", []string{
|
||||
types.ParseStatusPending,
|
||||
types.ParseStatusProcessing,
|
||||
types.ParseStatusFinalizing,
|
||||
types.ParseStatusDeleting,
|
||||
})
|
||||
summaryQuery := db.Model(&types.Knowledge{}).
|
||||
Where("summary_status IN ?", []string{types.SummaryStatusPending, types.SummaryStatusProcessing})
|
||||
syncQuery := db.Model(&types.SyncLog{}).
|
||||
Where("status IN ?", []string{types.SyncLogStatusRunning, "pending"})
|
||||
|
||||
if distributed {
|
||||
// 30-minute stale threshold — comfortably longer than any individual
|
||||
// stage's expected duration (DocReader 30m, embedding seconds, etc.)
|
||||
// while short enough that operators don't wait hours after a crash.
|
||||
staleCutoff := time.Now().Add(-30 * time.Minute)
|
||||
knowledgeQuery = knowledgeQuery.Where("updated_at < ?", staleCutoff)
|
||||
summaryQuery = summaryQuery.Where("updated_at < ?", staleCutoff)
|
||||
syncQuery = syncQuery.Where("start_time < ?", staleCutoff)
|
||||
}
|
||||
|
||||
// Cancel orphaned trace spans for knowledge rows we are about to mark
|
||||
// failed. resetPendingTasks does not touch asynq queues; this only
|
||||
// prevents the UI from showing duplicate running postprocess.*
|
||||
// subspans when a later retry also opens fresh spans.
|
||||
var stuckKnowledge []types.Knowledge
|
||||
if err := knowledgeQuery.Select("id").Find(&stuckKnowledge).Error; err != nil {
|
||||
logger.Warnf(ctx, "resetPendingTasks: list stuck knowledge failed: %v", err)
|
||||
} else {
|
||||
for _, k := range stuckKnowledge {
|
||||
attempt, err := spanRepo.LatestAttempt(ctx, k.ID)
|
||||
if err != nil || attempt <= 0 {
|
||||
continue
|
||||
}
|
||||
if n, err := spanRepo.CancelAllOpenSpans(ctx, k.ID, attempt,
|
||||
"SERVER_RESTART", "task interrupted due to application restart"); err != nil {
|
||||
logger.Warnf(ctx, "resetPendingTasks: cancel spans for %s failed: %v", k.ID, err)
|
||||
} else if n > 0 {
|
||||
logger.Infof(ctx, "resetPendingTasks: cancelled %d open span(s) for knowledge %s attempt %d",
|
||||
n, k.ID, attempt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 1. Reset knowledge parsing tasks (including finalizing rows whose
|
||||
// enrichment subtasks were lost with the process).
|
||||
result := knowledgeQuery.Updates(map[string]interface{}{
|
||||
"parse_status": types.ParseStatusFailed,
|
||||
"error_message": "Task interrupted due to application restart",
|
||||
"pending_subtasks_count": 0,
|
||||
})
|
||||
if result.Error != nil {
|
||||
logger.Warnf(context.Background(), "Failed to reset pending knowledge tasks: %v", result.Error)
|
||||
} else if result.RowsAffected > 0 {
|
||||
logger.Infof(context.Background(),
|
||||
"Reset %d stuck knowledge parsing tasks to failed state (distributed=%v)",
|
||||
result.RowsAffected, distributed)
|
||||
}
|
||||
|
||||
// 2. Reset knowledge summary tasks
|
||||
resultSummary := summaryQuery.Updates(map[string]interface{}{
|
||||
"summary_status": types.SummaryStatusFailed,
|
||||
})
|
||||
if resultSummary.Error != nil {
|
||||
logger.Warnf(context.Background(), "Failed to reset pending summary tasks: %v", resultSummary.Error)
|
||||
} else if resultSummary.RowsAffected > 0 {
|
||||
logger.Infof(context.Background(),
|
||||
"Reset %d stuck summary generation tasks to failed state (distributed=%v)",
|
||||
resultSummary.RowsAffected, distributed)
|
||||
}
|
||||
|
||||
// 3. Reset data source sync tasks
|
||||
resultSync := syncQuery.Updates(map[string]interface{}{
|
||||
"status": types.SyncLogStatusFailed,
|
||||
"error_message": "Sync interrupted due to application restart",
|
||||
"end_time": time.Now(),
|
||||
})
|
||||
if resultSync.Error != nil {
|
||||
logger.Warnf(context.Background(), "Failed to reset pending data source sync tasks: %v", resultSync.Error)
|
||||
} else if resultSync.RowsAffected > 0 {
|
||||
logger.Infof(context.Background(),
|
||||
"Reset %d stuck data source sync tasks to failed state (distributed=%v)",
|
||||
resultSync.RowsAffected, distributed)
|
||||
}
|
||||
}
|
||||
|
||||
// initFileService initializes file storage service
|
||||
// Creates the appropriate file storage service based on configuration
|
||||
// Supports multiple storage backends (MinIO, COS, local filesystem)
|
||||
|
||||
Reference in New Issue
Block a user