mirror of
https://github.com/hibiken/asynq.git
synced 2024-12-27 08:12:19 +08:00
Update RDB.CurrentStats and RDB.HistoricalStats with specific errors
This commit is contained in:
parent
76bd865ebc
commit
d2d4029aba
@ -101,12 +101,13 @@ return res`)
|
|||||||
|
|
||||||
// CurrentStats returns a current state of the queues.
|
// CurrentStats returns a current state of the queues.
|
||||||
func (r *RDB) CurrentStats(qname string) (*Stats, error) {
|
func (r *RDB) CurrentStats(qname string) (*Stats, error) {
|
||||||
|
var op errors.Op = "rdb.CurrentStats"
|
||||||
exists, err := r.client.SIsMember(base.AllQueues, qname).Result()
|
exists, err := r.client.SIsMember(base.AllQueues, qname).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.E(op, errors.Unknown, err)
|
||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
return nil, &QueueNotFoundError{qname}
|
return nil, errors.E(op, errors.NotFound, &errors.QueueNotFoundError{Queue: qname})
|
||||||
}
|
}
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
res, err := currentStatsCmd.Run(r.client, []string{
|
res, err := currentStatsCmd.Run(r.client, []string{
|
||||||
@ -120,11 +121,11 @@ func (r *RDB) CurrentStats(qname string) (*Stats, error) {
|
|||||||
base.PausedKey(qname),
|
base.PausedKey(qname),
|
||||||
}).Result()
|
}).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.E(op, errors.Unknown, err)
|
||||||
}
|
}
|
||||||
data, err := cast.ToSliceE(res)
|
data, err := cast.ToSliceE(res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.E(op, errors.Internal, "cast error: unexpected return value from Lua script")
|
||||||
}
|
}
|
||||||
stats := &Stats{
|
stats := &Stats{
|
||||||
Queue: qname,
|
Queue: qname,
|
||||||
@ -165,13 +166,14 @@ func (r *RDB) CurrentStats(qname string) (*Stats, error) {
|
|||||||
stats.Size = size
|
stats.Size = size
|
||||||
memusg, err := r.memoryUsage(qname)
|
memusg, err := r.memoryUsage(qname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.E(op, errors.CanonicalCode(err), err)
|
||||||
}
|
}
|
||||||
stats.MemoryUsage = memusg
|
stats.MemoryUsage = memusg
|
||||||
return stats, nil
|
return stats, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RDB) memoryUsage(qname string) (int64, error) {
|
func (r *RDB) memoryUsage(qname string) (int64, error) {
|
||||||
|
var op errors.Op = "rdb.memoryUsage"
|
||||||
var (
|
var (
|
||||||
keys []string
|
keys []string
|
||||||
data []string
|
data []string
|
||||||
@ -181,7 +183,7 @@ func (r *RDB) memoryUsage(qname string) (int64, error) {
|
|||||||
for {
|
for {
|
||||||
data, cursor, err = r.client.Scan(cursor, fmt.Sprintf("asynq:{%s}*", qname), 100).Result()
|
data, cursor, err = r.client.Scan(cursor, fmt.Sprintf("asynq:{%s}*", qname), 100).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, errors.E(op, errors.Unknown, fmt.Sprintf("redis command error: SCAN failed: %v", err))
|
||||||
}
|
}
|
||||||
keys = append(keys, data...)
|
keys = append(keys, data...)
|
||||||
if cursor == 0 {
|
if cursor == 0 {
|
||||||
@ -192,7 +194,7 @@ func (r *RDB) memoryUsage(qname string) (int64, error) {
|
|||||||
for _, k := range keys {
|
for _, k := range keys {
|
||||||
n, err := r.client.MemoryUsage(k).Result()
|
n, err := r.client.MemoryUsage(k).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, errors.E(op, errors.Unknown, fmt.Sprintf("redis command error: MEMORY USAGE failed: %v", err))
|
||||||
}
|
}
|
||||||
usg += n
|
usg += n
|
||||||
}
|
}
|
||||||
@ -212,15 +214,16 @@ return res`)
|
|||||||
|
|
||||||
// HistoricalStats returns a list of stats from the last n days for the given queue.
|
// HistoricalStats returns a list of stats from the last n days for the given queue.
|
||||||
func (r *RDB) HistoricalStats(qname string, n int) ([]*DailyStats, error) {
|
func (r *RDB) HistoricalStats(qname string, n int) ([]*DailyStats, error) {
|
||||||
|
var op errors.Op = "rdb.HistoricalStats"
|
||||||
if n < 1 {
|
if n < 1 {
|
||||||
return nil, fmt.Errorf("the number of days must be positive")
|
return nil, errors.E(op, errors.FailedPrecondition, "the number of days must be positive")
|
||||||
}
|
}
|
||||||
exists, err := r.client.SIsMember(base.AllQueues, qname).Result()
|
exists, err := r.client.SIsMember(base.AllQueues, qname).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.E(op, errors.Unknown, fmt.Sprintf("redis command error: SISMEMBER failed: %v", err))
|
||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
return nil, &QueueNotFoundError{qname}
|
return nil, errors.E(op, errors.NotFound, &errors.QueueNotFoundError{Queue: qname})
|
||||||
}
|
}
|
||||||
const day = 24 * time.Hour
|
const day = 24 * time.Hour
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
@ -234,11 +237,11 @@ func (r *RDB) HistoricalStats(qname string, n int) ([]*DailyStats, error) {
|
|||||||
}
|
}
|
||||||
res, err := historicalStatsCmd.Run(r.client, keys).Result()
|
res, err := historicalStatsCmd.Run(r.client, keys).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.E(op, errors.Unknown, fmt.Sprintf("redis eval error: %v", err))
|
||||||
}
|
}
|
||||||
data, err := cast.ToIntSliceE(res)
|
data, err := cast.ToIntSliceE(res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.E(op, errors.Internal, fmt.Sprintf("cast error: unexpected return value from Lua script: %v", res))
|
||||||
}
|
}
|
||||||
var stats []*DailyStats
|
var stats []*DailyStats
|
||||||
for i := 0; i < len(data); i += 2 {
|
for i := 0; i < len(data); i += 2 {
|
||||||
@ -1106,28 +1109,6 @@ func (r *RDB) DeleteAllPendingTasks(qname string) (int64, error) {
|
|||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueueNotFoundError indicates specified queue does not exist.
|
|
||||||
type QueueNotFoundError struct {
|
|
||||||
Name string // name of the queue
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *QueueNotFoundError) Unwrap() error { return base.ErrNotFound }
|
|
||||||
|
|
||||||
func (e *QueueNotFoundError) Error() string {
|
|
||||||
return fmt.Sprintf("queue %q does not exist", e.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
// QueueNotEmptyError indicates specified queue is not empty.
|
|
||||||
type QueueNotEmptyError struct {
|
|
||||||
Name string // name of the queue
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *QueueNotEmptyError) Unwrap() error { return base.ErrFailedPrecondition }
|
|
||||||
|
|
||||||
func (e *QueueNotEmptyError) Error() string {
|
|
||||||
return fmt.Sprintf("queue %q is not empty", e.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
// removeQueueForceCmd removes the given queue regardless of
|
// removeQueueForceCmd removes the given queue regardless of
|
||||||
// whether the queue is empty.
|
// whether the queue is empty.
|
||||||
// It only check whether active queue is empty before removing.
|
// It only check whether active queue is empty before removing.
|
||||||
|
@ -224,8 +224,8 @@ func TestCurrentStatsWithNonExistentQueue(t *testing.T) {
|
|||||||
|
|
||||||
qname := "non-existent"
|
qname := "non-existent"
|
||||||
got, err := r.CurrentStats(qname)
|
got, err := r.CurrentStats(qname)
|
||||||
if err == nil {
|
if !errors.IsQueueNotFound(err) {
|
||||||
t.Fatalf("r.CurrentStats(%q) = %v, %v, want nil, %v", qname, got, err, &QueueNotFoundError{qname})
|
t.Fatalf("r.CurrentStats(%q) = %v, %v, want nil, %v", qname, got, err, &errors.QueueNotFoundError{Queue: qname})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user