From 0d0d396f487206da2170436dd80e9353208562da Mon Sep 17 00:00:00 2001 From: Hao Wang Date: Wed, 2 Dec 2026 14:45:40 +0800 Subject: [PATCH] fix(task): find unfinished tasks through the status index (#8529) HasUnfinishedSyncTasks, GetAllUnFinishSyncTasks and GetTimedOutUnfinishedTasks exclude the two terminal statuses with "status != ?" or "NOT IN". PostgreSQL and SQLite cannot use idx_tasks_status for that filter, so every check reads all finished tasks. The async_task_poll scheduler runs the existence check every 15 seconds. On a PostgreSQL table with 1.4 million finished tasks, one check took about 27 seconds, and the database kept scanning the table even when no task was unfinished. Write the filter as the key ranges around FAILURE and SUCCESS. It matches the same rows, and all three databases answer it from the status index. Closes #8528 --- model/task.go | 17 +++++++++++++---- model/task_cas_test.go | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/model/task.go b/model/task.go index 3132b40fb1..51b03c16e8 100644 --- a/model/task.go +++ b/model/task.go @@ -13,6 +13,7 @@ import ( "github.com/QuantumNous/new-api/pkg/jsplugin" commonRelay "github.com/QuantumNous/new-api/relay/common" "github.com/QuantumNous/new-api/relaykit/dto" + "gorm.io/gorm" ) type TaskStatus string @@ -399,9 +400,18 @@ func TaskGetAllTasks(startIdx int, num int, queryParams SyncTaskQueryParams) []* return tasks } +// unfinishedTasks keeps tasks whose status is neither FAILURE nor SUCCESS. +// It writes the condition as the ranges around the two terminal statuses +// (FAILURE sorts before SUCCESS), so the database reads only unfinished rows +// from the status index instead of scanning every finished task. +func unfinishedTasks(tx *gorm.DB) *gorm.DB { + return tx.Where("status < ? OR (status > ? AND status < ?) OR status > ?", + TaskStatusFailure, TaskStatusFailure, TaskStatusSuccess, TaskStatusSuccess) +} + func GetTimedOutUnfinishedTasks(cutoffUnix int64, limit int) []*Task { var tasks []*Task - err := DB.Where("status NOT IN ?", []string{TaskStatusFailure, TaskStatusSuccess}). + err := DB.Scopes(unfinishedTasks). Where("submit_time < ?", cutoffUnix). Order("submit_time"). Limit(limit). @@ -416,7 +426,7 @@ func GetAllUnFinishSyncTasks(limit int) []*Task { var tasks []*Task var err error // Upstream progress can reach 100% before the task enters a terminal state. - err = DB.Where("status != ?", TaskStatusFailure).Where("status != ?", TaskStatusSuccess).Limit(limit).Order("id").Find(&tasks).Error + err = DB.Scopes(unfinishedTasks).Limit(limit).Order("id").Find(&tasks).Error if err != nil { return nil } @@ -430,8 +440,7 @@ func GetAllUnFinishSyncTasks(limit int) []*Task { func HasUnfinishedSyncTasks() bool { var id int64 err := DB.Model(&Task{}). - Where("status != ?", TaskStatusFailure). - Where("status != ?", TaskStatusSuccess). + Scopes(unfinishedTasks). Limit(1). Pluck("id", &id).Error return err == nil && id != 0 diff --git a/model/task_cas_test.go b/model/task_cas_test.go index 0a09ddcf3b..12cb343c5e 100644 --- a/model/task_cas_test.go +++ b/model/task_cas_test.go @@ -149,7 +149,7 @@ func TestUnfinishedTaskQueriesDatabaseMatrix(t *testing.T) { assert.False(t, HasUnfinishedSyncTasks()) const cutoff int64 = 1_800_000_000 - for _, status := range []TaskStatus{TaskStatusNotStart, TaskStatusSubmitted, TaskStatusQueued, TaskStatusInProgress, TaskStatusSuccess, TaskStatusFailure} { + for _, status := range []TaskStatus{"", TaskStatusNotStart, TaskStatusSubmitted, TaskStatusQueued, TaskStatusInProgress, TaskStatusUnknown, TaskStatusSuccess, TaskStatusFailure} { for _, progress := range []string{"99%", "100%"} { t.Run(string(status)+"/"+progress, func(t *testing.T) { task := &Task{TaskID: "query_candidate", Status: status, Progress: progress, SubmitTime: cutoff - 1}