2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-26 07:42:17 +08:00

Update Inspector.ListArchivedTasks

This commit is contained in:
Ken Hibino 2021-05-18 21:06:53 -07:00
parent cd351d49b9
commit e63d51da0c
2 changed files with 18 additions and 34 deletions

View File

@ -455,7 +455,7 @@ func (i *Inspector) ListRetryTasks(qname string, opts ...ListOption) ([]*TaskInf
// Tasks are sorted by LastFailedAt in descending order.
//
// By default, it retrieves the first 30 tasks.
func (i *Inspector) ListArchivedTasks(qname string, opts ...ListOption) ([]*ArchivedTask, error) {
func (i *Inspector) ListArchivedTasks(qname string, opts ...ListOption) ([]*TaskInfo, error) {
if err := base.ValidateQueueName(qname); err != nil {
return nil, fmt.Errorf("asynq: %v", err)
}
@ -466,20 +466,11 @@ func (i *Inspector) ListArchivedTasks(qname string, opts ...ListOption) ([]*Arch
// TODO: handle ErrQueueNotFound
return nil, fmt.Errorf("asynq: %v", err)
}
var tasks []*ArchivedTask
var tasks []*TaskInfo
for _, z := range zs {
failedAt := time.Unix(z.Score, 0)
t := NewTask(z.Message.Type, z.Message.Payload)
tasks = append(tasks, &ArchivedTask{
// TODO: How to handle last failed at
Task: t,
ID: z.Message.ID.String(),
Queue: z.Message.Queue,
MaxRetry: z.Message.Retry,
Retried: z.Message.Retried,
LastFailedAt: failedAt,
LastError: z.Message.ErrorMsg,
score: z.Score,
tasks = append(tasks, &TaskInfo{
msg: z.Message,
state: base.TaskStateArchived,
})
}
return tasks, nil

View File

@ -687,17 +687,11 @@ func TestInspectorListRetryTasks(t *testing.T) {
}
}
func createArchivedTask(z base.Z) *ArchivedTask {
msg := z.Message
return &ArchivedTask{
Task: NewTask(msg.Type, msg.Payload),
ID: msg.ID.String(),
Queue: msg.Queue,
MaxRetry: msg.Retry,
Retried: msg.Retried,
LastFailedAt: time.Unix(z.Score, 0),
LastError: msg.ErrorMsg,
score: z.Score,
func createArchivedTask(z base.Z) *TaskInfo {
return &TaskInfo{
msg: z.Message,
state: base.TaskStateArchived,
nextProcessAt: time.Time{},
}
}
@ -720,7 +714,7 @@ func TestInspectorListArchivedTasks(t *testing.T) {
desc string
archived map[string][]base.Z
qname string
want []*ArchivedTask
want []*TaskInfo
}{
{
desc: "with a few archived tasks",
@ -730,7 +724,7 @@ func TestInspectorListArchivedTasks(t *testing.T) {
},
qname: "default",
// Should be sorted by LastFailedAt.
want: []*ArchivedTask{
want: []*TaskInfo{
createArchivedTask(z2),
createArchivedTask(z1),
createArchivedTask(z3),
@ -742,7 +736,7 @@ func TestInspectorListArchivedTasks(t *testing.T) {
"default": {},
},
qname: "default",
want: []*ArchivedTask(nil),
want: []*TaskInfo(nil),
},
}
@ -755,8 +749,7 @@ func TestInspectorListArchivedTasks(t *testing.T) {
t.Errorf("%s; ListArchivedTasks(%q) returned error: %v", tc.desc, tc.qname, err)
continue
}
ignoreOpt := cmpopts.IgnoreUnexported(Task{}, ArchivedTask{})
if diff := cmp.Diff(tc.want, got, ignoreOpt); diff != "" {
if diff := cmp.Diff(tc.want, got, cmp.AllowUnexported(TaskInfo{})); diff != "" {
t.Errorf("%s; ListArchivedTask(%q) = %v, want %v; (-want,+got)\n%s",
tc.desc, tc.qname, got, tc.want, diff)
}
@ -1983,7 +1976,7 @@ func TestInspectorDeleteTaskDeletesArchivedTask(t *testing.T) {
"custom": {z3},
},
qname: "default",
id: createArchivedTask(z2).ID,
id: createArchivedTask(z2).ID(),
wantArchived: map[string][]base.Z{
"default": {z1},
"custom": {z3},
@ -2034,7 +2027,7 @@ func TestInspectorDeleteTaskError(t *testing.T) {
"custom": {z3},
},
qname: "nonexistent",
id: createArchivedTask(z2).ID,
id: createArchivedTask(z2).ID(),
wantErr: ErrQueueNotFound,
wantArchived: map[string][]base.Z{
"default": {z1, z2},
@ -2245,7 +2238,7 @@ func TestInspectorRunTaskRunsArchivedTask(t *testing.T) {
"low": {},
},
qname: "critical",
id: createArchivedTask(z2).ID,
id: createArchivedTask(z2).ID(),
wantArchived: map[string][]base.Z{
"default": {z1},
"critical": {},
@ -2319,7 +2312,7 @@ func TestInspectorRunTaskError(t *testing.T) {
"low": {},
},
qname: "nonexistent",
id: createArchivedTask(z2).ID,
id: createArchivedTask(z2).ID(),
wantErr: ErrQueueNotFound,
wantArchived: map[string][]base.Z{
"default": {z1},