2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-27 00:02:19 +08:00

Update RDB.ArchiveTask with custom errors

This commit is contained in:
Ken Hibino 2021-05-02 06:47:32 -07:00
parent 807624e7dd
commit 12a90f6a8d
2 changed files with 13 additions and 10 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/go-redis/redis/v7" "github.com/go-redis/redis/v7"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/hibiken/asynq/internal/base" "github.com/hibiken/asynq/internal/base"
"github.com/hibiken/asynq/internal/errors"
"github.com/spf13/cast" "github.com/spf13/cast"
) )
@ -636,11 +637,12 @@ return 1
// ArchiveTask finds a task that matches the id from the given queue and archives it. // ArchiveTask finds a task that matches the id from the given queue and archives it.
// It returns nil if it successfully archived the task. // It returns nil if it successfully archived the task.
// //
// If a queue with the given name doesn't exist, it returns ErrQueueNotFound. // If a queue with the given name doesn't exist, it returns QueueNotFoundError.
// If a task with the given id doesn't exist in the queue, it returns ErrTaskNotFound // If a task with the given id doesn't exist in the queue, it returns TaskNotFoundError
// If a task is already archived, it returns ErrTaskAlreadyArchived. // If a task is already archived, it returns TaskAlreadyArchivedError.
// If a task is in active state it returns non-nil error. // If a task is in active state it returns non-nil error.
func (r *RDB) ArchiveTask(qname string, id uuid.UUID) error { func (r *RDB) ArchiveTask(qname string, id uuid.UUID) error {
var op errors.Op = "rdb.ArchiveTask"
keys := []string{ keys := []string{
base.TaskKey(qname, id.String()), base.TaskKey(qname, id.String()),
base.ArchivedKey(qname), base.ArchivedKey(qname),
@ -657,25 +659,25 @@ func (r *RDB) ArchiveTask(qname string, id uuid.UUID) error {
} }
res, err := archiveTaskCmd.Run(r.client, keys, argv...).Result() res, err := archiveTaskCmd.Run(r.client, keys, argv...).Result()
if err != nil { if err != nil {
return err return errors.E(op, errors.Unknown, err)
} }
n, ok := res.(int64) n, ok := res.(int64)
if !ok { if !ok {
return fmt.Errorf("%w: could not cast %v to int64", base.ErrInternal, res) return errors.E(op, errors.Internal, fmt.Sprintf("could not cast the return value %v from archiveTaskCmd to int64.", res))
} }
switch n { switch n {
case 1: case 1:
return nil return nil
case 0: case 0:
return ErrTaskNotFound return errors.E(op, errors.NotFound, &errors.TaskNotFoundError{Queue: qname, ID: id.String()})
case -1: case -1:
return ErrTaskAlreadyArchived return errors.E(op, errors.FailedPrecondition, &errors.TaskAlreadyArchivedError{Queue: qname, ID: id.String()})
case -2: case -2:
return fmt.Errorf("%w: cannot archive task in active state. use CancelTask instead.", base.ErrFailedPrecondition) return errors.E(op, errors.FailedPrecondition, "cannot archive task in active state. use CancelTask instead.")
case -3: case -3:
return ErrQueueNotFound return errors.E(op, errors.NotFound, &errors.QueueNotFoundError{Queue: qname})
default: default:
return fmt.Errorf("%w: unexpected return value from archiveTaskCmd script: %d", base.ErrInternal, n) return errors.E(op, errors.Internal, fmt.Sprintf("unexpected return value from archiveTaskCmd script: %d", n))
} }
} }

View File

@ -15,6 +15,7 @@ import (
"github.com/spf13/cast" "github.com/spf13/cast"
) )
// TODO: remove this & use internal/errors package instead.
var ( var (
// ErrNoProcessableTask indicates that there are no tasks ready to be processed. // ErrNoProcessableTask indicates that there are no tasks ready to be processed.
ErrNoProcessableTask = errors.New("no tasks are ready for processing") ErrNoProcessableTask = errors.New("no tasks are ready for processing")