From 29b922e54530854f5560873bc815b94f63062f0e Mon Sep 17 00:00:00 2001 From: Xijun Dai Date: Thu, 7 Nov 2024 00:58:02 +0800 Subject: [PATCH] feat(periodic_task_manager): Add RedisUniversalClient support Signed-off-by: Xijun Dai --- periodic_task_manager.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/periodic_task_manager.go b/periodic_task_manager.go index 3b927ddb..a02620e7 100644 --- a/periodic_task_manager.go +++ b/periodic_task_manager.go @@ -10,6 +10,8 @@ import ( "sort" "sync" "time" + + "github.com/redis/go-redis/v9" ) // PeriodicTaskManager manages scheduling of periodic tasks. @@ -27,9 +29,12 @@ type PeriodicTaskManagerOpts struct { // Required: must be non nil PeriodicTaskConfigProvider PeriodicTaskConfigProvider - // Required: must be non nil + // Optional: if RedisUniversalClient is nil must be non nil RedisConnOpt RedisConnOpt + // Optional: if RedisUniversalClient is non nil, RedisConnOpt is ignored. + RedisUniversalClient redis.UniversalClient + // Optional: scheduler options *SchedulerOpts @@ -45,10 +50,16 @@ func NewPeriodicTaskManager(opts PeriodicTaskManagerOpts) (*PeriodicTaskManager, if opts.PeriodicTaskConfigProvider == nil { return nil, fmt.Errorf("PeriodicTaskConfigProvider cannot be nil") } - if opts.RedisConnOpt == nil { - return nil, fmt.Errorf("RedisConnOpt cannot be nil") + if opts.RedisConnOpt == nil && opts.RedisUniversalClient == nil { + return nil, fmt.Errorf("RedisConnOpt/RedisUniversalClient cannot be nil") } - scheduler := NewScheduler(opts.RedisConnOpt, opts.SchedulerOpts) + var scheduler *Scheduler + if opts.RedisUniversalClient != nil { + scheduler = NewSchedulerFromRedisClient(opts.RedisUniversalClient, opts.SchedulerOpts) + } else { + scheduler = NewScheduler(opts.RedisConnOpt, opts.SchedulerOpts) + } + syncInterval := opts.SyncInterval if syncInterval == 0 { syncInterval = defaultSyncInterval