mirror of
https://github.com/hibiken/asynq.git
synced 2024-12-26 15:52:18 +08:00
Fix ListSchedulerEnqueueEvents to list recent events first
This commit is contained in:
parent
38509e309f
commit
196d66f221
@ -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
|
||||
}
|
||||
|
@ -3035,6 +3035,7 @@ func TestSchedulerEnqueueEvents(t *testing.T) {
|
||||
var (
|
||||
now = time.Now()
|
||||
oneDayAgo = now.Add(-24 * time.Hour)
|
||||
fiveHoursAgo = now.Add(-5 * time.Hour)
|
||||
oneHourAgo = now.Add(-1 * time.Hour)
|
||||
)
|
||||
|
||||
@ -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,
|
||||
{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,
|
||||
},
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user