Merge pull request #1 from Selly-Modules/update-config-task-timeout

[Update] Add task timeout
This commit is contained in:
Nam Huynh 2022-02-16 17:58:09 +07:00 committed by GitHub
commit 4b5842b59d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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...)
}