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-05-19 09:45:15 +08:00
|
|
|
package asynq
|
2020-07-13 21:29:41 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-09-02 20:56:02 +08:00
|
|
|
"github.com/go-redis/redis/v8"
|
2021-01-29 00:59:13 +08:00
|
|
|
"github.com/hibiken/asynq/internal/base"
|
2021-05-11 12:19:57 +08:00
|
|
|
"github.com/hibiken/asynq/internal/errors"
|
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.
|
2021-05-19 09:45:15 +08:00
|
|
|
func NewInspector(r 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()
|
|
|
|
}
|
|
|
|
|
2021-05-23 03:13:19 +08:00
|
|
|
// QueueInfo represents a state of queues at a certain time.
|
|
|
|
type QueueInfo struct {
|
2020-08-17 05:51:56 +08:00
|
|
|
// Name of the queue.
|
|
|
|
Queue string
|
2021-06-14 22:31:39 +08:00
|
|
|
|
2021-01-27 10:36:45 +08:00
|
|
|
// Total number of bytes that the queue and its tasks require to be stored in redis.
|
2021-08-09 21:09:10 +08:00
|
|
|
// It is an approximate memory usage value in bytes since the value is computed by sampling.
|
2021-01-27 10:36:45 +08:00
|
|
|
MemoryUsage int64
|
2021-06-14 22:31:39 +08:00
|
|
|
|
2021-12-11 22:27:44 +08:00
|
|
|
// Latency of the queue, measured by the oldest pending task in the queue.
|
|
|
|
Latency time.Duration
|
|
|
|
|
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
|
2021-06-14 22:31:39 +08:00
|
|
|
|
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
|
2021-11-06 07:52:54 +08:00
|
|
|
// Number of stored completed tasks.
|
|
|
|
Completed int
|
2021-06-14 22:31:39 +08:00
|
|
|
|
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
|
2021-06-14 22:31:39 +08:00
|
|
|
|
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
|
2021-06-14 22:31:39 +08:00
|
|
|
|
|
|
|
// Time when this queue info snapshot was taken.
|
2020-08-17 05:51:56 +08:00
|
|
|
Timestamp time.Time
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
2021-05-23 03:13:19 +08:00
|
|
|
// GetQueueInfo returns current information of the given queue.
|
|
|
|
func (i *Inspector) GetQueueInfo(qname string) (*QueueInfo, 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
|
|
|
|
}
|
2021-05-23 03:13:19 +08:00
|
|
|
return &QueueInfo{
|
2021-01-27 10:36:45 +08:00
|
|
|
Queue: stats.Queue,
|
|
|
|
MemoryUsage: stats.MemoryUsage,
|
2021-12-11 22:27:44 +08:00
|
|
|
Latency: stats.Latency,
|
2021-01-27 10:36:45 +08:00
|
|
|
Size: stats.Size,
|
|
|
|
Pending: stats.Pending,
|
|
|
|
Active: stats.Active,
|
|
|
|
Scheduled: stats.Scheduled,
|
|
|
|
Retry: stats.Retry,
|
|
|
|
Archived: stats.Archived,
|
2021-11-06 07:52:54 +08:00
|
|
|
Completed: stats.Completed,
|
2021-01-27 10:36:45 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-01 21:47:49 +08:00
|
|
|
var (
|
|
|
|
// ErrQueueNotFound indicates that the specified queue does not exist.
|
|
|
|
ErrQueueNotFound = errors.New("queue not found")
|
2021-05-20 07:39:02 +08:00
|
|
|
|
2021-05-01 21:47:49 +08:00
|
|
|
// ErrQueueNotEmpty indicates that the specified queue is not empty.
|
|
|
|
ErrQueueNotEmpty = errors.New("queue is not empty")
|
2021-05-20 07:39:02 +08:00
|
|
|
|
2021-05-16 22:32:47 +08:00
|
|
|
// ErrTaskNotFound indicates that the specified task cannot be found in the queue.
|
|
|
|
ErrTaskNotFound = errors.New("task not found")
|
2021-05-01 21:47:49 +08:00
|
|
|
)
|
2020-11-28 14:27:54 +08:00
|
|
|
|
|
|
|
// 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)
|
2021-05-11 12:19:57 +08:00
|
|
|
if errors.IsQueueNotFound(err) {
|
2021-05-01 21:47:49 +08:00
|
|
|
return fmt.Errorf("%w: queue=%q", ErrQueueNotFound, qname)
|
2020-11-28 14:27:54 +08:00
|
|
|
}
|
2021-05-11 12:19:57 +08:00
|
|
|
if errors.IsQueueNotEmpty(err) {
|
2021-05-01 21:47:49 +08:00
|
|
|
return fmt.Errorf("%w: queue=%q", ErrQueueNotEmpty, qname)
|
2020-11-28 14:27:54 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-23 09:30:44 +08:00
|
|
|
// GetTaskInfo retrieves task information given a task id and queue name.
|
|
|
|
//
|
2021-11-10 07:43:04 +08:00
|
|
|
// Returns an error wrapping ErrQueueNotFound if a queue with the given name doesn't exist.
|
|
|
|
// Returns an error wrapping ErrTaskNotFound if a task with the given id doesn't exist in the queue.
|
2021-05-23 09:30:44 +08:00
|
|
|
func (i *Inspector) GetTaskInfo(qname, id string) (*TaskInfo, error) {
|
2021-09-12 12:11:04 +08:00
|
|
|
info, err := i.rdb.GetTaskInfo(qname, id)
|
2021-05-23 09:30:44 +08:00
|
|
|
switch {
|
|
|
|
case errors.IsQueueNotFound(err):
|
|
|
|
return nil, fmt.Errorf("asynq: %w", ErrQueueNotFound)
|
|
|
|
case errors.IsTaskNotFound(err):
|
|
|
|
return nil, fmt.Errorf("asynq: %w", ErrTaskNotFound)
|
|
|
|
case err != nil:
|
|
|
|
return nil, fmt.Errorf("asynq: %v", err)
|
|
|
|
}
|
2021-11-06 07:52:54 +08:00
|
|
|
return newTaskInfo(info.Message, info.State, info.NextProcessAt, info.Result), nil
|
2021-05-23 09:30:44 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2021-05-19 10:13:52 +08:00
|
|
|
func (i *Inspector) ListPendingTasks(qname string, opts ...ListOption) ([]*TaskInfo, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2021-05-19 10:13:52 +08:00
|
|
|
return nil, fmt.Errorf("asynq: %v", err)
|
2020-08-31 20:53:17 +08:00
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
opt := composeListOptions(opts...)
|
|
|
|
pgn := rdb.Pagination{Size: opt.pageSize, Page: opt.pageNum - 1}
|
2021-11-06 07:52:54 +08:00
|
|
|
infos, err := i.rdb.ListPending(qname, pgn)
|
2021-05-19 20:59:30 +08:00
|
|
|
switch {
|
|
|
|
case errors.IsQueueNotFound(err):
|
|
|
|
return nil, fmt.Errorf("asynq: %w", ErrQueueNotFound)
|
|
|
|
case err != nil:
|
2021-05-19 10:13:52 +08:00
|
|
|
return nil, fmt.Errorf("asynq: %v", err)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2021-05-19 10:13:52 +08:00
|
|
|
var tasks []*TaskInfo
|
2021-11-06 07:52:54 +08:00
|
|
|
for _, i := range infos {
|
|
|
|
tasks = append(tasks, newTaskInfo(
|
|
|
|
i.Message,
|
|
|
|
i.State,
|
|
|
|
i.NextProcessAt,
|
|
|
|
i.Result,
|
|
|
|
))
|
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.
|
2021-05-19 10:13:52 +08:00
|
|
|
func (i *Inspector) ListActiveTasks(qname string, opts ...ListOption) ([]*TaskInfo, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2021-05-19 10:13:52 +08:00
|
|
|
return nil, fmt.Errorf("asynq: %v", err)
|
2020-08-31 20:53:17 +08:00
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
opt := composeListOptions(opts...)
|
|
|
|
pgn := rdb.Pagination{Size: opt.pageSize, Page: opt.pageNum - 1}
|
2021-11-06 07:52:54 +08:00
|
|
|
infos, err := i.rdb.ListActive(qname, pgn)
|
2021-05-19 20:59:30 +08:00
|
|
|
switch {
|
|
|
|
case errors.IsQueueNotFound(err):
|
|
|
|
return nil, fmt.Errorf("asynq: %w", ErrQueueNotFound)
|
|
|
|
case err != nil:
|
2021-05-19 10:13:52 +08:00
|
|
|
return nil, fmt.Errorf("asynq: %v", err)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2021-05-19 10:13:52 +08:00
|
|
|
var tasks []*TaskInfo
|
2021-11-06 07:52:54 +08:00
|
|
|
for _, i := range infos {
|
|
|
|
tasks = append(tasks, newTaskInfo(
|
|
|
|
i.Message,
|
|
|
|
i.State,
|
|
|
|
i.NextProcessAt,
|
|
|
|
i.Result,
|
|
|
|
))
|
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.
|
2021-05-19 10:13:52 +08:00
|
|
|
// Tasks are sorted by NextProcessAt in ascending order.
|
2020-07-13 21:29:41 +08:00
|
|
|
//
|
|
|
|
// By default, it retrieves the first 30 tasks.
|
2021-05-19 10:13:52 +08:00
|
|
|
func (i *Inspector) ListScheduledTasks(qname string, opts ...ListOption) ([]*TaskInfo, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2021-05-19 10:13:52 +08:00
|
|
|
return nil, fmt.Errorf("asynq: %v", err)
|
2020-08-31 20:53:17 +08:00
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
opt := composeListOptions(opts...)
|
|
|
|
pgn := rdb.Pagination{Size: opt.pageSize, Page: opt.pageNum - 1}
|
2021-11-06 07:52:54 +08:00
|
|
|
infos, err := i.rdb.ListScheduled(qname, pgn)
|
2021-05-19 20:59:30 +08:00
|
|
|
switch {
|
|
|
|
case errors.IsQueueNotFound(err):
|
|
|
|
return nil, fmt.Errorf("asynq: %w", ErrQueueNotFound)
|
|
|
|
case err != nil:
|
2021-05-19 10:13:52 +08:00
|
|
|
return nil, fmt.Errorf("asynq: %v", err)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2021-05-19 10:13:52 +08:00
|
|
|
var tasks []*TaskInfo
|
2021-11-06 07:52:54 +08:00
|
|
|
for _, i := range infos {
|
2021-06-14 22:31:39 +08:00
|
|
|
tasks = append(tasks, newTaskInfo(
|
2021-11-06 07:52:54 +08:00
|
|
|
i.Message,
|
|
|
|
i.State,
|
|
|
|
i.NextProcessAt,
|
|
|
|
i.Result,
|
2021-06-14 22:31:39 +08:00
|
|
|
))
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
return tasks, nil
|
|
|
|
}
|
|
|
|
|
2020-08-17 05:51:56 +08:00
|
|
|
// ListRetryTasks retrieves retry tasks from the specified queue.
|
2021-05-19 10:13:52 +08:00
|
|
|
// Tasks are sorted by NextProcessAt in ascending order.
|
2020-07-13 21:29:41 +08:00
|
|
|
//
|
|
|
|
// By default, it retrieves the first 30 tasks.
|
2021-05-19 10:13:52 +08:00
|
|
|
func (i *Inspector) ListRetryTasks(qname string, opts ...ListOption) ([]*TaskInfo, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2021-05-19 10:13:52 +08:00
|
|
|
return nil, fmt.Errorf("asynq: %v", err)
|
2020-08-31 20:53:17 +08:00
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
opt := composeListOptions(opts...)
|
|
|
|
pgn := rdb.Pagination{Size: opt.pageSize, Page: opt.pageNum - 1}
|
2021-11-06 07:52:54 +08:00
|
|
|
infos, err := i.rdb.ListRetry(qname, pgn)
|
2021-05-19 20:59:30 +08:00
|
|
|
switch {
|
|
|
|
case errors.IsQueueNotFound(err):
|
|
|
|
return nil, fmt.Errorf("asynq: %w", ErrQueueNotFound)
|
|
|
|
case err != nil:
|
2021-05-19 10:13:52 +08:00
|
|
|
return nil, fmt.Errorf("asynq: %v", err)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2021-05-19 10:13:52 +08:00
|
|
|
var tasks []*TaskInfo
|
2021-11-06 07:52:54 +08:00
|
|
|
for _, i := range infos {
|
2021-06-14 22:31:39 +08:00
|
|
|
tasks = append(tasks, newTaskInfo(
|
2021-11-06 07:52:54 +08:00
|
|
|
i.Message,
|
|
|
|
i.State,
|
|
|
|
i.NextProcessAt,
|
|
|
|
i.Result,
|
2021-06-14 22:31:39 +08:00
|
|
|
))
|
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.
|
2021-05-19 10:13:52 +08:00
|
|
|
// Tasks are sorted by LastFailedAt in descending order.
|
2020-07-13 21:29:41 +08:00
|
|
|
//
|
|
|
|
// By default, it retrieves the first 30 tasks.
|
2021-05-19 12:06:53 +08:00
|
|
|
func (i *Inspector) ListArchivedTasks(qname string, opts ...ListOption) ([]*TaskInfo, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2021-05-19 10:13:52 +08:00
|
|
|
return nil, fmt.Errorf("asynq: %v", err)
|
2020-08-31 20:53:17 +08:00
|
|
|
}
|
2020-07-13 21:29:41 +08:00
|
|
|
opt := composeListOptions(opts...)
|
|
|
|
pgn := rdb.Pagination{Size: opt.pageSize, Page: opt.pageNum - 1}
|
2021-11-06 07:52:54 +08:00
|
|
|
infos, err := i.rdb.ListArchived(qname, pgn)
|
2021-05-19 20:59:30 +08:00
|
|
|
switch {
|
|
|
|
case errors.IsQueueNotFound(err):
|
|
|
|
return nil, fmt.Errorf("asynq: %w", ErrQueueNotFound)
|
|
|
|
case err != nil:
|
2021-05-19 10:13:52 +08:00
|
|
|
return nil, fmt.Errorf("asynq: %v", err)
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2021-05-19 12:06:53 +08:00
|
|
|
var tasks []*TaskInfo
|
2021-11-06 07:52:54 +08:00
|
|
|
for _, i := range infos {
|
2021-06-14 22:31:39 +08:00
|
|
|
tasks = append(tasks, newTaskInfo(
|
2021-11-06 07:52:54 +08:00
|
|
|
i.Message,
|
|
|
|
i.State,
|
|
|
|
i.NextProcessAt,
|
|
|
|
i.Result,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
return tasks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListCompletedTasks retrieves completed tasks from the specified queue.
|
|
|
|
// Tasks are sorted by expiration time (i.e. CompletedAt + Retention) in descending order.
|
|
|
|
//
|
|
|
|
// By default, it retrieves the first 30 tasks.
|
|
|
|
func (i *Inspector) ListCompletedTasks(qname string, opts ...ListOption) ([]*TaskInfo, error) {
|
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
|
|
|
return nil, fmt.Errorf("asynq: %v", err)
|
|
|
|
}
|
|
|
|
opt := composeListOptions(opts...)
|
|
|
|
pgn := rdb.Pagination{Size: opt.pageSize, Page: opt.pageNum - 1}
|
|
|
|
infos, err := i.rdb.ListCompleted(qname, pgn)
|
|
|
|
switch {
|
|
|
|
case errors.IsQueueNotFound(err):
|
|
|
|
return nil, fmt.Errorf("asynq: %w", ErrQueueNotFound)
|
|
|
|
case err != nil:
|
|
|
|
return nil, fmt.Errorf("asynq: %v", err)
|
|
|
|
}
|
|
|
|
var tasks []*TaskInfo
|
|
|
|
for _, i := range infos {
|
|
|
|
tasks = append(tasks, newTaskInfo(
|
|
|
|
i.Message,
|
|
|
|
i.State,
|
|
|
|
i.NextProcessAt,
|
|
|
|
i.Result,
|
2021-06-14 22:31:39 +08:00
|
|
|
))
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-06 07:52:54 +08:00
|
|
|
// DeleteAllCompletedTasks deletes all completed tasks from the specified queue,
|
|
|
|
// and reports the number tasks deleted.
|
|
|
|
func (i *Inspector) DeleteAllCompletedTasks(qname string) (int, error) {
|
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
n, err := i.rdb.DeleteAllCompletedTasks(qname)
|
|
|
|
return int(n), err
|
|
|
|
}
|
|
|
|
|
2021-05-19 07:27:16 +08:00
|
|
|
// DeleteTask deletes a task with the given id from the given queue.
|
|
|
|
// The task needs to be in pending, scheduled, retry, or archived state,
|
|
|
|
// otherwise DeleteTask will return an error.
|
|
|
|
//
|
2021-11-10 07:43:04 +08:00
|
|
|
// If a queue with the given name doesn't exist, it returns an error wrapping ErrQueueNotFound.
|
|
|
|
// If a task with the given id doesn't exist in the queue, it returns an error wrapping ErrTaskNotFound.
|
2021-05-19 07:27:16 +08:00
|
|
|
// If the task is in active state, it returns a non-nil error.
|
2021-05-18 07:37:30 +08:00
|
|
|
func (i *Inspector) DeleteTask(qname, id string) error {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2021-05-18 07:37:30 +08:00
|
|
|
return fmt.Errorf("asynq: %v", err)
|
2020-08-31 20:53:17 +08:00
|
|
|
}
|
2021-09-12 12:11:04 +08:00
|
|
|
err := i.rdb.DeleteTask(qname, id)
|
2021-05-18 07:37:30 +08:00
|
|
|
switch {
|
|
|
|
case errors.IsQueueNotFound(err):
|
|
|
|
return fmt.Errorf("asynq: %w", ErrQueueNotFound)
|
|
|
|
case errors.IsTaskNotFound(err):
|
|
|
|
return fmt.Errorf("asynq: %w", ErrTaskNotFound)
|
|
|
|
case err != nil:
|
|
|
|
return fmt.Errorf("asynq: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-16 22:32:47 +08:00
|
|
|
// RunTask updates the task to pending state given a queue name and task id.
|
|
|
|
// The task needs to be in scheduled, retry, or archived state, otherwise RunTask
|
|
|
|
// will return an error.
|
|
|
|
//
|
2021-11-10 07:43:04 +08:00
|
|
|
// If a queue with the given name doesn't exist, it returns an error wrapping ErrQueueNotFound.
|
|
|
|
// If a task with the given id doesn't exist in the queue, it returns an error wrapping ErrTaskNotFound.
|
2021-05-16 22:32:47 +08:00
|
|
|
// If the task is in pending or active state, it returns a non-nil error.
|
|
|
|
func (i *Inspector) RunTask(qname, id string) error {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2021-05-16 22:32:47 +08:00
|
|
|
return fmt.Errorf("asynq: %v", err)
|
2020-08-31 20:53:17 +08:00
|
|
|
}
|
2021-09-12 12:11:04 +08:00
|
|
|
err := i.rdb.RunTask(qname, id)
|
2021-05-16 22:32:47 +08:00
|
|
|
switch {
|
|
|
|
case errors.IsQueueNotFound(err):
|
|
|
|
return fmt.Errorf("asynq: %w", ErrQueueNotFound)
|
|
|
|
case errors.IsTaskNotFound(err):
|
|
|
|
return fmt.Errorf("asynq: %w", ErrTaskNotFound)
|
|
|
|
case err != nil:
|
|
|
|
return fmt.Errorf("asynq: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
|
|
|
|
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-05-19 07:27:16 +08:00
|
|
|
// ArchiveTask archives a task with the given id in the given queue.
|
|
|
|
// The task needs to be in pending, scheduled, or retry state, otherwise ArchiveTask
|
|
|
|
// will return an error.
|
|
|
|
//
|
2021-11-10 07:43:04 +08:00
|
|
|
// If a queue with the given name doesn't exist, it returns an error wrapping ErrQueueNotFound.
|
|
|
|
// If a task with the given id doesn't exist in the queue, it returns an error wrapping ErrTaskNotFound.
|
2021-05-19 07:27:16 +08:00
|
|
|
// If the task is in already archived, it returns a non-nil error.
|
|
|
|
func (i *Inspector) ArchiveTask(qname, id string) error {
|
2021-01-29 00:59:13 +08:00
|
|
|
if err := base.ValidateQueueName(qname); err != nil {
|
2021-05-19 07:27:16 +08:00
|
|
|
return fmt.Errorf("asynq: err")
|
2020-08-31 20:53:17 +08:00
|
|
|
}
|
2021-09-12 12:11:04 +08:00
|
|
|
err := i.rdb.ArchiveTask(qname, id)
|
2021-05-19 07:27:16 +08:00
|
|
|
switch {
|
|
|
|
case errors.IsQueueNotFound(err):
|
|
|
|
return fmt.Errorf("asynq: %w", ErrQueueNotFound)
|
|
|
|
case errors.IsTaskNotFound(err):
|
|
|
|
return fmt.Errorf("asynq: %w", ErrTaskNotFound)
|
|
|
|
case err != nil:
|
|
|
|
return fmt.Errorf("asynq: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-13 21:29:41 +08:00
|
|
|
}
|
2020-07-29 12:56:48 +08:00
|
|
|
|
2021-05-21 07:59:43 +08:00
|
|
|
// CancelProcessing sends a signal to cancel processing of the task
|
|
|
|
// given a task id. CancelProcessing is best-effort, which means that it does not
|
2020-12-05 22:49:01 +08:00
|
|
|
// guarantee that the task with the given id will be canceled. The return
|
|
|
|
// value only indicates whether the cancelation signal has been sent.
|
2021-05-21 07:59:43 +08:00
|
|
|
func (i *Inspector) CancelProcessing(id string) error {
|
2020-12-05 22:49:01 +08:00
|
|
|
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-05-20 07:23:38 +08:00
|
|
|
TaskID: w.ID,
|
|
|
|
TaskType: w.Type,
|
|
|
|
TaskPayload: w.Payload,
|
|
|
|
Queue: w.Queue,
|
|
|
|
Started: w.Started,
|
|
|
|
Deadline: w.Deadline,
|
2020-12-30 23:10:53 +08:00
|
|
|
}
|
|
|
|
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 {
|
2021-05-20 07:23:38 +08:00
|
|
|
// ID of the task the worker is processing.
|
|
|
|
TaskID string
|
|
|
|
// Type of the task the worker is processing.
|
|
|
|
TaskType string
|
|
|
|
// Payload of the task the worker is processing.
|
|
|
|
TaskPayload []byte
|
|
|
|
// Queue from which the worker got its task.
|
|
|
|
Queue string
|
2020-12-30 23:10:53 +08:00
|
|
|
// 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.
|
2021-05-24 11:46:22 +08:00
|
|
|
ID string
|
2020-09-01 21:57:08 +08:00
|
|
|
|
|
|
|
// Address of the node.
|
2021-05-24 11:46:22 +08:00
|
|
|
Addr string
|
2020-09-01 21:57:08 +08:00
|
|
|
}
|
|
|
|
|
2021-01-13 03:01:21 +08:00
|
|
|
// ClusterNodes returns a list of nodes the given queue belongs to.
|
2021-05-20 07:48:38 +08:00
|
|
|
//
|
|
|
|
// Only relevant if task queues are stored in redis cluster.
|
|
|
|
func (i *Inspector) ClusterNodes(qname string) ([]*ClusterNode, error) {
|
2020-09-01 21:57:08 +08:00
|
|
|
nodes, err := i.rdb.ClusterNodes(qname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-20 07:48:38 +08:00
|
|
|
var res []*ClusterNode
|
2020-09-01 21:57:08 +08:00
|
|
|
for _, node := range nodes {
|
2021-05-24 11:46:22 +08:00
|
|
|
res = append(res, &ClusterNode{ID: node.ID, Addr: node.Addr})
|
2020-09-01 21:57:08 +08:00
|
|
|
}
|
|
|
|
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-05-19 09:45:15 +08:00
|
|
|
Task *Task
|
2020-12-01 23:37:09 +08:00
|
|
|
|
|
|
|
// Opts is the options for the periodic task.
|
2021-05-19 09:45:15 +08:00
|
|
|
Opts []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-05-19 09:45:15 +08:00
|
|
|
task := NewTask(e.Type, e.Payload)
|
|
|
|
var opts []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.
|
2021-05-19 09:45:15 +08:00
|
|
|
func parseOption(s string) (Option, error) {
|
2021-01-29 00:59:13 +08:00
|
|
|
fn, arg := parseOptionFunc(s), parseOptionArg(s)
|
|
|
|
switch fn {
|
|
|
|
case "Queue":
|
|
|
|
qname, err := strconv.Unquote(arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-19 09:45:15 +08:00
|
|
|
return Queue(qname), nil
|
2021-01-29 00:59:13 +08:00
|
|
|
case "MaxRetry":
|
|
|
|
n, err := strconv.Atoi(arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-19 09:45:15 +08:00
|
|
|
return MaxRetry(n), nil
|
2021-01-29 00:59:13 +08:00
|
|
|
case "Timeout":
|
|
|
|
d, err := time.ParseDuration(arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-19 09:45:15 +08:00
|
|
|
return Timeout(d), nil
|
2021-01-29 00:59:13 +08:00
|
|
|
case "Deadline":
|
|
|
|
t, err := time.Parse(time.UnixDate, arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-19 09:45:15 +08:00
|
|
|
return Deadline(t), nil
|
2021-01-29 00:59:13 +08:00
|
|
|
case "Unique":
|
|
|
|
d, err := time.ParseDuration(arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-19 09:45:15 +08:00
|
|
|
return Unique(d), nil
|
2021-01-29 00:59:13 +08:00
|
|
|
case "ProcessAt":
|
|
|
|
t, err := time.Parse(time.UnixDate, arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-19 09:45:15 +08:00
|
|
|
return ProcessAt(t), nil
|
2021-01-29 00:59:13 +08:00
|
|
|
case "ProcessIn":
|
|
|
|
d, err := time.ParseDuration(arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-19 09:45:15 +08:00
|
|
|
return ProcessIn(d), nil
|
2021-11-06 07:52:54 +08:00
|
|
|
case "Retention":
|
|
|
|
d, err := time.ParseDuration(arg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return Retention(d), nil
|
2021-01-29 00:59:13 +08:00
|
|
|
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
|
|
|
|
}
|