From cb6f1c515b5ca59237d166efb5b37ed033bc693b Mon Sep 17 00:00:00 2001 From: Sinh Date: Fri, 18 Nov 2022 17:04:42 +0700 Subject: [PATCH] add middleware and queue inspector --- config.go | 5 +++-- instance.go | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 780dbec..85ae60f 100644 --- a/config.go +++ b/config.go @@ -17,8 +17,9 @@ type Config struct { Concurrency int Priority ConfigPriority - TaskTimeout time.Duration - RetryDelayFunc asynq.RetryDelayFunc + TaskTimeout time.Duration + RetryDelayFunc asynq.RetryDelayFunc + ServerMiddlewares []asynq.MiddlewareFunc } // ConfigRedis ... diff --git a/instance.go b/instance.go index 0f12628..44112fd 100644 --- a/instance.go +++ b/instance.go @@ -12,6 +12,7 @@ type Instance struct { Client *asynq.Client Server *asynq.ServeMux Scheduler *asynq.Scheduler + Inspector *asynq.Inspector Config Config } @@ -31,6 +32,7 @@ func NewInstance(cfg Config) Instance { instance.Server = initServer(redisConn, cfg) instance.Scheduler = initScheduler(redisConn) instance.Client = initClient(redisConn) + instance.Inspector = initInspector(redisConn) instance.Config = cfg // Return instance @@ -73,6 +75,9 @@ func initServer(redisConn asynq.RedisClientOpt, cfg Config) *asynq.ServeMux { // Init mux server mux := asynq.NewServeMux() + if len(cfg.ServerMiddlewares) > 0 { + mux.Use(cfg.ServerMiddlewares...) + } // Run server go func() { @@ -113,6 +118,10 @@ func initClient(redisConn asynq.RedisClientOpt) *asynq.Client { return client } +func initInspector(redisConn asynq.RedisClientOpt) *asynq.Inspector { + return asynq.NewInspector(redisConn) +} + // GetInstance ... func GetInstance() Instance { return instance