优化一些功能
This commit is contained in:
@@ -43,7 +43,7 @@ func (a *App) SwitchActivity(id string) types.SwitchActivityResult {
|
||||
return types.SwitchActivityResult{Error: "配置保存失败: " + err.Error()}
|
||||
}
|
||||
a.setCfg(cfg)
|
||||
newClient := client.New(act, cfg.TimeAPIURL, a.trace)
|
||||
newClient := client.New(act, cfg.TimeAPIURL, a.getTrace())
|
||||
a.setClient(newClient)
|
||||
if !a.scheduler.UpdateClient(newClient) {
|
||||
logger.LogEvent("活动", "warn", "抢购任务正在运行,调度器客户端将在下次抢购时生效")
|
||||
@@ -62,6 +62,24 @@ func (a *App) GetSettings() types.Config {
|
||||
|
||||
// SaveSettings 保存配置并运行时生效。
|
||||
func (a *App) SaveSettings(cfg types.Config) types.SimpleResult {
|
||||
oldCfg := a.getCfg()
|
||||
if cfg.Activity == "" {
|
||||
cfg.Activity = oldCfg.Activity
|
||||
}
|
||||
act := activity.Get(cfg.Activity)
|
||||
if act == nil {
|
||||
unknownActivity := cfg.Activity
|
||||
act = activity.Default()
|
||||
cfg.Activity = act.ID
|
||||
logger.LogEvent("配置", "warn", "保存设置时活动 %q 未识别,已回退到默认活动 %s", unknownActivity, act.DisplayName)
|
||||
}
|
||||
if cfg.TimeAPIURL == "" {
|
||||
cfg.TimeAPIURL = constants.DefaultTimeAPI
|
||||
}
|
||||
if cfg.TraceMaxBody < 0 {
|
||||
cfg.TraceMaxBody = 0
|
||||
}
|
||||
|
||||
// 校验
|
||||
if cfg.Concurrency <= 0 {
|
||||
cfg.Concurrency = constants.DefaultConcurrency
|
||||
@@ -79,10 +97,28 @@ func (a *App) SaveSettings(cfg types.Config) types.SimpleResult {
|
||||
return types.SimpleResult{}
|
||||
}
|
||||
|
||||
traceChanged := cfg.TraceEnabled != oldCfg.TraceEnabled || cfg.TraceMaxBody != oldCfg.TraceMaxBody
|
||||
clientChanged := traceChanged || cfg.TimeAPIURL != oldCfg.TimeAPIURL || cfg.Activity != oldCfg.Activity
|
||||
if traceChanged {
|
||||
a.replaceTrace(cfg)
|
||||
}
|
||||
|
||||
// 运行时生效
|
||||
a.setCfg(cfg)
|
||||
logger.SetMinLevel(cfg.LogLevel)
|
||||
a.scheduler.UpdateSettings(cfg.Concurrency, cfg.Attempts, cfg.RetryStages, cfg.SeckillMode)
|
||||
if clientChanged {
|
||||
newClient := client.New(act, cfg.TimeAPIURL, a.getTrace())
|
||||
a.setClient(newClient)
|
||||
if !a.scheduler.UpdateClient(newClient) {
|
||||
logger.LogEvent("配置", "warn", "抢购任务正在运行,客户端配置将在下次抢购时完整生效")
|
||||
}
|
||||
if sample, err := newClient.ServerTimeSample(); err == nil {
|
||||
logger.SetDouyuOffset(sample.Offset)
|
||||
} else {
|
||||
logger.LogEvent("时间", "warn", "保存配置后服务端时间校准失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
logger.LogEvent("配置", "success", "配置已保存并生效: 日志等级=%s 并发=%d 重试=%d 重试阶段=%d 模式=%d trace=%v",
|
||||
cfg.LogLevel, cfg.Concurrency, cfg.Attempts, len(cfg.RetryStages), cfg.SeckillMode, cfg.TraceEnabled)
|
||||
|
||||
Reference in New Issue
Block a user