2020-07-13 21:29:41 +08:00
|
|
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2021-01-29 00:59:13 +08:00
|
|
|
package inspeq
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-01-29 00:59:13 +08:00
|
|
|
"github.com/go-redis/redis/v7"
|
2020-07-13 21:29:41 +08:00
|
|
|
"github.com/google/uuid"
|
2021-01-29 00:59:13 +08:00
|
|
|
"github.com/hibiken/asynq"
|
|
|
|
"github.com/hibiken/asynq/internal/base"
|
2020-07-13 21:29:41 +08:00
|
|
|
"github.com/hibiken/asynq/internal/rdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Inspector is a client interface to inspect and mutate the state of
|
|
|
|
// queues and tasks.
|
|
|
|
type Inspector struct {
|
|
|
|
rdb *rdb.RDB
|
|
|
|
}
|
|
|
|
|
2021-01-29 00:59:13 +08:00
|
|
|
// New returns a new instance of Inspector.
|
|
|
|
func New(r asynq.RedisConnOpt) *Inspector {
|
2021-01-29 22:37:35 +08:00
|
|
|
c, ok := r.MakeRedisClient().(redis.UniversalClient)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("inspeq: unsupported RedisConnOpt type %T", r))
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2021-01-29 22:37:35 +08:00
|
|
|
return &Inspector{
|
|
|
|
rdb: rdb.NewRDB(c),
|
2021-01-29 00:59:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 21:52:34 +08:00
|
|
|
// Close closes the connection with redis.
|
|
|
|
func (i *Inspector) Close() error {
|
|
|
|
return i.rdb.Close()
|
|
|
|
}
|
|
|
|
|
2020-08-21 12:17:44 +08:00
|
|
|
// Queues returns a list of all queue names.
|
|
|
|
func (i *Inspector) Queues() ([]string, error) {
|
|
|
|
return i.rdb.AllQueues()
|
|
|
|
}
|
|
|
|
|
2020-08-21 21:00:49 +08:00
|
|
|
// QueueStats represents a state of queues at a certain time.
|
|
|
|
type QueueStats struct {
|
2020-08-17 05:51:56 +08:00
|
|
|
// Name of the queue.
|
|
|
|
Queue string
|
2021-01-27 10:36:45 +08:00
|
|
|
// Total number of bytes that the queue and its tasks require to be stored in redis.
|
|
|
|
MemoryUsage int64
|
2020-08-21 21:00:49 +08:00
|
|
|
// Size is the total number of tasks in the queue.
|
2021-01-13 03:01:21 +08:00
|
|
|
// The value is the sum of Pending, Active, Scheduled, Retry, and Archived.
|
2020-08-21 21:00:49 +08:00
|
|
|
Size int
|
2020-09-05 22:03:43 +08:00
|
|
|
// Number of pending tasks.
|
|
|
|
Pending int
|
2020-09-06 03:43:15 +08:00
|
|
|
// Number of active tasks.
|
|
|
|
Active int
|
2020-08-17 05:51:56 +08:00
|
|
|
// Number of scheduled tasks.
|
|
|
|
Scheduled int
|
|
|
|
// Number of retry tasks.
|
|
|
|
Retry int
|
2021-01-13 03:01:21 +08:00
|
|
|
// Number of archived tasks.
|
|
|
|
Archived int
|
2020-08-17 05:51:56 +08:00
|
|
|
// Total number of tasks being processed during the given date.
|
|
|
|
// The number includes both succeeded and failed tasks.
|
|
|
|
Processed int
|
|
|
|
// Total number of tasks failed to be processed during the given date.
|
|
|
|
Failed int
|
2020-07-13 21:29:41 +08:00
|
|
|
// Paused indicates whether the queue is paused.
|
2020-08-17 05:51:56 +08:00
|
|
|
// If true, tasks in the queue will not be processed.
|
2020-07-13 21:29:41 +08:00
|
|
|
Paused bool
|
2020-08-17 05:51:56 +08:00
|
|
|
// Time when this stats was taken.
|
|
|
|
Timestamp time.Time
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
2020-08-17 05:51:56 +08:00
|
|
|
// CurrentStats returns a current stats of the given queue.
|
2020-08-21 21:00:49 +08:00
|
|
|
func (i *Inspector) CurrentStats(qname string) (*QueueStats, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
stats, err := i.rdb.CurrentStats(qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-21 21:00:49 +08:00
|
|
|
return &QueueStats{
|
2021-01-27 10:36:45 +08:00
|
|
|
Queue: stats.Queue,
|
|
|
|
MemoryUsage: stats.MemoryUsage,
|
|
|
|
Size: stats.Size,
|
|
|
|
Pending: stats.Pending,
|
|
|
|
Active: stats.Active,
|
|
|
|
Scheduled: stats.Scheduled,
|
|
|
|
Retry: stats.Retry,
|
|
|
|
Archived: stats.Archived,
|
|
|
|
Processed: stats.Processed,
|
|
|
|
Failed: stats.Failed,
|
|
|
|
Paused: stats.Paused,
|
|
|
|
Timestamp: stats.Timestamp,
|
2020-08-18 11:49:55 +08:00
|
|
|
}, nil
|
2020-08-17 05:51:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DailyStats holds aggregate data for a given day for a given queue.
|
2020-07-13 21:29:41 +08:00
|
|
|
type DailyStats struct {
|
2020-08-17 05:51:56 +08:00
|
|
|
// Name of the queue.
|
|
|
|
Queue string
|
|
|
|
// Total number of tasks being processed during the given date.
|
|
|
|
// The number includes both succeeded and failed tasks.
|
2020-07-13 21:29:41 +08:00
|
|
|
Processed int
|
2020-08-17 05:51:56 +08:00
|
|
|
// Total number of tasks failed to be processed during the given date.
|
|
|
|
Failed int
|
|
|
|
// Date this stats was taken.
|
|
|
|
Date time.Time
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// History returns a list of stats from the last n days.
|
2020-08-17 05:51:56 +08:00
|
|
|
func (i *Inspector) History(qname string, n int) ([]*DailyStats, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
stats, err := i.rdb.HistoricalStats(qname, n)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var res []*DailyStats
|
|
|
|
for _, s := range stats {
|
|
|
|
res = append(res, &DailyStats{
|
2020-08-17 05:51:56 +08:00
|
|
|
Queue: s.Queue,
|
2020-07-13 21:29:41 +08:00
|
|
|
Processed: s.Processed,
|
|
|
|
Failed: s.Failed,
|
|
|
|
Date: s.Time,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-11-28 14:27:54 +08:00
|
|
|
// ErrQueueNotFound indicates that the specified queue does not exist.
|
|
|
|
type ErrQueueNotFound struct {
|
|
|
|
qname string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrQueueNotFound) Error() string {
|
|
|
|
return fmt.Sprintf("queue %q does not exist", e.qname)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrQueueNotEmpty indicates that the specified queue is not empty.
|
|
|
|
type ErrQueueNotEmpty struct {
|
|
|
|
qname string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrQueueNotEmpty) Error() string {
|
|
|
|
return fmt.Sprintf("queue %q is not empty", e.qname)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteQueue removes the specified queue.
|
|
|
|
//
|
|
|
|
// If force is set to true, DeleteQueue will remove the queue regardless of
|
|
|
|
// the queue size as long as no tasks are active in the queue.
|
|
|
|
// If force is set to false, DeleteQueue will remove the queue only if
|
|
|
|
// the queue is empty.
|
|
|
|
//
|
|
|
|
// If the specified queue does not exist, DeleteQueue returns ErrQueueNotFound.
|
|
|
|
// If force is set to false and the specified queue is not empty, DeleteQueue
|
|
|
|
// returns ErrQueueNotEmpty.
|
|
|
|
func (i *Inspector) DeleteQueue(qname string, force bool) error {
|
|
|
|
err := i.rdb.RemoveQueue(qname, force)
|
|
|
|
if _, ok := err.(*rdb.ErrQueueNotFound); ok {
|
|
|
|
return &ErrQueueNotFound{qname}
|
|
|
|
}
|
|
|
|
if _, ok := err.(*rdb.ErrQueueNotEmpty); ok {
|
|
|
|
return &ErrQueueNotEmpty{qname}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
// PendingTask is a task in a queue and is ready to be processed.
|
|
|
|
type PendingTask struct {
|
2021-01-29 00:59:13 +08:00
|
|
|
*asynq.Task
|
2021-01-27 22:59:32 +08:00
|
|
|
ID string
|
|
|
|
Queue string
|
|
|
|
MaxRetry int
|
|
|
|
Retried int
|
|
|
|
LastError string
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
2020-09-06 03:43:15 +08:00
|
|
|
// ActiveTask is a task that's currently being processed.
|
|
|
|
type ActiveTask struct {
|
2021-01-29 00:59:13 +08:00
|
|
|
*asynq.Task
|
2021-01-27 22:59:32 +08:00
|
|
|
ID string
|
|
|
|
Queue string
|
|
|
|
MaxRetry int
|
|
|
|
Retried int
|
|
|
|
LastError string
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ScheduledTask is a task scheduled to be processed in the future.
|
|
|
|
type ScheduledTask struct {
|
2021-01-29 00:59:13 +08:00
|
|
|
*asynq.Task
|
2020-07-13 21:29:41 +08:00
|
|
|
ID string
|
|
|
|
Queue string
|
2021-01-27 22:59:32 +08:00
|
|
|
MaxRetry int
|
|
|
|
Retried int
|
|
|
|
LastError string
|
2020-09-05 22:11:55 +08:00
|
|
|
NextProcessAt time.Time
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
score int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetryTask is a task scheduled to be retried in the future.
|
|
|
|
type RetryTask struct {
|
2021-01-29 00:59:13 +08:00
|
|
|
*asynq.Task
|
2020-07-13 21:29:41 +08:00
|
|
|
ID string
|
|
|
|
Queue string
|
2020-09-05 22:11:55 +08:00
|
|
|
NextProcessAt time.Time
|
2020-07-13 21:29:41 +08:00
|
|
|
MaxRetry int
|
|
|
|
Retried int
|
2021-01-27 22:59:32 +08:00
|
|
|
LastError string
|
2020-07-13 21:29:41 +08:00
|
|
|
// TODO: LastFailedAt time.Time
|
|
|
|
|
|
|
|
score int64
|
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
// ArchivedTask is a task archived for debugging and inspection purposes, and
|
2021-01-13 03:28:46 +08:00
|
|
|
// it won't be retried automatically.
|
2021-01-13 03:01:21 +08:00
|
|
|
// A task can be archived when the task exhausts its retry counts or manually
|
2021-01-13 03:28:46 +08:00
|
|
|
// archived by a user via the CLI or Inspector.
|
2021-01-13 03:01:21 +08:00
|
|
|
type ArchivedTask struct {
|
2021-01-29 00:59:13 +08:00
|
|
|
*asynq.Task
|
2020-07-13 21:29:41 +08:00
|
|
|
ID string
|
|
|
|
Queue string
|
|
|
|
MaxRetry int
|
|
|
|
Retried int
|
|
|
|
LastFailedAt time.Time
|
2021-01-27 22:59:32 +08:00
|
|
|
LastError string
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
score int64
|
|
|
|
}
|
|
|
|
|
2021-01-21 07:03:34 +08:00
|
|
|
// Format string used for task key.
|
|
|
|
// Format is <prefix>:<uuid>:<score>.
|
|
|
|
const taskKeyFormat = "%s:%v:%v"
|
|
|
|
|
|
|
|
// Prefix used for task key.
|
|
|
|
const (
|
|
|
|
keyPrefixPending = "p"
|
|
|
|
keyPrefixScheduled = "s"
|
|
|
|
keyPrefixRetry = "r"
|
|
|
|
keyPrefixArchived = "a"
|
|
|
|
|
|
|
|
allKeyPrefixes = keyPrefixPending + keyPrefixScheduled + keyPrefixRetry + keyPrefixArchived
|
|
|
|
)
|
|
|
|
|
|
|
|
// Key returns a key used to delete, and archive the pending task.
|
|
|
|
func (t *PendingTask) Key() string {
|
|
|
|
// Note: Pending tasks are stored in redis LIST, therefore no score.
|
|
|
|
// Use zero for the score to use the same key format.
|
|
|
|
return fmt.Sprintf(taskKeyFormat, keyPrefixPending, t.ID, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key returns a key used to delete, run, and archive the scheduled task.
|
2020-07-13 21:29:41 +08:00
|
|
|
func (t *ScheduledTask) Key() string {
|
2021-01-21 07:03:34 +08:00
|
|
|
return fmt.Sprintf(taskKeyFormat, keyPrefixScheduled, t.ID, t.score)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
2021-01-21 07:03:34 +08:00
|
|
|
// Key returns a key used to delete, run, and archive the retry task.
|
2020-07-13 21:29:41 +08:00
|
|
|
func (t *RetryTask) Key() string {
|
2021-01-21 07:03:34 +08:00
|
|
|
return fmt.Sprintf(taskKeyFormat, keyPrefixRetry, t.ID, t.score)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
2021-01-21 07:03:34 +08:00
|
|
|
// Key returns a key used to delete and run the archived task.
|
2021-01-13 03:01:21 +08:00
|
|
|
func (t *ArchivedTask) Key() string {
|
2021-01-21 07:03:34 +08:00
|
|
|
return fmt.Sprintf(taskKeyFormat, keyPrefixArchived, t.ID, t.score)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseTaskKey parses a key string and returns each part of key with proper
|
|
|
|
// type if valid, otherwise it reports an error.
|
2021-01-21 07:03:34 +08:00
|
|
|
func parseTaskKey(key string) (prefix string, id uuid.UUID, score int64, err error) {
|
2020-07-13 21:29:41 +08:00
|
|
|
parts := strings.Split(key, ":")
|
|
|
|
if len(parts) != 3 {
|
2021-01-21 07:03:34 +08:00
|
|
|
return "", uuid.Nil, 0, fmt.Errorf("invalid id")
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
id, err = uuid.Parse(parts[1])
|
|
|
|
if err != nil {
|
2021-01-21 07:03:34 +08:00
|
|
|
return "", uuid.Nil, 0, fmt.Errorf("invalid id")
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
score, err = strconv.ParseInt(parts[2], 10, 64)
|
|
|
|
if err != nil {
|
2021-01-21 07:03:34 +08:00
|
|
|
return "", uuid.Nil, 0, fmt.Errorf("invalid id")
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2021-01-21 07:03:34 +08:00
|
|
|
prefix = parts[0]
|
|
|
|
if len(prefix) != 1 || !strings.Contains(allKeyPrefixes, prefix) {
|
|
|
|
return "", uuid.Nil, 0, fmt.Errorf("invalid id")
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2021-01-21 07:03:34 +08:00
|
|
|
return prefix, id, score, nil
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListOption specifies behavior of list operation.
|
|
|
|
type ListOption interface{}
|
|
|
|
|
|
|
|
// Internal list option representations.
|
|
|
|
type (
|
|
|
|
pageSizeOpt int
|
|
|
|
pageNumOpt int
|
|
|
|
)
|
|
|
|
|
|
|
|
type listOption struct {
|
|
|
|
pageSize int
|
|
|
|
pageNum int
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Page size used by default in list operation.
|
|
|
|
defaultPageSize = 30
|
|
|
|
|
|
|
|
// Page number used by default in list operation.
|
|
|
|
defaultPageNum = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
func composeListOptions(opts ...ListOption) listOption {
|
|
|
|
res := listOption{
|
|
|
|
pageSize: defaultPageSize,
|
|
|
|
pageNum: defaultPageNum,
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt := opt.(type) {
|
|
|
|
case pageSizeOpt:
|
|
|
|
res.pageSize = int(opt)
|
|
|
|
case pageNumOpt:
|
|
|
|
res.pageNum = int(opt)
|
|
|
|
default:
|
|
|
|
// ignore unexpected option
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// PageSize returns an option to specify the page size for list operation.
|
|
|
|
//
|
|
|
|
// Negative page size is treated as zero.
|
|
|
|
func PageSize(n int) ListOption {
|
|
|
|
if n < 0 {
|
|
|
|
n = 0
|
|
|
|
}
|
|
|
|
return pageSizeOpt(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Page returns an option to specify the page number for list operation.
|
|
|
|
// The value 1 fetches the first page.
|
|
|
|
//
|
|
|
|
// Negative page number is treated as one.
|
|
|
|
func Page(n int) ListOption {
|
|
|
|
if n < 0 {
|
|
|
|
n = 1
|
|
|
|
}
|
|
|
|
return pageNumOpt(n)
|
|
|
|
}
|
|
|
|
|
2020-09-05 22:03:43 +08:00
|
|
|
// ListPendingTasks retrieves pending tasks from the specified queue.
|
2020-07-13 21:29:41 +08:00
|
|
|
//
|
|
|
|
// By default, it retrieves the first 30 tasks.
|
2020-09-05 22:03:43 +08:00
|
|
|
func (i *Inspector) ListPendingTasks(qname string, opts ...ListOption) ([]*PendingTask, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
opt := composeListOptions(opts...)
|
|
|
|
pgn := rdb.Pagination{Size: opt.pageSize, Page: opt.pageNum - 1}
|
2020-09-05 22:03:43 +08:00
|
|
|
msgs, err := i.rdb.ListPending(qname, pgn)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-05 22:03:43 +08:00
|
|
|
var tasks []*PendingTask
|
2020-07-13 21:29:41 +08:00
|
|
|
for _, m := range msgs {
|
2020-09-05 22:03:43 +08:00
|
|
|
tasks = append(tasks, &PendingTask{
|
2021-01-29 00:59:13 +08:00
|
|
|
Task: asynq.NewTask(m.Type, m.Payload),
|
2021-01-27 22:59:32 +08:00
|
|
|
ID: m.ID.String(),
|
|
|
|
Queue: m.Queue,
|
|
|
|
MaxRetry: m.Retry,
|
|
|
|
Retried: m.Retried,
|
|
|
|
LastError: m.ErrorMsg,
|
2020-07-13 21:29:41 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return tasks, err
|
|
|
|
}
|
|
|
|
|
2020-09-06 03:43:15 +08:00
|
|
|
// ListActiveTasks retrieves active tasks from the specified queue.
|
2020-07-13 21:29:41 +08:00
|
|
|
//
|
|
|
|
// By default, it retrieves the first 30 tasks.
|
2020-09-06 03:43:15 +08:00
|
|
|
func (i *Inspector) ListActiveTasks(qname string, opts ...ListOption) ([]*ActiveTask, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
opt := composeListOptions(opts...)
|
|
|
|
pgn := rdb.Pagination{Size: opt.pageSize, Page: opt.pageNum - 1}
|
2020-09-06 03:43:15 +08:00
|
|
|
msgs, err := i.rdb.ListActive(qname, pgn)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-06 03:43:15 +08:00
|
|
|
var tasks []*ActiveTask
|
2020-07-13 21:29:41 +08:00
|
|
|
for _, m := range msgs {
|
2021-01-27 22:59:32 +08:00
|
|
|
|
2020-09-06 03:43:15 +08:00
|
|
|
tasks = append(tasks, &ActiveTask{
|
2021-01-29 00:59:13 +08:00
|
|
|
Task: asynq.NewTask(m.Type, m.Payload),
|
2021-01-27 22:59:32 +08:00
|
|
|
ID: m.ID.String(),
|
|
|
|
Queue: m.Queue,
|
|
|
|
MaxRetry: m.Retry,
|
|
|
|
Retried: m.Retried,
|
|
|
|
LastError: m.ErrorMsg,
|
2020-07-13 21:29:41 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return tasks, err
|
|
|
|
}
|
|
|
|
|
2020-08-17 05:51:56 +08:00
|
|
|
// ListScheduledTasks retrieves scheduled tasks from the specified queue.
|
2020-09-05 22:11:55 +08:00
|
|
|
// Tasks are sorted by NextProcessAt field in ascending order.
|
2020-07-13 21:29:41 +08:00
|
|
|
//
|
|
|
|
// By default, it retrieves the first 30 tasks.
|
2020-08-17 05:51:56 +08:00
|
|
|
func (i *Inspector) ListScheduledTasks(qname string, opts ...ListOption) ([]*ScheduledTask, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
opt := composeListOptions(opts...)
|
|
|
|
pgn := rdb.Pagination{Size: opt.pageSize, Page: opt.pageNum - 1}
|
2020-08-17 05:51:56 +08:00
|
|
|
zs, err := i.rdb.ListScheduled(qname, pgn)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var tasks []*ScheduledTask
|
|
|
|
for _, z := range zs {
|
2020-09-05 22:11:55 +08:00
|
|
|
processAt := time.Unix(z.Score, 0)
|
2021-01-29 00:59:13 +08:00
|
|
|
t := asynq.NewTask(z.Message.Type, z.Message.Payload)
|
2020-07-13 21:29:41 +08:00
|
|
|
tasks = append(tasks, &ScheduledTask{
|
|
|
|
Task: t,
|
|
|
|
ID: z.Message.ID.String(),
|
|
|
|
Queue: z.Message.Queue,
|
2021-01-27 22:59:32 +08:00
|
|
|
MaxRetry: z.Message.Retry,
|
|
|
|
Retried: z.Message.Retried,
|
|
|
|
LastError: z.Message.ErrorMsg,
|
2020-09-05 22:11:55 +08:00
|
|
|
NextProcessAt: processAt,
|
2020-07-13 21:29:41 +08:00
|
|
|
score: z.Score,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return tasks, nil
|
|
|
|
}
|
|
|
|
|
2020-08-17 05:51:56 +08:00
|
|
|
// ListRetryTasks retrieves retry tasks from the specified queue.
|
2020-09-05 22:11:55 +08:00
|
|
|
// Tasks are sorted by NextProcessAt field in ascending order.
|
2020-07-13 21:29:41 +08:00
|
|
|
//
|
|
|
|
// By default, it retrieves the first 30 tasks.
|
2020-08-17 05:51:56 +08:00
|
|
|
func (i *Inspector) ListRetryTasks(qname string, opts ...ListOption) ([]*RetryTask, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
opt := composeListOptions(opts...)
|
|
|
|
pgn := rdb.Pagination{Size: opt.pageSize, Page: opt.pageNum - 1}
|
2020-08-17 05:51:56 +08:00
|
|
|
zs, err := i.rdb.ListRetry(qname, pgn)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var tasks []*RetryTask
|
|
|
|
for _, z := range zs {
|
2020-09-05 22:11:55 +08:00
|
|
|
processAt := time.Unix(z.Score, 0)
|
2021-01-29 00:59:13 +08:00
|
|
|
t := asynq.NewTask(z.Message.Type, z.Message.Payload)
|
2020-07-13 21:29:41 +08:00
|
|
|
tasks = append(tasks, &RetryTask{
|
|
|
|
Task: t,
|
|
|
|
ID: z.Message.ID.String(),
|
|
|
|
Queue: z.Message.Queue,
|
2020-09-05 22:11:55 +08:00
|
|
|
NextProcessAt: processAt,
|
2020-07-13 21:29:41 +08:00
|
|
|
MaxRetry: z.Message.Retry,
|
|
|
|
Retried: z.Message.Retried,
|
|
|
|
// TODO: LastFailedAt: z.Message.LastFailedAt
|
2021-01-27 22:59:32 +08:00
|
|
|
LastError: z.Message.ErrorMsg,
|
|
|
|
score: z.Score,
|
2020-07-13 21:29:41 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return tasks, nil
|
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
// ListArchivedTasks retrieves archived tasks from the specified queue.
|
2020-07-13 21:29:41 +08:00
|
|
|
// Tasks are sorted by LastFailedAt field in descending order.
|
|
|
|
//
|
|
|
|
// By default, it retrieves the first 30 tasks.
|
2021-01-13 03:01:21 +08:00
|
|
|
func (i *Inspector) ListArchivedTasks(qname string, opts ...ListOption) ([]*ArchivedTask, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
opt := composeListOptions(opts...)
|
|
|
|
pgn := rdb.Pagination{Size: opt.pageSize, Page: opt.pageNum - 1}
|
2021-01-13 03:01:21 +08:00
|
|
|
zs, err := i.rdb.ListArchived(qname, pgn)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
var tasks []*ArchivedTask
|
2020-07-13 21:29:41 +08:00
|
|
|
for _, z := range zs {
|
|
|
|
failedAt := time.Unix(z.Score, 0)
|
2021-01-29 00:59:13 +08:00
|
|
|
t := asynq.NewTask(z.Message.Type, z.Message.Payload)
|
2021-01-13 03:01:21 +08:00
|
|
|
tasks = append(tasks, &ArchivedTask{
|
2020-07-13 21:29:41 +08:00
|
|
|
Task: t,
|
|
|
|
ID: z.Message.ID.String(),
|
|
|
|
Queue: z.Message.Queue,
|
|
|
|
MaxRetry: z.Message.Retry,
|
|
|
|
Retried: z.Message.Retried,
|
|
|
|
LastFailedAt: failedAt,
|
2021-01-27 22:59:32 +08:00
|
|
|
LastError: z.Message.ErrorMsg,
|
2020-07-13 21:29:41 +08:00
|
|
|
score: z.Score,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return tasks, nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 07:03:34 +08:00
|
|
|
// DeleteAllPendingTasks deletes all pending tasks from the specified queue,
|
|
|
|
// and reports the number tasks deleted.
|
|
|
|
func (i *Inspector) DeleteAllPendingTasks(qname string) (int, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2021-01-21 07:03:34 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
n, err := i.rdb.DeleteAllPendingTasks(qname)
|
|
|
|
return int(n), err
|
|
|
|
}
|
|
|
|
|
2020-08-17 05:51:56 +08:00
|
|
|
// DeleteAllScheduledTasks deletes all scheduled tasks from the specified queue,
|
2020-07-13 21:29:41 +08:00
|
|
|
// and reports the number tasks deleted.
|
2020-08-17 05:51:56 +08:00
|
|
|
func (i *Inspector) DeleteAllScheduledTasks(qname string) (int, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
n, err := i.rdb.DeleteAllScheduledTasks(qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
return int(n), err
|
|
|
|
}
|
|
|
|
|
2020-08-17 05:51:56 +08:00
|
|
|
// DeleteAllRetryTasks deletes all retry tasks from the specified queue,
|
2020-07-13 21:29:41 +08:00
|
|
|
// and reports the number tasks deleted.
|
2020-08-17 05:51:56 +08:00
|
|
|
func (i *Inspector) DeleteAllRetryTasks(qname string) (int, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
2020-08-17 05:51:56 +08:00
|
|
|
n, err := i.rdb.DeleteAllRetryTasks(qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
return int(n), err
|
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
// DeleteAllArchivedTasks deletes all archived tasks from the specified queue,
|
2020-07-13 21:29:41 +08:00
|
|
|
// and reports the number tasks deleted.
|
2021-01-13 03:01:21 +08:00
|
|
|
func (i *Inspector) DeleteAllArchivedTasks(qname string) (int, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
n, err := i.rdb.DeleteAllArchivedTasks(qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
return int(n), err
|
|
|
|
}
|
|
|
|
|
2020-08-17 05:51:56 +08:00
|
|
|
// DeleteTaskByKey deletes a task with the given key from the given queue.
|
2021-03-13 08:23:08 +08:00
|
|
|
// TODO: We don't need score any more. Update this to delete task by ID
|
2020-08-17 05:51:56 +08:00
|
|
|
func (i *Inspector) DeleteTaskByKey(qname, key string) error {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-03-13 08:23:08 +08:00
|
|
|
prefix, id, _, err := parseTaskKey(key)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-21 07:03:34 +08:00
|
|
|
switch prefix {
|
|
|
|
case keyPrefixPending:
|
|
|
|
return i.rdb.DeletePendingTask(qname, id)
|
|
|
|
case keyPrefixScheduled:
|
2021-03-13 08:23:08 +08:00
|
|
|
return i.rdb.DeleteScheduledTask(qname, id)
|
2021-01-21 07:03:34 +08:00
|
|
|
case keyPrefixRetry:
|
2021-03-13 08:23:08 +08:00
|
|
|
return i.rdb.DeleteRetryTask(qname, id)
|
2021-01-21 07:03:34 +08:00
|
|
|
case keyPrefixArchived:
|
2021-03-13 08:23:08 +08:00
|
|
|
return i.rdb.DeleteArchivedTask(qname, id)
|
2020-07-13 21:29:41 +08:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid key")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 07:03:34 +08:00
|
|
|
// RunAllScheduledTasks transition all scheduled tasks to pending state from the given queue,
|
2020-09-06 04:35:52 +08:00
|
|
|
// and reports the number of tasks transitioned.
|
|
|
|
func (i *Inspector) RunAllScheduledTasks(qname string) (int, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
2020-09-06 04:35:52 +08:00
|
|
|
n, err := i.rdb.RunAllScheduledTasks(qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
return int(n), err
|
|
|
|
}
|
|
|
|
|
2021-01-21 07:03:34 +08:00
|
|
|
// RunAllRetryTasks transition all retry tasks to pending state from the given queue,
|
2020-09-06 04:35:52 +08:00
|
|
|
// and reports the number of tasks transitioned.
|
|
|
|
func (i *Inspector) RunAllRetryTasks(qname string) (int, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
2020-09-06 04:35:52 +08:00
|
|
|
n, err := i.rdb.RunAllRetryTasks(qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
return int(n), err
|
|
|
|
}
|
|
|
|
|
2021-01-21 07:03:34 +08:00
|
|
|
// RunAllArchivedTasks transition all archived tasks to pending state from the given queue,
|
2020-09-06 04:35:52 +08:00
|
|
|
// and reports the number of tasks transitioned.
|
2021-01-13 03:01:21 +08:00
|
|
|
func (i *Inspector) RunAllArchivedTasks(qname string) (int, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
n, err := i.rdb.RunAllArchivedTasks(qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
return int(n), err
|
|
|
|
}
|
|
|
|
|
2020-09-06 04:35:52 +08:00
|
|
|
// RunTaskByKey transition a task to pending state given task key and queue name.
|
2021-03-13 08:23:08 +08:00
|
|
|
// TODO: Update this to run task by ID.
|
2020-09-06 04:35:52 +08:00
|
|
|
func (i *Inspector) RunTaskByKey(qname, key string) error {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-03-13 08:23:08 +08:00
|
|
|
prefix, id, _, err := parseTaskKey(key)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-21 07:03:34 +08:00
|
|
|
switch prefix {
|
|
|
|
case keyPrefixScheduled:
|
2021-04-28 22:27:35 +08:00
|
|
|
return i.rdb.RunTask(qname, id)
|
2021-01-21 07:03:34 +08:00
|
|
|
case keyPrefixRetry:
|
2021-04-28 22:27:35 +08:00
|
|
|
return i.rdb.RunTask(qname, id)
|
2021-01-21 07:03:34 +08:00
|
|
|
case keyPrefixArchived:
|
2021-04-28 22:27:35 +08:00
|
|
|
return i.rdb.RunTask(qname, id)
|
2021-01-21 07:03:34 +08:00
|
|
|
case keyPrefixPending:
|
|
|
|
return fmt.Errorf("task is already pending for run")
|
2020-07-13 21:29:41 +08:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid key")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 07:03:34 +08:00
|
|
|
// ArchiveAllPendingTasks archives all pending tasks from the given queue,
|
|
|
|
// and reports the number of tasks archived.
|
|
|
|
func (i *Inspector) ArchiveAllPendingTasks(qname string) (int, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2021-01-21 07:03:34 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
n, err := i.rdb.ArchiveAllPendingTasks(qname)
|
|
|
|
return int(n), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArchiveAllScheduledTasks archives all scheduled tasks from the given queue,
|
2021-01-13 03:01:21 +08:00
|
|
|
// and reports the number of tasks archiveed.
|
|
|
|
func (i *Inspector) ArchiveAllScheduledTasks(qname string) (int, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
n, err := i.rdb.ArchiveAllScheduledTasks(qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
return int(n), err
|
|
|
|
}
|
|
|
|
|
2021-01-21 07:03:34 +08:00
|
|
|
// ArchiveAllRetryTasks archives all retry tasks from the given queue,
|
2021-01-13 03:01:21 +08:00
|
|
|
// and reports the number of tasks archiveed.
|
|
|
|
func (i *Inspector) ArchiveAllRetryTasks(qname string) (int, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return 0, err
|
|
|
|
}
|
2021-01-13 03:01:21 +08:00
|
|
|
n, err := i.rdb.ArchiveAllRetryTasks(qname)
|
2020-07-13 21:29:41 +08:00
|
|
|
return int(n), err
|
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
// ArchiveTaskByKey archives a task with the given key in the given queue.
|
2021-03-13 08:23:08 +08:00
|
|
|
// TODO: Update this to Archive task by ID.
|
2021-01-13 03:01:21 +08:00
|
|
|
func (i *Inspector) ArchiveTaskByKey(qname, key string) error {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-03-13 08:23:08 +08:00
|
|
|
prefix, id, _, err := parseTaskKey(key)
|
2020-07-13 21:29:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-21 07:03:34 +08:00
|
|
|
switch prefix {
|
|
|
|
case keyPrefixPending:
|
|
|
|
return i.rdb.ArchivePendingTask(qname, id)
|
|
|
|
case keyPrefixScheduled:
|
2021-03-13 08:23:08 +08:00
|
|
|
return i.rdb.ArchiveScheduledTask(qname, id)
|
2021-01-21 07:03:34 +08:00
|
|
|
case keyPrefixRetry:
|
2021-03-13 08:23:08 +08:00
|
|
|
return i.rdb.ArchiveRetryTask(qname, id)
|
2021-01-21 07:03:34 +08:00
|
|
|
case keyPrefixArchived:
|
|
|
|
return fmt.Errorf("task is already archived")
|
2020-07-13 21:29:41 +08:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid key")
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 12:56:48 +08:00
|
|
|
|
2020-12-05 22:49:01 +08:00
|
|
|
// CancelActiveTask sends a signal to cancel processing of the task with
|
|
|
|
// the given id. CancelActiveTask is best-effort, which means that it does not
|
|
|
|
// guarantee that the task with the given id will be canceled. The return
|
|
|
|
// value only indicates whether the cancelation signal has been sent.
|
|
|
|
func (i *Inspector) CancelActiveTask(id string) error {
|
|
|
|
return i.rdb.PublishCancelation(id)
|
|
|
|
}
|
|
|
|
|
2020-07-29 12:56:48 +08:00
|
|
|
// PauseQueue pauses task processing on the specified queue.
|
|
|
|
// If the queue is already paused, it will return a non-nil error.
|
|
|
|
func (i *Inspector) PauseQueue(qname string) error {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return err
|
|
|
|
}
|
2020-07-29 12:56:48 +08:00
|
|
|
return i.rdb.Pause(qname)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnpauseQueue resumes task processing on the specified queue.
|
|
|
|
// If the queue is not paused, it will return a non-nil error.
|
|
|
|
func (i *Inspector) UnpauseQueue(qname string) error {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2020-08-31 20:53:17 +08:00
|
|
|
return err
|
|
|
|
}
|
2020-07-29 12:56:48 +08:00
|
|
|
return i.rdb.Unpause(qname)
|
|
|
|
}
|
2020-09-01 21:57:08 +08:00
|
|
|
|
2020-12-30 23:10:53 +08:00
|
|
|
// Servers return a list of running servers' information.
|
|
|
|
func (i *Inspector) Servers() ([]*ServerInfo, error) {
|
|
|
|
servers, err := i.rdb.ListServers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
workers, err := i.rdb.ListWorkers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m := make(map[string]*ServerInfo) // ServerInfo keyed by serverID
|
|
|
|
for _, s := range servers {
|
|
|
|
m[s.ServerID] = &ServerInfo{
|
|
|
|
ID: s.ServerID,
|
|
|
|
Host: s.Host,
|
|
|
|
PID: s.PID,
|
|
|
|
Concurrency: s.Concurrency,
|
|
|
|
Queues: s.Queues,
|
|
|
|
StrictPriority: s.StrictPriority,
|
|
|
|
Started: s.Started,
|
|
|
|
Status: s.Status,
|
|
|
|
ActiveWorkers: make([]*WorkerInfo, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, w := range workers {
|
|
|
|
srvInfo, ok := m[w.ServerID]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wrkInfo := &WorkerInfo{
|
2021-01-28 07:55:43 +08:00
|
|
|
Started: w.Started,
|
|
|
|
Deadline: w.Deadline,
|
2020-12-30 23:10:53 +08:00
|
|
|
Task: &ActiveTask{
|
2021-01-29 00:59:13 +08:00
|
|
|
Task: asynq.NewTask(w.Type, w.Payload),
|
2020-12-30 23:10:53 +08:00
|
|
|
ID: w.ID,
|
|
|
|
Queue: w.Queue,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
srvInfo.ActiveWorkers = append(srvInfo.ActiveWorkers, wrkInfo)
|
|
|
|
}
|
|
|
|
var out []*ServerInfo
|
|
|
|
for _, srvInfo := range m {
|
|
|
|
out = append(out, srvInfo)
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerInfo describes a running Server instance.
|
|
|
|
type ServerInfo struct {
|
|
|
|
// Unique Identifier for the server.
|
|
|
|
ID string
|
|
|
|
// Host machine on which the server is running.
|
|
|
|
Host string
|
|
|
|
// PID of the process in which the server is running.
|
|
|
|
PID int
|
|
|
|
|
|
|
|
// Server configuration details.
|
|
|
|
// See Config doc for field descriptions.
|
|
|
|
Concurrency int
|
|
|
|
Queues map[string]int
|
|
|
|
StrictPriority bool
|
|
|
|
|
|
|
|
// Time the server started.
|
|
|
|
Started time.Time
|
|
|
|
// Status indicates the status of the server.
|
|
|
|
// TODO: Update comment with more details.
|
|
|
|
Status string
|
|
|
|
// A List of active workers currently processing tasks.
|
|
|
|
ActiveWorkers []*WorkerInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// WorkerInfo describes a running worker processing a task.
|
|
|
|
type WorkerInfo struct {
|
|
|
|
// The task the worker is processing.
|
|
|
|
Task *ActiveTask
|
|
|
|
// Time the worker started processing the task.
|
|
|
|
Started time.Time
|
2021-01-28 07:55:43 +08:00
|
|
|
// Time the worker needs to finish processing the task by.
|
|
|
|
Deadline time.Time
|
2020-12-30 23:10:53 +08:00
|
|
|
}
|
|
|
|
|
2020-09-01 21:57:08 +08:00
|
|
|
// ClusterKeySlot returns an integer identifying the hash slot the given queue hashes to.
|
|
|
|
func (i *Inspector) ClusterKeySlot(qname string) (int64, error) {
|
|
|
|
return i.rdb.ClusterKeySlot(qname)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClusterNode describes a node in redis cluster.
|
|
|
|
type ClusterNode struct {
|
|
|
|
// Node ID in the cluster.
|
|
|
|
ID string
|
|
|
|
|
|
|
|
// Address of the node.
|
|
|
|
Addr string
|
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
// ClusterNodes returns a list of nodes the given queue belongs to.
|
2020-09-01 21:57:08 +08:00
|
|
|
func (i *Inspector) ClusterNodes(qname string) ([]ClusterNode, error) {
|
|
|
|
nodes, err := i.rdb.ClusterNodes(qname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var res []ClusterNode
|
|
|
|
for _, node := range nodes {
|
|
|
|
res = append(res, ClusterNode{ID: node.ID, Addr: node.Addr})
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2020-12-01 23:37:09 +08:00
|
|
|
|
|
|
|
// SchedulerEntry holds information about a periodic task registered with a scheduler.
|
|
|
|
type SchedulerEntry struct {
|
|
|
|
// Identifier of this entry.
|
|
|
|
ID string
|
|
|
|
|
|
|
|
// Spec describes the schedule of this entry.
|
|
|
|
Spec string
|
|
|
|
|
|
|
|
// Periodic Task registered for this entry.
|
2021-01-29 00:59:13 +08:00
|
|
|
Task *asynq.Task
|
2020-12-01 23:37:09 +08:00
|
|
|
|
|
|
|
// Opts is the options for the periodic task.
|
2021-01-29 00:59:13 +08:00
|
|
|
Opts []asynq.Option
|
2020-12-01 23:37:09 +08:00
|
|
|
|
|
|
|
// Next shows the next time the task will be enqueued.
|
|
|
|
Next time.Time
|
|
|
|
|
|
|
|
// Prev shows the last time the task was enqueued.
|
|
|
|
// Zero time if task was never enqueued.
|
|
|
|
Prev time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// SchedulerEntries returns a list of all entries registered with
|
|
|
|
// currently running schedulers.
|
|
|
|
func (i *Inspector) SchedulerEntries() ([]*SchedulerEntry, error) {
|
|
|
|
var entries []*SchedulerEntry
|
|
|
|
res, err := i.rdb.ListSchedulerEntries()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, e := range res {
|
2021-01-29 00:59:13 +08:00
|
|
|
task := asynq.NewTask(e.Type, e.Payload)
|
|
|
|
var opts []asynq.Option
|
2020-12-01 23:37:09 +08:00
|
|
|
for _, s := range e.Opts {
|
|
|
|
if o, err := parseOption(s); err == nil {
|
|
|
|
// ignore bad data
|
|
|
|
opts = append(opts, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entries = append(entries, &SchedulerEntry{
|
|
|
|
ID: e.ID,
|
|
|
|
Spec: e.Spec,
|
|
|
|
Task: task,
|
|
|
|
Opts: opts,
|
|
|
|
Next: e.Next,
|
|
|
|
Prev: e.Prev,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return entries, nil
|
|
|
|
}
|
2020-12-26 23:03:04 +08:00
|
|
|
|
2021-01-29 00:59:13 +08:00
|
|
|
// parseOption interprets a string s as an Option and returns the Option if parsing is successful,
|
|
|
|
// otherwise returns non-nil error.
|
|
|
|
func parseOption(s string) (asynq.Option, error) {
|
|
|
|
fn, arg := parseOptionFunc(s), parseOptionArg(s)
|
|
|
|
switch fn {
|
|
|
|
case "Queue":
|
|
|
|
qname, err := strconv.Unquote(arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return asynq.Queue(qname), nil
|
|
|
|
case "MaxRetry":
|
|
|
|
n, err := strconv.Atoi(arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return asynq.MaxRetry(n), nil
|
|
|
|
case "Timeout":
|
|
|
|
d, err := time.ParseDuration(arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return asynq.Timeout(d), nil
|
|
|
|
case "Deadline":
|
|
|
|
t, err := time.Parse(time.UnixDate, arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return asynq.Deadline(t), nil
|
|
|
|
case "Unique":
|
|
|
|
d, err := time.ParseDuration(arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return asynq.Unique(d), nil
|
|
|
|
case "ProcessAt":
|
|
|
|
t, err := time.Parse(time.UnixDate, arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return asynq.ProcessAt(t), nil
|
|
|
|
case "ProcessIn":
|
|
|
|
d, err := time.ParseDuration(arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return asynq.ProcessIn(d), nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("cannot not parse option string %q", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseOptionFunc(s string) string {
|
|
|
|
i := strings.Index(s, "(")
|
|
|
|
return s[:i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseOptionArg(s string) string {
|
|
|
|
i := strings.Index(s, "(")
|
|
|
|
if i >= 0 {
|
|
|
|
j := strings.Index(s, ")")
|
|
|
|
if j > i {
|
|
|
|
return s[i+1 : j]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-12-26 23:03:04 +08:00
|
|
|
// SchedulerEnqueueEvent holds information about an enqueue event by a scheduler.
|
|
|
|
type SchedulerEnqueueEvent struct {
|
|
|
|
// ID of the task that was enqueued.
|
|
|
|
TaskID string
|
|
|
|
|
|
|
|
// Time the task was enqueued.
|
|
|
|
EnqueuedAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListSchedulerEnqueueEvents retrieves a list of enqueue events from the specified scheduler entry.
|
|
|
|
//
|
|
|
|
// By default, it retrieves the first 30 tasks.
|
|
|
|
func (i *Inspector) ListSchedulerEnqueueEvents(entryID string, opts ...ListOption) ([]*SchedulerEnqueueEvent, error) {
|
|
|
|
opt := composeListOptions(opts...)
|
|
|
|
pgn := rdb.Pagination{Size: opt.pageSize, Page: opt.pageNum - 1}
|
|
|
|
data, err := i.rdb.ListSchedulerEnqueueEvents(entryID, pgn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var events []*SchedulerEnqueueEvent
|
|
|
|
for _, e := range data {
|
|
|
|
events = append(events, &SchedulerEnqueueEvent{TaskID: e.TaskID, EnqueuedAt: e.EnqueuedAt})
|
|
|
|
}
|
|
|
|
return events, nil
|
|
|
|
}
|