[Update] Add task timeout #1

Merged
trunglt251292 merged 3 commits from update-config-task-timeout into master 2022-02-16 10:58:09 +00:00
3 changed files with 17 additions and 0 deletions

View File

@ -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 ...

View File

@ -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
View File

@ -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...)
}