2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-25 23:32:17 +08:00

Rename error types

This commit is contained in:
Ken Hibino 2021-05-01 06:47:49 -07:00
parent 1db516c53c
commit 76486b5cb4
3 changed files with 36 additions and 36 deletions

View File

@ -5,6 +5,7 @@
package inspeq
import (
"errors"
"fmt"
"strconv"
"strings"
@ -134,23 +135,12 @@ func (i *Inspector) History(qname string, n int) ([]*DailyStats, error) {
return res, nil
}
// 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)
}
var (
// ErrQueueNotFound indicates that the specified queue does not exist.
ErrQueueNotFound = errors.New("queue not found")
// ErrQueueNotEmpty indicates that the specified queue is not empty.
ErrQueueNotEmpty = errors.New("queue is not empty")
)
// DeleteQueue removes the specified queue.
//
@ -164,11 +154,11 @@ func (e *ErrQueueNotEmpty) Error() string {
// 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.QueueNotFoundError); ok {
return fmt.Errorf("%w: queue=%q", ErrQueueNotFound, qname)
}
if _, ok := err.(*rdb.ErrQueueNotEmpty); ok {
return &ErrQueueNotEmpty{qname}
if _, ok := err.(*rdb.QueueNotEmptyError); ok {
return fmt.Errorf("%w: queue=%q", ErrQueueNotEmpty, qname)
}
return err
}

View File

@ -105,7 +105,7 @@ func (r *RDB) CurrentStats(qname string) (*Stats, error) {
return nil, err
}
if !exists {
return nil, &ErrQueueNotFound{qname}
return nil, &QueueNotFoundError{qname}
}
now := time.Now()
res, err := currentStatsCmd.Run(r.client, []string{
@ -219,7 +219,7 @@ func (r *RDB) HistoricalStats(qname string, n int) ([]*DailyStats, error) {
return nil, err
}
if !exists {
return nil, &ErrQueueNotFound{qname}
return nil, &QueueNotFoundError{qname}
}
const day = 24 * time.Hour
now := time.Now().UTC()
@ -594,6 +594,9 @@ func (r *RDB) ArchiveAllPendingTasks(qname string) (int64, error) {
return n, nil
}
// archiveTaskCmd is a Lua script that arhives a task given a task id.
//
// Input:
// KEYS[1] -> task key (asynq:{<qname>}:t:<task_id>)
// KEYS[2] -> archived key (asynq:{<qname>}:archived)
// ARGV[1] -> id of the task to archive
@ -601,6 +604,9 @@ func (r *RDB) ArchiveAllPendingTasks(qname string) (int64, error) {
// ARGV[3] -> cutoff timestamp (e.g., 90 days ago)
// ARGV[4] -> max number of tasks in archived state (e.g., 100)
// ARGV[5] -> queue key prefix (asynq:{<qname>}:)
//
// Output:
// TODO: document return value of the script
var archiveTaskCmd = redis.NewScript(`
if redis.call("EXISTS", KEYS[1]) == 0 then
return 0
@ -826,22 +832,26 @@ func (r *RDB) DeleteAllPendingTasks(qname string) (int64, error) {
return n, nil
}
// ErrQueueNotFound indicates specified queue does not exist.
type ErrQueueNotFound struct {
qname string
// QueueNotFoundError indicates specified queue does not exist.
type QueueNotFoundError struct {
Name string // name of the queue
}
func (e *ErrQueueNotFound) Error() string {
return fmt.Sprintf("queue %q does not exist", e.qname)
func (e *QueueNotFoundError) Unwrap() error { return base.ErrNotFound }
func (e *QueueNotFoundError) Error() string {
return fmt.Sprintf("queue %q does not exist", e.Name)
}
// ErrQueueNotEmpty indicates specified queue is not empty.
type ErrQueueNotEmpty struct {
qname string
// QueueNotEmptyError indicates specified queue is not empty.
type QueueNotEmptyError struct {
Name string // name of the queue
}
func (e *ErrQueueNotEmpty) Error() string {
return fmt.Sprintf("queue %q is not empty", e.qname)
func (e *QueueNotEmptyError) Unwrap() error { return base.ErrFailedPrecondition }
func (e *QueueNotEmptyError) Error() string {
return fmt.Sprintf("queue %q is not empty", e.Name)
}
// Only check whether active queue is empty before removing.
@ -931,7 +941,7 @@ func (r *RDB) RemoveQueue(qname string, force bool) error {
return err
}
if !exists {
return &ErrQueueNotFound{qname}
return &QueueNotFoundError{qname}
}
var script *redis.Script
if force {
@ -949,7 +959,7 @@ func (r *RDB) RemoveQueue(qname string, force bool) error {
}
if err := script.Run(r.client, keys, base.TaskKeyPrefix(qname)).Err(); err != nil {
if err.Error() == "QUEUE NOT EMPTY" {
return &ErrQueueNotEmpty{qname}
return &QueueNotEmptyError{qname}
}
return err
}

View File

@ -224,7 +224,7 @@ func TestCurrentStatsWithNonExistentQueue(t *testing.T) {
qname := "non-existent"
got, err := r.CurrentStats(qname)
if err == nil {
t.Fatalf("r.CurrentStats(%q) = %v, %v, want nil, %v", qname, got, err, &ErrQueueNotFound{qname})
t.Fatalf("r.CurrentStats(%q) = %v, %v, want nil, %v", qname, got, err, &QueueNotFoundError{qname})
}
}