2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-27 00:02:19 +08:00

Fix ListSchedulerEnqueueEvents to list recent events first

This commit is contained in:
Ken Hibino 2020-12-26 14:35:25 -08:00
parent 38509e309f
commit 196d66f221
3 changed files with 22 additions and 23 deletions

View File

@ -855,7 +855,7 @@ func (r *RDB) ListSchedulerEntries() ([]*base.SchedulerEntry, error) {
// ListSchedulerEnqueueEvents returns the list of scheduler enqueue events. // ListSchedulerEnqueueEvents returns the list of scheduler enqueue events.
func (r *RDB) ListSchedulerEnqueueEvents(entryID string, pgn Pagination) ([]*base.SchedulerEnqueueEvent, error) { func (r *RDB) ListSchedulerEnqueueEvents(entryID string, pgn Pagination) ([]*base.SchedulerEnqueueEvent, error) {
key := base.SchedulerHistoryKey(entryID) key := base.SchedulerHistoryKey(entryID)
zs, err := r.client.ZRangeWithScores(key, pgn.start(), pgn.stop()).Result() zs, err := r.client.ZRevRangeWithScores(key, pgn.start(), pgn.stop()).Result()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -3035,6 +3035,7 @@ func TestSchedulerEnqueueEvents(t *testing.T) {
var ( var (
now = time.Now() now = time.Now()
oneDayAgo = now.Add(-24 * time.Hour) oneDayAgo = now.Add(-24 * time.Hour)
fiveHoursAgo = now.Add(-5 * time.Hour)
oneHourAgo = now.Add(-1 * time.Hour) oneHourAgo = now.Add(-1 * time.Hour)
) )
@ -3047,22 +3048,26 @@ func TestSchedulerEnqueueEvents(t *testing.T) {
tests := []struct { tests := []struct {
entryID string entryID string
events []*base.SchedulerEnqueueEvent events []*base.SchedulerEnqueueEvent
want []*base.SchedulerEnqueueEvent
}{ }{
{ {
entryID: "entry123", entryID: "entry123",
events: []*base.SchedulerEnqueueEvent{ events: []*base.SchedulerEnqueueEvent{
{ {TaskID: "task123", EnqueuedAt: oneDayAgo},
TaskID: "task123", {TaskID: "task789", EnqueuedAt: oneHourAgo},
EnqueuedAt: oneDayAgo, {TaskID: "task456", EnqueuedAt: fiveHoursAgo},
}, {
TaskID: "task456",
EnqueuedAt: oneHourAgo,
}, },
// Recent events first
want: []*base.SchedulerEnqueueEvent{
{TaskID: "task789", EnqueuedAt: oneHourAgo},
{TaskID: "task456", EnqueuedAt: fiveHoursAgo},
{TaskID: "task123", EnqueuedAt: oneDayAgo},
}, },
}, },
{ {
entryID: "entry123", entryID: "entry456",
events: []*base.SchedulerEnqueueEvent{}, events: nil,
want: nil,
}, },
} }
@ -3076,14 +3081,14 @@ loop:
continue loop continue loop
} }
} }
got, err := r.ListSchedulerEnqueueEvents(tc.entryID) got, err := r.ListSchedulerEnqueueEvents(tc.entryID, Pagination{Size: 20, Page: 0})
if err != nil { if err != nil {
t.Errorf("ListSchedulerEnqueueEvents(%q) failed: %v", tc.entryID, err) t.Errorf("ListSchedulerEnqueueEvents(%q) failed: %v", tc.entryID, err)
continue continue
} }
if diff := cmp.Diff(tc.events, got, h.SortSchedulerEnqueueEventOpt, timeCmpOpt); diff != "" { if diff := cmp.Diff(tc.want, got, timeCmpOpt); diff != "" {
t.Errorf("ListSchedulerEnqueueEvent(%q) = %v, want %v; (-want,+got)\n%s", t.Errorf("ListSchedulerEnqueueEvent(%q) = %v, want %v; (-want,+got)\n%s",
tc.entryID, got, tc.events, diff) tc.entryID, got, tc.want, diff)
} }
} }
} }

View File

@ -118,12 +118,6 @@ func cronHistory(cmd *cobra.Command, args []string) {
continue continue
} }
// Sort entries by enqueuedAt timestamp.
sort.Slice(events, func(i, j int) bool {
x, y := events[i], events[j]
return x.EnqueuedAt.Unix() > y.EnqueuedAt.Unix()
})
cols := []string{"TaskID", "EnqueuedAt"} cols := []string{"TaskID", "EnqueuedAt"}
printRows := func(w io.Writer, tmpl string) { printRows := func(w io.Writer, tmpl string) {
for _, e := range events { for _, e := range events {