|
@@ -281,7 +281,7 @@ func (t *wafTask) findMismatchedExpirations(ctx context.Context, wafLimits []mod
|
|
}
|
|
}
|
|
renewalRequests = append(renewalRequests, RenewalRequest{
|
|
renewalRequests = append(renewalRequests, RenewalRequest{
|
|
HostId: hostId,
|
|
HostId: hostId,
|
|
- ExpiredAt: hostTime, // 以 WAF 表的时间为准
|
|
|
|
|
|
+ ExpiredAt: hostTime, // 以 host 表的时间为准
|
|
PlanId: planId,
|
|
PlanId: planId,
|
|
})
|
|
})
|
|
}
|
|
}
|
|
@@ -291,7 +291,7 @@ func (t *wafTask) findMismatchedExpirations(ctx context.Context, wafLimits []mod
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-//获取到期时间小于3天的同步时间
|
|
|
|
|
|
+//获取到期时间小于1天的同步时间
|
|
|
|
|
|
func (t *wafTask) SynchronizationTime(ctx context.Context) error {
|
|
func (t *wafTask) SynchronizationTime(ctx context.Context) error {
|
|
// 1. 获取 WAF 全局配置中即将到期(小于3天)的数据
|
|
// 1. 获取 WAF 全局配置中即将到期(小于3天)的数据
|
|
@@ -322,7 +322,7 @@ func (t *wafTask) SynchronizationTime(ctx context.Context) error {
|
|
func (t *wafTask) StopPlan(ctx context.Context) error {
|
|
func (t *wafTask) StopPlan(ctx context.Context) error {
|
|
// 1. 获取 WAF 全局配置中已经到期的数据
|
|
// 1. 获取 WAF 全局配置中已经到期的数据
|
|
// 使用 time.Now().Unix() 表示获取所有 expired_at <= 当前时间的记录
|
|
// 使用 time.Now().Unix() 表示获取所有 expired_at <= 当前时间的记录
|
|
- wafLimits, err := t.globalLimitRep.GetGlobalLimitAlmostExpired(ctx, time.Now().Unix())
|
|
|
|
|
|
+ wafLimits, err := t.globalLimitRep.GetGlobalLimitAlmostExpired(ctx, 0)
|
|
if err != nil {
|
|
if err != nil {
|
|
return fmt.Errorf("获取全局到期配置失败: %w", err)
|
|
return fmt.Errorf("获取全局到期配置失败: %w", err)
|
|
}
|
|
}
|
|
@@ -367,76 +367,49 @@ func (t *wafTask) StopPlan(ctx context.Context) error {
|
|
|
|
|
|
// RecoverStopPlan 对于到期7天内续费的产品进行恢复操作
|
|
// RecoverStopPlan 对于到期7天内续费的产品进行恢复操作
|
|
func (t *wafTask) RecoverStopPlan(ctx context.Context) error {
|
|
func (t *wafTask) RecoverStopPlan(ctx context.Context) error {
|
|
- // 1. 查找在过去7天内到期,并且当前状态为“已关闭”的 WAF 记录
|
|
|
|
- // 这可能需要一个新的 repository 方法,例如: GetRecentlyClosedLimits
|
|
|
|
- // 我们先假设有这样一个方法,它返回 state=false 且 expired_at 在 (now-7天, now] 之间的记录
|
|
|
|
- since := time.Now().Add(-7 * 24 * time.Hour).Unix()
|
|
|
|
-
|
|
|
|
- // 假设你有一个方法 `GetClosedLimitsSince(ctx, sinceTime)`
|
|
|
|
- // closedLimits, err := t.globalLimitRep.GetClosedLimitsSince(ctx, since)
|
|
|
|
- // 为简化,我们先获取所有7天内到期的,再在逻辑里判断
|
|
|
|
-
|
|
|
|
- // 简单的实现:获取7天内到期的所有记录
|
|
|
|
- wafLimits, err := t.globalLimitRep.GetLimitsExpiredSince(ctx, since) // 假设有这个方法
|
|
|
|
|
|
+ // 1. 获取所有已过期(expired_at < now)但状态仍为 true 的 WAF 记录
|
|
|
|
+ // StopPlan 任务会禁用这些服务,但不会改变它们的 state
|
|
|
|
+ wafLimits, err := t.globalLimitRep.GetGlobalLimitAlmostExpired(ctx, SevenDaysInSeconds) // addTime=0 表示获取所有当前时间之前到期的
|
|
if err != nil {
|
|
if err != nil {
|
|
- return fmt.Errorf("获取近期到期配置失败: %w", err)
|
|
|
|
|
|
+ return fmt.Errorf("获取过期WAF配置失败: %w", err)
|
|
}
|
|
}
|
|
if len(wafLimits) == 0 {
|
|
if len(wafLimits) == 0 {
|
|
|
|
+ t.logger.Info("没有已过期且需要检查恢复的服务")
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
- // 提取 hostIds 并过滤出已关闭的记录
|
|
|
|
- var hostIds []int
|
|
|
|
- closedLimitsMap := make(map[int]model.GlobalLimit)
|
|
|
|
- for _, limit := range wafLimits {
|
|
|
|
- if !limit.State { // 只处理状态为“已关闭”的
|
|
|
|
- hostIds = append(hostIds, limit.HostId)
|
|
|
|
- closedLimitsMap[limit.HostId] = limit
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if len(hostIds) == 0 {
|
|
|
|
- return nil // 没有已关闭的记录需要检查
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 2. 获取这些 host 的当前到期时间
|
|
|
|
- hostExpirations, err := t.hostRep.GetExpireTimeByHostId(ctx, hostIds)
|
|
|
|
|
|
+ // 2. 检查这些记录对应的 host 是否已续费
|
|
|
|
+ // findMismatchedExpirations 会比较 waf.expired_at 和 host.nextduedate
|
|
|
|
+ renewalRequests, err := t.findMismatchedExpirations(ctx, wafLimits)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return fmt.Errorf("获取主机当前到期时间失败: %w", err)
|
|
|
|
|
|
+ return fmt.Errorf("检查续费状态失败: %w", err)
|
|
}
|
|
}
|
|
- hostExpiredMap := make(map[int]int64)
|
|
|
|
- for _, h := range hostExpirations {
|
|
|
|
- hostExpiredMap[h.HostId] = h.ExpiredAt
|
|
|
|
|
|
+
|
|
|
|
+ if len(renewalRequests) == 0 {
|
|
|
|
+ t.logger.Info("在已过期的服务中,没有发现已续费且需要恢复的服务")
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 3. 对已续费的服务执行恢复操作
|
|
|
|
+ t.logger.Info("发现已续费、需要恢复的WAF服务", zap.Int("数量", len(renewalRequests)))
|
|
var allErrors *multierror.Error
|
|
var allErrors *multierror.Error
|
|
- // 3. 比较时间,找出已续费的 host,并恢复服务
|
|
|
|
- for hostId, closedLimit := range closedLimitsMap {
|
|
|
|
- currentHostExpiry, ok := hostExpiredMap[hostId]
|
|
|
|
- if !ok {
|
|
|
|
- continue // host 不存在了,跳过
|
|
|
|
- }
|
|
|
|
|
|
|
|
- // 如果 host 表的到期时间 > global_limit 表的到期时间,说明已续费
|
|
|
|
- if currentHostExpiry > closedLimit.ExpiredAt {
|
|
|
|
- t.logger.Info("发现已续费并关闭的WAF服务,准备恢复", zap.Int("hostId", hostId))
|
|
|
|
|
|
+ for _, req := range renewalRequests {
|
|
|
|
+ // 启用CDN服务
|
|
|
|
+ webIds, err := t.GetCdnWebId(ctx, req.HostId)
|
|
|
|
+ if err != nil {
|
|
|
|
+ allErrors = multierror.Append(allErrors, fmt.Errorf("恢复hostId %d: 获取webId失败: %w", req.HostId, err))
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
|
|
- // 3a. 恢复网站服务
|
|
|
|
- webIds, err := t.GetCdnWebId(ctx, hostId)
|
|
|
|
- if err != nil {
|
|
|
|
- allErrors = multierror.Append(allErrors, fmt.Errorf("恢复hostId %d 时获取webId失败: %w", hostId, err))
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- if err := t.BanServer(ctx, webIds, true); err != nil { // true 表示启用
|
|
|
|
- allErrors = multierror.Append(allErrors, fmt.Errorf("恢复hostId %d 服务失败: %w", hostId, err))
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
|
|
+ if err := t.BanServer(ctx, webIds, true); err != nil {
|
|
|
|
+ allErrors = multierror.Append(allErrors, fmt.Errorf("恢复hostId %d: 启用服务失败: %w", req.HostId, err))
|
|
|
|
+ continue // 服务启用失败,暂时不更新数据库状态
|
|
|
|
+ }
|
|
|
|
|
|
- // 3b. 更新 global_limit 表的时间和状态
|
|
|
|
- var singleUpdate []struct{hostId int; expiredAt int64}
|
|
|
|
- singleUpdate = append(singleUpdate, struct{hostId int; expiredAt int64}{hostId: hostId, expiredAt: currentHostExpiry})
|
|
|
|
- if err := t.EditGlobalExpired(ctx, singleUpdate, true); err != nil { // true 表示启用
|
|
|
|
- allErrors = multierror.Append(allErrors, fmt.Errorf("更新hostId %d 状态为已恢复失败: %w", hostId, err))
|
|
|
|
- }
|
|
|
|
|
|
+ // 更新数据库状态(到期时间),state 保持为 true
|
|
|
|
+ if err := t.EditExpired(ctx, []RenewalRequest{req}); err != nil {
|
|
|
|
+ allErrors = multierror.Append(allErrors, fmt.Errorf("恢复hostId %d: 更新数据库状态失败: %w", req.HostId, err))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|