[Update] Add task timeout

This commit is contained in:
trunglt251292 2022-02-16 17:47:57 +07:00
parent b9eb0a2a87
commit 1e8c775bb6
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
Configs 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.Configs = 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.Configs.TaskTimeout != 0 {
options = append(options, asynq.Timeout(i.Configs.TaskTimeout))
}
// Enqueue task
return i.Client.Enqueue(task, options...)
}