mirror of
https://github.com/hibiken/asynq.git
synced 2025-10-25 23:06:12 +08:00
feat: add custom unique key option
This commit is contained in:
32
client.go
32
client.go
@@ -44,6 +44,7 @@ const (
|
||||
TimeoutOpt
|
||||
DeadlineOpt
|
||||
UniqueOpt
|
||||
UniqueKeyOpt
|
||||
ProcessAtOpt
|
||||
ProcessInOpt
|
||||
TaskIDOpt
|
||||
@@ -71,6 +72,7 @@ type (
|
||||
timeoutOption time.Duration
|
||||
deadlineOption time.Time
|
||||
uniqueOption time.Duration
|
||||
uniqueKeyOption string
|
||||
processAtOption time.Time
|
||||
processInOption time.Duration
|
||||
retentionOption time.Duration
|
||||
@@ -149,10 +151,11 @@ func (t deadlineOption) Value() interface{} { return time.Time(t) }
|
||||
// ErrDuplicateTask error is returned when enqueueing a duplicate task.
|
||||
// TTL duration must be greater than or equal to 1 second.
|
||||
//
|
||||
// Uniqueness of a task is based on the following properties:
|
||||
// By default, the uniqueness of a task is based on the following properties:
|
||||
// - Task Type
|
||||
// - Task Payload
|
||||
// - Queue Name
|
||||
// UniqueKey can be used to specify a custom string for calculating uniqueness, instead of task payload.
|
||||
func Unique(ttl time.Duration) Option {
|
||||
return uniqueOption(ttl)
|
||||
}
|
||||
@@ -161,6 +164,24 @@ func (ttl uniqueOption) String() string { return fmt.Sprintf("Unique(%v)", t
|
||||
func (ttl uniqueOption) Type() OptionType { return UniqueOpt }
|
||||
func (ttl uniqueOption) Value() interface{} { return time.Duration(ttl) }
|
||||
|
||||
// UniqueKey returns an option to define the custom uniqueness of a task.
|
||||
// If uniqueKey is not empty, the uniqueness of a task is based on the following properties:
|
||||
// - Task Type
|
||||
// - UniqueKey
|
||||
// - Queue Name
|
||||
// Otherwise, task payload will be used, see Unique.
|
||||
//
|
||||
// UniqueKey should be used together with Unique.
|
||||
func UniqueKey(uniqueKey string) Option {
|
||||
return uniqueKeyOption(uniqueKey)
|
||||
}
|
||||
|
||||
func (uniqueKey uniqueKeyOption) String() string {
|
||||
return fmt.Sprintf("UniqueKey(%q)", string(uniqueKey))
|
||||
}
|
||||
func (uniqueKey uniqueKeyOption) Type() OptionType { return UniqueKeyOpt }
|
||||
func (uniqueKey uniqueKeyOption) Value() interface{} { return string(uniqueKey) }
|
||||
|
||||
// ProcessAt returns an option to specify when to process the given task.
|
||||
//
|
||||
// If there's a conflicting ProcessIn option, the last option passed to Enqueue overrides the others.
|
||||
@@ -223,6 +244,7 @@ type option struct {
|
||||
timeout time.Duration
|
||||
deadline time.Time
|
||||
uniqueTTL time.Duration
|
||||
uniqueKey string
|
||||
processAt time.Time
|
||||
retention time.Duration
|
||||
group string
|
||||
@@ -267,6 +289,8 @@ func composeOptions(opts ...Option) (option, error) {
|
||||
return option{}, errors.New("Unique TTL cannot be less than 1s")
|
||||
}
|
||||
res.uniqueTTL = ttl
|
||||
case uniqueKeyOption:
|
||||
res.uniqueKey = string(opt)
|
||||
case processAtOption:
|
||||
res.processAt = time.Time(opt)
|
||||
case processInOption:
|
||||
@@ -365,7 +389,11 @@ func (c *Client) EnqueueContext(ctx context.Context, task *Task, opts ...Option)
|
||||
}
|
||||
var uniqueKey string
|
||||
if opt.uniqueTTL > 0 {
|
||||
uniqueKey = base.UniqueKey(opt.queue, task.Type(), task.Payload())
|
||||
if opt.uniqueKey != "" {
|
||||
uniqueKey = base.CustomUniqueKey(opt.queue, task.Type(), opt.uniqueKey)
|
||||
} else {
|
||||
uniqueKey = base.UniqueKey(opt.queue, task.Type(), task.Payload())
|
||||
}
|
||||
}
|
||||
msg := &base.TaskMessage{
|
||||
ID: opt.taskID,
|
||||
|
||||
Reference in New Issue
Block a user