diff --git a/task/backend/bolt/bolt.go b/task/backend/bolt/bolt.go index 7c922dac0a..32c1e5a83c 100644 --- a/task/backend/bolt/bolt.go +++ b/task/backend/bolt/bolt.go @@ -15,6 +15,7 @@ package bolt import ( + "bytes" "context" "errors" "fmt" @@ -302,7 +303,17 @@ func (s *Store) ListTasks(ctx context.Context, params backend.TaskSearchParams) if err != nil { return err } - c.Seek(encodedAfter) + + // If the taskID returned by c.Seek is greater than after param, append taskID to taskIDs. + k, _ := c.Seek(encodedAfter) + if bytes.Compare(k, encodedAfter) > 0 { + var nID platform.ID + if err := nID.Decode(k); err != nil { + return err + } + taskIDs = append(taskIDs, nID) + } + for k, _ := c.Next(); k != nil && len(taskIDs) < lim; k, _ = c.Next() { var nID platform.ID if err := nID.Decode(k); err != nil { diff --git a/task/backend/storetest/storetest.go b/task/backend/storetest/storetest.go index ba4d24152e..e7198a5209 100644 --- a/task/backend/storetest/storetest.go +++ b/task/backend/storetest/storetest.go @@ -358,6 +358,30 @@ from(bucket:"test") |> range(start:-1h)` t.Fatalf("exp meta %v, got meta %v", *meta, ts[0].Meta) } + // Test ListTasks with After + ts, err = s.ListTasks(context.Background(), backend.TaskSearchParams{After: id - 1}) + if err != nil { + t.Fatal(err) + } + if len(ts) != 2 { + t.Fatalf("expected 2 result, got %d", len(ts)) + } + if ts[0].Task.ID != id { + t.Fatalf("got task ID %v, exp %v", ts[0].Task.ID, id) + } + + if ts[1].Task.ID != newID { + t.Fatalf("got task ID %v, exp %v", ts[1].Task.ID, newID) + } + + ts, err = s.ListTasks(context.Background(), backend.TaskSearchParams{After: newID}) + if err != nil { + t.Fatal(err) + } + if len(ts) != 0 { + t.Fatalf("expected 0 result, got %d", len(ts)) + } + // should return the last 2 tasks _, err = s.CreateTask(context.Background(), backend.CreateTaskRequest{Org: orgID, AuthorizationID: authzID, Script: fmt.Sprintf(scriptFmt, 3)}) if err != nil {