mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-08 08:36:37 +08:00
refactor(core): decouple gin from pkg/util and reduce code duplication
This commit is contained in:
@@ -139,34 +139,7 @@ func (p *Plugin) Start(_ context.Context) error {
|
||||
}
|
||||
|
||||
if p.scheduler == nil {
|
||||
opts := p.schedulerOpts
|
||||
if opts == nil {
|
||||
opts = &asynq.SchedulerOpts{
|
||||
Location: p.location,
|
||||
}
|
||||
} else if opts.Location == nil && p.location != nil {
|
||||
opts.Location = p.location
|
||||
}
|
||||
|
||||
opt := p.redisOpt
|
||||
if opt == nil {
|
||||
if RedisOpt != nil {
|
||||
opt = RedisOpt
|
||||
} else {
|
||||
redisCfg := config.Config.Redis
|
||||
addr := "127.0.0.1:6379"
|
||||
if len(redisCfg.Addrs) > 0 && redisCfg.Addrs[0] != "" {
|
||||
addr = redisCfg.Addrs[0]
|
||||
}
|
||||
opt = asynq.RedisClientOpt{
|
||||
Addr: addr,
|
||||
Username: redisCfg.Username,
|
||||
Password: redisCfg.Password,
|
||||
DB: redisCfg.DB,
|
||||
}
|
||||
}
|
||||
}
|
||||
p.scheduler = asynq.NewScheduler(opt, opts)
|
||||
p.scheduler = p.initScheduler()
|
||||
}
|
||||
|
||||
if p.coreCtx != nil && p.coreCtx.Schedules() != nil {
|
||||
@@ -268,3 +241,37 @@ func buildAsynqOptions(opts map[string]any) []asynq.Option {
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func (p *Plugin) initScheduler() *asynq.Scheduler {
|
||||
opts := p.schedulerOpts
|
||||
if opts == nil {
|
||||
opts = &asynq.SchedulerOpts{
|
||||
Location: p.location,
|
||||
}
|
||||
} else if opts.Location == nil && p.location != nil {
|
||||
opts.Location = p.location
|
||||
}
|
||||
|
||||
opt := p.resolveRedisOpt()
|
||||
return asynq.NewScheduler(opt, opts)
|
||||
}
|
||||
|
||||
func (p *Plugin) resolveRedisOpt() asynq.RedisConnOpt {
|
||||
if p.redisOpt != nil {
|
||||
return p.redisOpt
|
||||
}
|
||||
if RedisOpt != nil {
|
||||
return RedisOpt
|
||||
}
|
||||
redisCfg := config.Config.Redis
|
||||
addr := "127.0.0.1:6379"
|
||||
if len(redisCfg.Addrs) > 0 && redisCfg.Addrs[0] != "" {
|
||||
addr = redisCfg.Addrs[0]
|
||||
}
|
||||
return asynq.RedisClientOpt{
|
||||
Addr: addr,
|
||||
Username: redisCfg.Username,
|
||||
Password: redisCfg.Password,
|
||||
DB: redisCfg.DB,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,21 +111,7 @@ func ReloadScheduler() error {
|
||||
// 4. 遍历并注册任务
|
||||
taskSvc := getTaskService()
|
||||
for _, s := range schedules {
|
||||
taskName := s.TaskType
|
||||
maxRetry := 3
|
||||
queue := "default"
|
||||
|
||||
if taskSvc != nil {
|
||||
if meta, ok := taskSvc.GetTaskMeta(s.TaskType); ok {
|
||||
taskName = meta.Name
|
||||
if meta.MaxRetry > 0 {
|
||||
maxRetry = meta.MaxRetry
|
||||
}
|
||||
if meta.Queue != "" {
|
||||
queue = meta.Queue
|
||||
}
|
||||
}
|
||||
}
|
||||
taskName, maxRetry, queue := resolveTaskScheduleMeta(taskSvc, s.TaskType)
|
||||
|
||||
// 构造 Asynq 载荷
|
||||
t := asynq.NewTask(taskName, []byte(s.Payload))
|
||||
@@ -160,3 +146,24 @@ func waitForStop(done, signals <-chan struct{}) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func resolveTaskScheduleMeta(taskSvc contracts.TaskService, taskType string) (name string, maxRetry int, queue string) {
|
||||
name = taskType
|
||||
maxRetry = 3
|
||||
queue = "default"
|
||||
if taskSvc == nil {
|
||||
return
|
||||
}
|
||||
meta, ok := taskSvc.GetTaskMeta(taskType)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
name = meta.Name
|
||||
if meta.MaxRetry > 0 {
|
||||
maxRetry = meta.MaxRetry
|
||||
}
|
||||
if meta.Queue != "" {
|
||||
queue = meta.Queue
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user