feat(system): add cursor pagination for runtime queue tasks

Replace offset-based page numbers with opaque Redis-backed cursors so
scheduled/retry tasks sort by next run time and live lists stay stable
when queue state changes between requests.
This commit is contained in:
wizardchen
2026-07-14 16:48:01 +08:00
committed by lyingbug
parent f583d77fa0
commit 01f1011511
17 changed files with 620 additions and 114 deletions
+3 -2
View File
@@ -73,8 +73,9 @@ type RuntimeTaskInspector interface {
ctx context.Context,
queue string,
state types.RuntimeTaskState,
page, pageSize int,
) (tasks []types.RuntimeTaskInfo, supported bool, err error)
cursor string,
pageSize int,
) (page types.RuntimeTaskPage, supported bool, err error)
GetRuntimeTask(ctx context.Context, queue, taskID string) (task *types.RuntimeTaskInfo, supported bool, err error)
RunRuntimeTask(ctx context.Context, queue, taskID string) (supported bool, err error)
DeleteRuntimeTask(ctx context.Context, queue, taskID string) (supported bool, err error)
+22 -1
View File
@@ -1,6 +1,19 @@
package types
import "time"
import (
"errors"
"time"
)
var (
// ErrInvalidRuntimeTaskCursor means the client supplied a cursor that was
// malformed or belonged to another queue/state view.
ErrInvalidRuntimeTaskCursor = errors.New("invalid runtime task cursor")
// ErrExpiredRuntimeTaskCursor means every task anchor retained in the
// cursor has left the selected live state. The caller should refresh from
// the first page rather than falling back to offset pagination.
ErrExpiredRuntimeTaskCursor = errors.New("expired runtime task cursor")
)
// RuntimeTaskState is the stable operator-facing task lifecycle. It mirrors
// the durable states exposed by asynq while keeping the HTTP API independent
@@ -69,6 +82,14 @@ type RuntimeTaskInfo struct {
KnowledgeCount int `json:"knowledge_count,omitempty"`
}
// RuntimeTaskPage is one stable slice of the operator task list. NextCursor
// is opaque to HTTP clients and remains tied to the selected queue/state.
type RuntimeTaskPage struct {
Tasks []RuntimeTaskInfo
NextCursor string
HasMore bool
}
func (t RuntimeTaskInfo) Allows(action RuntimeTaskAction) bool {
for _, allowed := range t.AllowedActions {
if allowed == action {