Add ListSchedulerEntries API endpoint

This commit is contained in:
Ken Hibino
2020-12-02 07:19:06 -08:00
parent 3dd6fdc0b0
commit fbbc414bdf
5 changed files with 88 additions and 5 deletions

View File

@@ -6,6 +6,12 @@ import (
"github.com/hibiken/asynq"
)
// ****************************************************************************
// This file defines:
// - internal types with JSON struct tags
// - conversion function from an external type to an internal type
// ****************************************************************************
type QueueStateSnapshot struct {
// Name of the queue.
Queue string `json:"queue"`
@@ -206,3 +212,37 @@ func toDeadTasks(in []*asynq.DeadTask) []*DeadTask {
}
return out
}
type SchedulerEntry struct {
ID string `json:"id"`
Spec string `json:"spec"`
TaskType string `json:"task_type"`
TaskPayload asynq.Payload `json:"task_payload"`
Opts []string `json:"options"`
NextEnqueueAt time.Time `json:"next_enqueue_at"`
PrevEnqueueAt time.Time `json:"prev_enqueue_at"`
}
func toSchedulerEntry(e *asynq.SchedulerEntry) *SchedulerEntry {
opts := make([]string, 0) // create a non-nil, empty slice to avoid null in json output
for _, o := range e.Opts {
opts = append(opts, o.String())
}
return &SchedulerEntry{
ID: e.ID,
Spec: e.Spec,
TaskType: e.Task.Type,
TaskPayload: e.Task.Payload,
Opts: opts,
NextEnqueueAt: e.Next,
PrevEnqueueAt: e.Prev,
}
}
func toSchedulerEntries(in []*asynq.SchedulerEntry) []*SchedulerEntry {
out := make([]*SchedulerEntry, len(in))
for i, e := range in {
out[i] = toSchedulerEntry(e)
}
return out
}