fix: 修复禁用所有轮询配置重启后再启用无法工作的问题
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m23s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m23s
- 新增 RedisPollingConfigChangedChannel 常量,用于跨进程配置变更通知 - ConfigService 注入 Redis,Create/Delete/UpdateStatus 后发布 Pub/Sub 事件 - PollingConfigManager 新增 WatchChanges() 方法,收到通知立即刷新内存缓存 - PollingInitializer 新增 Restart() 方法,支持初始化完成后安全重启(CAS 防并发) - Worker 订阅配置变更事件,configs 从空变为非空时触发 Initializer.Restart() 根因:启动时所有配置禁用导致分片队列为空,Initializer 是一次性的, 之后启用配置只更新 DB 但不重建队列,调度器无卡可处理
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
@@ -18,11 +20,22 @@ import (
|
||||
// ConfigService 轮询配置服务
|
||||
type ConfigService struct {
|
||||
configStore *postgres.PollingConfigStore
|
||||
redis *redis.Client
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewConfigService 创建轮询配置服务实例
|
||||
func NewConfigService(configStore *postgres.PollingConfigStore) *ConfigService {
|
||||
return &ConfigService{configStore: configStore}
|
||||
func NewConfigService(configStore *postgres.PollingConfigStore, redisClient *redis.Client, logger *zap.Logger) *ConfigService {
|
||||
return &ConfigService{configStore: configStore, redis: redisClient, logger: logger}
|
||||
}
|
||||
|
||||
func (s *ConfigService) notifyConfigChanged(ctx context.Context, event string) {
|
||||
if s.redis == nil {
|
||||
return
|
||||
}
|
||||
if err := s.redis.Publish(ctx, constants.RedisPollingConfigChangedChannel(), event).Err(); err != nil {
|
||||
s.logger.Warn("通知轮询配置变更失败", zap.String("event", event), zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建轮询配置
|
||||
@@ -66,6 +79,7 @@ func (s *ConfigService) Create(ctx context.Context, req *dto.CreatePollingConfig
|
||||
return nil, errors.Wrap(errors.CodeInternalError, err, "创建轮询配置失败")
|
||||
}
|
||||
|
||||
s.notifyConfigChanged(ctx, "created")
|
||||
return s.toResponse(config), nil
|
||||
}
|
||||
|
||||
@@ -158,6 +172,7 @@ func (s *ConfigService) Delete(ctx context.Context, id uint) error {
|
||||
return errors.Wrap(errors.CodeInternalError, err, "删除轮询配置失败")
|
||||
}
|
||||
|
||||
s.notifyConfigChanged(ctx, "deleted")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -224,6 +239,7 @@ func (s *ConfigService) UpdateStatus(ctx context.Context, id uint, status int16)
|
||||
return errors.Wrap(errors.CodeInternalError, err, "更新轮询配置状态失败")
|
||||
}
|
||||
|
||||
s.notifyConfigChanged(ctx, "updated")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user