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:
@@ -2,6 +2,7 @@ package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -11,12 +12,13 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// NewAsynqInspector constructs an *asynq.Inspector pointed at the same
|
||||
// Redis used by the asynq client. Only registered in asynq mode.
|
||||
func NewAsynqInspector() *asynq.Inspector {
|
||||
return asynq.NewInspector(getAsynqRedisClientOpt())
|
||||
func NewAsynqInspector(redisClient *redis.Client) *asynq.Inspector {
|
||||
return asynq.NewInspectorFromRedisClient(redisClient)
|
||||
}
|
||||
|
||||
// asynqTaskInspector implements interfaces.TaskInspector backed by an
|
||||
@@ -27,16 +29,17 @@ func NewAsynqInspector() *asynq.Inspector {
|
||||
// flaky.
|
||||
type asynqTaskInspector struct {
|
||||
inspector *asynq.Inspector
|
||||
redis redis.UniversalClient
|
||||
}
|
||||
|
||||
// NewAsynqTaskInspector returns a TaskInspector wrapping the given
|
||||
// *asynq.Inspector. nil-safe: a nil inspector degrades to a no-op so
|
||||
// the cancel path remains usable when the inspector failed to init.
|
||||
func NewAsynqTaskInspector(inspector *asynq.Inspector) interfaces.TaskInspector {
|
||||
if inspector == nil {
|
||||
func NewAsynqTaskInspector(inspector *asynq.Inspector, redisClient *redis.Client) interfaces.TaskInspector {
|
||||
if inspector == nil || redisClient == nil {
|
||||
return noopTaskInspector{}
|
||||
}
|
||||
return &asynqTaskInspector{inspector: inspector}
|
||||
return &asynqTaskInspector{inspector: inspector, redis: redisClient}
|
||||
}
|
||||
|
||||
// knowledgeIDProbe is the minimal payload shape we need to filter
|
||||
@@ -346,23 +349,176 @@ func (a *asynqTaskInspector) activeWorkerMetadata() map[string]runtimeWorkerMeta
|
||||
return result
|
||||
}
|
||||
|
||||
// ListRuntimeTasks returns one task state page. Only allow-listed routing
|
||||
// metadata is projected from the payload so the dashboard never exposes raw
|
||||
// document content, signed URLs, or connector secrets.
|
||||
const (
|
||||
runtimeTaskCursorVersion = 1
|
||||
runtimeTaskCursorMaxAnchors = 32
|
||||
runtimeTaskCursorMaxBytes = 16 * 1024
|
||||
)
|
||||
|
||||
type runtimeTaskCursor struct {
|
||||
Version int `json:"v"`
|
||||
Queue string `json:"q"`
|
||||
State types.RuntimeTaskState `json:"s"`
|
||||
Anchors []string `json:"a"`
|
||||
}
|
||||
|
||||
type runtimeTaskStorageOrder int
|
||||
|
||||
const (
|
||||
runtimeTaskListNewestFirst runtimeTaskStorageOrder = iota
|
||||
runtimeTaskZSetEarliestFirst
|
||||
runtimeTaskZSetNewestFirst
|
||||
)
|
||||
|
||||
func runtimeTaskStorage(state types.RuntimeTaskState) (suffix string, order runtimeTaskStorageOrder) {
|
||||
switch state {
|
||||
case types.RuntimeTaskPending, types.RuntimeTaskActive:
|
||||
// Asynq LPUSHes newly enqueued/started tasks, so index zero is the
|
||||
// newest task in these live-state lists.
|
||||
return string(state), runtimeTaskListNewestFirst
|
||||
case types.RuntimeTaskScheduled, types.RuntimeTaskRetry:
|
||||
// Sorted-set scores are NextProcessAt. Earliest-first keeps the next
|
||||
// operational action visible instead of hiding it below later work.
|
||||
return string(state), runtimeTaskZSetEarliestFirst
|
||||
case types.RuntimeTaskArchived, types.RuntimeTaskCompleted:
|
||||
// Archived scores are LastFailedAt; completed scores are expiry time.
|
||||
// Reverse score order presents the newest failures and the newest
|
||||
// retained completion records first.
|
||||
return string(state), runtimeTaskZSetNewestFirst
|
||||
default:
|
||||
return "", runtimeTaskListNewestFirst
|
||||
}
|
||||
}
|
||||
|
||||
func runtimeTaskStateKey(queue string, state types.RuntimeTaskState) (string, runtimeTaskStorageOrder) {
|
||||
suffix, order := runtimeTaskStorage(state)
|
||||
// Asynq's public Inspector only supports offset pages and does not expose
|
||||
// sort direction or continuation cursors. Keep the v0.26 queue-key schema
|
||||
// isolated here and covered by cursor-order integration tests.
|
||||
return "asynq:{" + queue + "}:" + suffix, order
|
||||
}
|
||||
|
||||
func decodeRuntimeTaskCursor(raw, queue string, state types.RuntimeTaskState) ([]string, error) {
|
||||
if raw == "" {
|
||||
return nil, nil
|
||||
}
|
||||
if len(raw) > runtimeTaskCursorMaxBytes {
|
||||
return nil, types.ErrInvalidRuntimeTaskCursor
|
||||
}
|
||||
data, err := base64.RawURLEncoding.DecodeString(raw)
|
||||
if err != nil {
|
||||
return nil, types.ErrInvalidRuntimeTaskCursor
|
||||
}
|
||||
var cursor runtimeTaskCursor
|
||||
if err = json.Unmarshal(data, &cursor); err != nil ||
|
||||
cursor.Version != runtimeTaskCursorVersion || cursor.Queue != queue || cursor.State != state ||
|
||||
len(cursor.Anchors) == 0 || len(cursor.Anchors) > runtimeTaskCursorMaxAnchors {
|
||||
return nil, types.ErrInvalidRuntimeTaskCursor
|
||||
}
|
||||
for _, anchor := range cursor.Anchors {
|
||||
if anchor == "" {
|
||||
return nil, types.ErrInvalidRuntimeTaskCursor
|
||||
}
|
||||
}
|
||||
return cursor.Anchors, nil
|
||||
}
|
||||
|
||||
func encodeRuntimeTaskCursor(queue string, state types.RuntimeTaskState, anchors []string) (string, error) {
|
||||
if len(anchors) > runtimeTaskCursorMaxAnchors {
|
||||
anchors = anchors[len(anchors)-runtimeTaskCursorMaxAnchors:]
|
||||
}
|
||||
data, err := json.Marshal(runtimeTaskCursor{
|
||||
Version: runtimeTaskCursorVersion,
|
||||
Queue: queue,
|
||||
State: state,
|
||||
Anchors: anchors,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(data), nil
|
||||
}
|
||||
|
||||
// runtimeTaskAnchorOffset finds the newest surviving anchor. Retaining a
|
||||
// small window of anchors lets pagination continue when the last item from a
|
||||
// previous live-state page finishes, retries, or is deleted between requests.
|
||||
func (a *asynqTaskInspector) runtimeTaskAnchorOffset(
|
||||
ctx context.Context,
|
||||
key string,
|
||||
order runtimeTaskStorageOrder,
|
||||
anchors []string,
|
||||
) (int64, error) {
|
||||
if len(anchors) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
pipe := a.redis.Pipeline()
|
||||
ranks := make([]*redis.IntCmd, 0, len(anchors))
|
||||
for i := len(anchors) - 1; i >= 0; i-- {
|
||||
switch order {
|
||||
case runtimeTaskListNewestFirst:
|
||||
ranks = append(ranks, pipe.LPos(ctx, key, anchors[i], redis.LPosArgs{}))
|
||||
case runtimeTaskZSetEarliestFirst:
|
||||
ranks = append(ranks, pipe.ZRank(ctx, key, anchors[i]))
|
||||
case runtimeTaskZSetNewestFirst:
|
||||
ranks = append(ranks, pipe.ZRevRank(ctx, key, anchors[i]))
|
||||
}
|
||||
}
|
||||
_, err := pipe.Exec(ctx)
|
||||
if err != nil && !errors.Is(err, redis.Nil) {
|
||||
return 0, err
|
||||
}
|
||||
for _, rank := range ranks {
|
||||
if rank.Err() == nil {
|
||||
return rank.Val() + 1, nil
|
||||
}
|
||||
if !errors.Is(rank.Err(), redis.Nil) {
|
||||
return 0, rank.Err()
|
||||
}
|
||||
}
|
||||
return 0, types.ErrExpiredRuntimeTaskCursor
|
||||
}
|
||||
|
||||
func (a *asynqTaskInspector) listRuntimeTaskIDs(
|
||||
ctx context.Context,
|
||||
queue string,
|
||||
state types.RuntimeTaskState,
|
||||
anchors []string,
|
||||
limit int,
|
||||
) ([]string, error) {
|
||||
key, order := runtimeTaskStateKey(queue, state)
|
||||
start, err := a.runtimeTaskAnchorOffset(ctx, key, order, anchors)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stop := start + int64(limit) - 1
|
||||
switch order {
|
||||
case runtimeTaskListNewestFirst:
|
||||
return a.redis.LRange(ctx, key, start, stop).Result()
|
||||
case runtimeTaskZSetEarliestFirst:
|
||||
return a.redis.ZRange(ctx, key, start, stop).Result()
|
||||
case runtimeTaskZSetNewestFirst:
|
||||
return a.redis.ZRevRange(ctx, key, start, stop).Result()
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported runtime task storage order %d", order)
|
||||
}
|
||||
}
|
||||
|
||||
// ListRuntimeTasks returns one cursor page in state-appropriate time order:
|
||||
// newest first for pending/active/archived/completed, and next-to-run first
|
||||
// for scheduled/retry. Only allow-listed routing metadata is projected from
|
||||
// payloads so the dashboard never exposes document content or secrets.
|
||||
func (a *asynqTaskInspector) ListRuntimeTasks(
|
||||
ctx context.Context,
|
||||
queue string,
|
||||
state types.RuntimeTaskState,
|
||||
page, pageSize int,
|
||||
) ([]types.RuntimeTaskInfo, bool, error) {
|
||||
if a == nil || a.inspector == nil {
|
||||
return nil, false, nil
|
||||
cursor string,
|
||||
pageSize int,
|
||||
) (types.RuntimeTaskPage, bool, error) {
|
||||
if a == nil || a.inspector == nil || a.redis == nil {
|
||||
return types.RuntimeTaskPage{}, false, nil
|
||||
}
|
||||
if !state.Valid() {
|
||||
return nil, true, fmt.Errorf("unsupported runtime task state %q", state)
|
||||
}
|
||||
if page < 1 {
|
||||
page = 1
|
||||
return types.RuntimeTaskPage{}, true, fmt.Errorf("unsupported runtime task state %q", state)
|
||||
}
|
||||
if pageSize < 1 {
|
||||
pageSize = 20
|
||||
@@ -370,46 +526,76 @@ func (a *asynqTaskInspector) ListRuntimeTasks(
|
||||
if pageSize > 100 {
|
||||
pageSize = 100
|
||||
}
|
||||
opts := []asynq.ListOption{asynq.Page(page), asynq.PageSize(pageSize)}
|
||||
var tasks []*asynq.TaskInfo
|
||||
var err error
|
||||
switch state {
|
||||
case types.RuntimeTaskPending:
|
||||
tasks, err = a.inspector.ListPendingTasks(queue, opts...)
|
||||
case types.RuntimeTaskActive:
|
||||
tasks, err = a.inspector.ListActiveTasks(queue, opts...)
|
||||
case types.RuntimeTaskScheduled:
|
||||
tasks, err = a.inspector.ListScheduledTasks(queue, opts...)
|
||||
case types.RuntimeTaskRetry:
|
||||
tasks, err = a.inspector.ListRetryTasks(queue, opts...)
|
||||
case types.RuntimeTaskArchived:
|
||||
tasks, err = a.inspector.ListArchivedTasks(queue, opts...)
|
||||
case types.RuntimeTaskCompleted:
|
||||
tasks, err = a.inspector.ListCompletedTasks(queue, opts...)
|
||||
}
|
||||
if errors.Is(err, asynq.ErrQueueNotFound) {
|
||||
return []types.RuntimeTaskInfo{}, true, nil
|
||||
}
|
||||
anchors, err := decodeRuntimeTaskCursor(cursor, queue, state)
|
||||
if err != nil {
|
||||
return nil, true, err
|
||||
return types.RuntimeTaskPage{}, true, err
|
||||
}
|
||||
workers := map[string]runtimeWorkerMetadata{}
|
||||
if state == types.RuntimeTaskActive {
|
||||
workers = a.activeWorkerMetadata()
|
||||
}
|
||||
result := make([]types.RuntimeTaskInfo, 0, len(tasks))
|
||||
for _, task := range tasks {
|
||||
if task == nil {
|
||||
continue
|
||||
result := make([]types.RuntimeTaskInfo, 0, pageSize)
|
||||
hasMore := false
|
||||
|
||||
for len(result) < pageSize {
|
||||
// The extra ID proves there is another raw item without consuming it
|
||||
// into this page's continuation cursor.
|
||||
batchLimit := pageSize - len(result) + 1
|
||||
ids, listErr := a.listRuntimeTaskIDs(ctx, queue, state, anchors, batchLimit)
|
||||
if listErr != nil {
|
||||
if errors.Is(listErr, types.ErrExpiredRuntimeTaskCursor) && cursor == "" {
|
||||
return types.RuntimeTaskPage{Tasks: result}, true, nil
|
||||
}
|
||||
return types.RuntimeTaskPage{}, true, listErr
|
||||
}
|
||||
info, projectErr := projectRuntimeTask(task, workers[task.Queue+"\x00"+task.ID])
|
||||
if projectErr != nil {
|
||||
logger.Warnf(ctx, "[TaskInspector] project runtime task queue=%s id=%s: %v", queue, task.ID, projectErr)
|
||||
continue
|
||||
if len(ids) == 0 {
|
||||
break
|
||||
}
|
||||
for i, id := range ids {
|
||||
if len(result) == pageSize {
|
||||
hasMore = true
|
||||
break
|
||||
}
|
||||
anchors = append(anchors, id)
|
||||
if len(anchors) > runtimeTaskCursorMaxAnchors {
|
||||
anchors = anchors[len(anchors)-runtimeTaskCursorMaxAnchors:]
|
||||
}
|
||||
task, getErr := a.inspector.GetTaskInfo(queue, id)
|
||||
if errors.Is(getErr, asynq.ErrTaskNotFound) || errors.Is(getErr, asynq.ErrQueueNotFound) {
|
||||
continue
|
||||
}
|
||||
if getErr != nil {
|
||||
return types.RuntimeTaskPage{}, true, getErr
|
||||
}
|
||||
info, projectErr := projectRuntimeTask(task, workers[task.Queue+"\x00"+task.ID])
|
||||
if projectErr != nil {
|
||||
logger.Warnf(ctx, "[TaskInspector] project runtime task queue=%s id=%s: %v", queue, task.ID, projectErr)
|
||||
continue
|
||||
}
|
||||
if info.State != state {
|
||||
continue
|
||||
}
|
||||
result = append(result, info)
|
||||
if len(result) == pageSize {
|
||||
// A full raw batch may have contained stale IDs that were
|
||||
// skipped above. In that case the last returned item can also be
|
||||
// the last fetched ID even though older Redis entries still exist.
|
||||
hasMore = i < len(ids)-1 || len(ids) == batchLimit
|
||||
}
|
||||
}
|
||||
if hasMore || len(ids) < batchLimit {
|
||||
break
|
||||
}
|
||||
result = append(result, info)
|
||||
}
|
||||
return result, true, nil
|
||||
|
||||
page := types.RuntimeTaskPage{Tasks: result, HasMore: hasMore}
|
||||
if hasMore {
|
||||
page.NextCursor, err = encodeRuntimeTaskCursor(queue, state, anchors)
|
||||
if err != nil {
|
||||
return types.RuntimeTaskPage{}, true, err
|
||||
}
|
||||
}
|
||||
return page, true, nil
|
||||
}
|
||||
|
||||
func (a *asynqTaskInspector) GetRuntimeTask(
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func TestProjectRuntimeTaskRedactsPayloadAndBuildsSafeActions(t *testing.T) {
|
||||
@@ -94,3 +97,163 @@ func TestProjectRuntimeTaskUsesAllowListedBatchMetadata(t *testing.T) {
|
||||
t.Fatalf("generic maintenance task must not expose raw deletion: %v", info.AllowedActions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeTaskCursorUsesStateTimeOrderAndSurvivingAnchors(t *testing.T) {
|
||||
server := miniredis.RunT(t)
|
||||
client := redis.NewClient(&redis.Options{Addr: server.Addr()})
|
||||
t.Cleanup(func() { _ = client.Close() })
|
||||
inspector := &asynqTaskInspector{redis: client}
|
||||
ctx := context.Background()
|
||||
|
||||
pendingKey, _ := runtimeTaskStateKey(types.QueueDefault, types.RuntimeTaskPending)
|
||||
if err := client.LPush(ctx, pendingKey, "old", "middle", "new").Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ids, err := inspector.listRuntimeTaskIDs(ctx, types.QueueDefault, types.RuntimeTaskPending, nil, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, want := ids, []string{"new", "middle"}; !equalStrings(got, want) {
|
||||
t.Fatalf("pending order = %v, want %v", got, want)
|
||||
}
|
||||
if err = client.LRem(ctx, pendingKey, 0, "middle").Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ids, err = inspector.listRuntimeTaskIDs(
|
||||
ctx, types.QueueDefault, types.RuntimeTaskPending, []string{"new", "middle"}, 2,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, want := ids, []string{"old"}; !equalStrings(got, want) {
|
||||
t.Fatalf("anchor fallback order = %v, want %v", got, want)
|
||||
}
|
||||
|
||||
scheduledKey, _ := runtimeTaskStateKey(types.QueueDefault, types.RuntimeTaskScheduled)
|
||||
if err = client.ZAdd(ctx, scheduledKey,
|
||||
redis.Z{Score: 30, Member: "later"},
|
||||
redis.Z{Score: 10, Member: "next"},
|
||||
redis.Z{Score: 20, Member: "after-next"},
|
||||
).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ids, err = inspector.listRuntimeTaskIDs(ctx, types.QueueDefault, types.RuntimeTaskScheduled, nil, 3)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, want := ids, []string{"next", "after-next", "later"}; !equalStrings(got, want) {
|
||||
t.Fatalf("scheduled order = %v, want %v", got, want)
|
||||
}
|
||||
|
||||
archivedKey, _ := runtimeTaskStateKey(types.QueueDefault, types.RuntimeTaskArchived)
|
||||
if err = client.ZAdd(ctx, archivedKey,
|
||||
redis.Z{Score: 10, Member: "old-failure"},
|
||||
redis.Z{Score: 30, Member: "new-failure"},
|
||||
redis.Z{Score: 20, Member: "middle-failure"},
|
||||
).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ids, err = inspector.listRuntimeTaskIDs(ctx, types.QueueDefault, types.RuntimeTaskArchived, nil, 3)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, want := ids, []string{"new-failure", "middle-failure", "old-failure"}; !equalStrings(got, want) {
|
||||
t.Fatalf("archived order = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeTaskCursorIsBoundToQueueAndState(t *testing.T) {
|
||||
raw, err := encodeRuntimeTaskCursor(
|
||||
types.QueueDefault,
|
||||
types.RuntimeTaskArchived,
|
||||
[]string{"task-1", "task-2"},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
anchors, err := decodeRuntimeTaskCursor(raw, types.QueueDefault, types.RuntimeTaskArchived)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !equalStrings(anchors, []string{"task-1", "task-2"}) {
|
||||
t.Fatalf("anchors = %v", anchors)
|
||||
}
|
||||
if _, err = decodeRuntimeTaskCursor(raw, types.QueueDefault, types.RuntimeTaskRetry); err == nil {
|
||||
t.Fatal("cursor from another state should be rejected")
|
||||
}
|
||||
if _, err = decodeRuntimeTaskCursor("not-base64", types.QueueDefault, types.RuntimeTaskArchived); err == nil {
|
||||
t.Fatal("malformed cursor should be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListRuntimeTasksPaginatesNewestPendingTasksWithoutOverlap(t *testing.T) {
|
||||
server := miniredis.RunT(t)
|
||||
client := redis.NewClient(&redis.Options{Addr: server.Addr()})
|
||||
t.Cleanup(func() { _ = client.Close() })
|
||||
asynqClient := asynq.NewClientFromRedisClient(client)
|
||||
inspector := &asynqTaskInspector{
|
||||
inspector: asynq.NewInspectorFromRedisClient(client),
|
||||
redis: client,
|
||||
}
|
||||
|
||||
for _, id := range []string{"old", "middle", "new"} {
|
||||
_, err := asynqClient.Enqueue(
|
||||
asynq.NewTask(types.TypeDocumentProcess, []byte(`{"tenant_id":1,"knowledge_id":"knowledge-1"}`)),
|
||||
asynq.Queue(types.QueueDefault),
|
||||
asynq.TaskID(id),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("enqueue %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
pendingKey, _ := runtimeTaskStateKey(types.QueueDefault, types.RuntimeTaskPending)
|
||||
if err := client.LInsert(context.Background(), pendingKey, "AFTER", "new", "already-gone").Err(); err != nil {
|
||||
t.Fatalf("insert stale task id: %v", err)
|
||||
}
|
||||
|
||||
first, supported, err := inspector.ListRuntimeTasks(
|
||||
context.Background(), types.QueueDefault, types.RuntimeTaskPending, "", 2,
|
||||
)
|
||||
if err != nil || !supported {
|
||||
t.Fatalf("first page: supported=%v err=%v", supported, err)
|
||||
}
|
||||
if got, want := runtimeTaskIDs(first.Tasks), []string{"new", "middle"}; !equalStrings(got, want) {
|
||||
t.Fatalf("first page = %v, want %v", got, want)
|
||||
}
|
||||
if !first.HasMore || first.NextCursor == "" {
|
||||
t.Fatalf("first page cursor missing: %+v", first)
|
||||
}
|
||||
|
||||
second, supported, err := inspector.ListRuntimeTasks(
|
||||
context.Background(), types.QueueDefault, types.RuntimeTaskPending, first.NextCursor, 2,
|
||||
)
|
||||
if err != nil || !supported {
|
||||
t.Fatalf("second page: supported=%v err=%v", supported, err)
|
||||
}
|
||||
if got, want := runtimeTaskIDs(second.Tasks), []string{"old"}; !equalStrings(got, want) {
|
||||
t.Fatalf("second page = %v, want %v", got, want)
|
||||
}
|
||||
if second.HasMore || second.NextCursor != "" {
|
||||
t.Fatalf("unexpected continuation after final page: %+v", second)
|
||||
}
|
||||
}
|
||||
|
||||
func runtimeTaskIDs(tasks []types.RuntimeTaskInfo) []string {
|
||||
ids := make([]string, 0, len(tasks))
|
||||
for _, task := range tasks {
|
||||
ids = append(ids, task.ID)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func equalStrings(left, right []string) bool {
|
||||
if len(left) != len(right) {
|
||||
return false
|
||||
}
|
||||
for i := range left {
|
||||
if left[i] != right[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user