Merge pull request #1 from Selly-Modules/update-config-task-timeout
[Update] Add task timeout
This commit is contained in:
commit
4b5842b59d
|
@ -1,5 +1,7 @@
|
|||
package queue
|
||||
|
||||
import "time"
|
||||
|
||||
// Config ...
|
||||
type Config struct {
|
||||
// For message queue
|
||||
|
@ -10,6 +12,8 @@ type Config struct {
|
|||
// https://github.com/hibiken/asynq/wiki/Queue-Priority
|
||||
Concurrency int
|
||||
Priority ConfigPriority
|
||||
|
||||
TaskTimeout time.Duration
|
||||
}
|
||||
|
||||
// ConfigRedis ...
|
||||
|
|
|
@ -12,6 +12,8 @@ type Instance struct {
|
|||
Client *asynq.Client
|
||||
Server *asynq.ServeMux
|
||||
Scheduler *asynq.Scheduler
|
||||
|
||||
Config Config
|
||||
}
|
||||
|
||||
var instance Instance
|
||||
|
@ -29,6 +31,7 @@ func NewInstance(cfg Config) Instance {
|
|||
instance.Server = initServer(redisConn, cfg)
|
||||
instance.Scheduler = initScheduler(redisConn)
|
||||
instance.Client = initClient(redisConn)
|
||||
instance.Config = cfg
|
||||
|
||||
// Return instance
|
||||
return instance
|
||||
|
|
10
task.go
10
task.go
|
@ -2,6 +2,11 @@ package queue
|
|||
|
||||
import (
|
||||
"github.com/hibiken/asynq"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
NoTimeout time.Duration = 0
|
||||
)
|
||||
|
||||
// RunTask ...
|
||||
|
@ -22,6 +27,11 @@ func (i Instance) RunTask(typename string, payload []byte, priority string, retr
|
|||
}
|
||||
options = append(options, asynq.MaxRetry(retryTimes))
|
||||
|
||||
// Task timeout
|
||||
if i.Config.TaskTimeout > 0 {
|
||||
options = append(options, asynq.Timeout(i.Config.TaskTimeout))
|
||||
}
|
||||
|
||||
// Enqueue task
|
||||
return i.Client.Enqueue(task, options...)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue