Skip to content

Commit

Permalink
Fix ListSchedulerEnqueueEvents to list recent events first
Browse files Browse the repository at this point in the history
  • Loading branch information
hibiken committed Jan 14, 2021
1 parent 38509e3 commit 196d66f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion internal/rdb/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ func (r *RDB) ListSchedulerEntries() ([]*base.SchedulerEntry, error) {
// ListSchedulerEnqueueEvents returns the list of scheduler enqueue events.
func (r *RDB) ListSchedulerEnqueueEvents(entryID string, pgn Pagination) ([]*base.SchedulerEnqueueEvent, error) {
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 {
return nil, err
}
Expand Down
37 changes: 21 additions & 16 deletions internal/rdb/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3033,9 +3033,10 @@ func TestSchedulerEnqueueEvents(t *testing.T) {
r := setup(t)

var (
now = time.Now()
oneDayAgo = now.Add(-24 * time.Hour)
oneHourAgo = now.Add(-1 * time.Hour)
now = time.Now()
oneDayAgo = now.Add(-24 * time.Hour)
fiveHoursAgo = now.Add(-5 * time.Hour)
oneHourAgo = now.Add(-1 * time.Hour)
)

type event struct {
Expand All @@ -3047,22 +3048,26 @@ func TestSchedulerEnqueueEvents(t *testing.T) {
tests := []struct {
entryID string
events []*base.SchedulerEnqueueEvent
want []*base.SchedulerEnqueueEvent
}{
{
entryID: "entry123",
events: []*base.SchedulerEnqueueEvent{
{
TaskID: "task123",
EnqueuedAt: oneDayAgo,
}, {
TaskID: "task456",
EnqueuedAt: oneHourAgo,
},
events: []*base.SchedulerEnqueueEvent{
{TaskID: "task123", EnqueuedAt: oneDayAgo},
{TaskID: "task789", EnqueuedAt: oneHourAgo},
{TaskID: "task456", EnqueuedAt: fiveHoursAgo},
},
// Recent events first
want: []*base.SchedulerEnqueueEvent{
{TaskID: "task789", EnqueuedAt: oneHourAgo},
{TaskID: "task456", EnqueuedAt: fiveHoursAgo},
{TaskID: "task123", EnqueuedAt: oneDayAgo},
},
},
{
entryID: "entry123",
events: []*base.SchedulerEnqueueEvent{},
entryID: "entry456",
events: nil,
want: nil,
},
}

Expand All @@ -3076,14 +3081,14 @@ loop:
continue loop
}
}
got, err := r.ListSchedulerEnqueueEvents(tc.entryID)
got, err := r.ListSchedulerEnqueueEvents(tc.entryID, Pagination{Size: 20, Page: 0})
if err != nil {
t.Errorf("ListSchedulerEnqueueEvents(%q) failed: %v", tc.entryID, err)
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",
tc.entryID, got, tc.events, diff)
tc.entryID, got, tc.want, diff)
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions tools/asynq/cmd/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,6 @@ func cronHistory(cmd *cobra.Command, args []string) {
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"}
printRows := func(w io.Writer, tmpl string) {
for _, e := range events {
Expand Down

0 comments on commit 196d66f

Please sign in to comment.