|
@@ -16,6 +16,8 @@ import (
|
|
|
type WafTask interface {
|
|
|
//获取到期时间小于3天的同步时间
|
|
|
SynchronizationTime(ctx context.Context) error
|
|
|
+ StopPlan(ctx context.Context) error
|
|
|
+ RecoverStopPlan(ctx context.Context) error
|
|
|
}
|
|
|
|
|
|
func NewWafTask (
|
|
@@ -25,6 +27,7 @@ func NewWafTask (
|
|
|
cdn service.CdnService,
|
|
|
hostRep repository.HostRepository,
|
|
|
globalLimitRep repository.GlobalLimitRepository,
|
|
|
+ expiredRep repository.ExpiredRepository,
|
|
|
task *Task,
|
|
|
) WafTask{
|
|
|
return &wafTask{
|
|
@@ -35,6 +38,7 @@ func NewWafTask (
|
|
|
cdn: cdn,
|
|
|
hostRep: hostRep,
|
|
|
globalLimitRep: globalLimitRep,
|
|
|
+ expiredRep: expiredRep,
|
|
|
}
|
|
|
}
|
|
|
type wafTask struct {
|
|
@@ -45,6 +49,7 @@ type wafTask struct {
|
|
|
cdn service.CdnService
|
|
|
hostRep repository.HostRepository
|
|
|
globalLimitRep repository.GlobalLimitRepository
|
|
|
+ expiredRep repository.ExpiredRepository
|
|
|
}
|
|
|
|
|
|
|
|
@@ -291,7 +296,7 @@ func (t *wafTask) findMismatchedExpirations(ctx context.Context, wafLimits []mod
|
|
|
}
|
|
|
|
|
|
|
|
|
-//获取到期时间小于1天的同步时间
|
|
|
+//获取同步到期时间小于1天的套餐
|
|
|
|
|
|
func (t *wafTask) SynchronizationTime(ctx context.Context) error {
|
|
|
// 1. 获取 WAF 全局配置中即将到期(小于3天)的数据
|
|
@@ -317,7 +322,6 @@ func (t *wafTask) SynchronizationTime(ctx context.Context) error {
|
|
|
|
|
|
|
|
|
|
|
|
-//获取到期的进行关闭套餐操作
|
|
|
// 获取到期的进行关闭套餐操作
|
|
|
func (t *wafTask) StopPlan(ctx context.Context) error {
|
|
|
// 1. 获取 WAF 全局配置中已经到期的数据
|
|
@@ -343,27 +347,56 @@ func (t *wafTask) StopPlan(ctx context.Context) error {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 3. 关闭所有已经到期的套餐
|
|
|
- t.logger.Info("开始关闭已到期的WAF服务", zap.Int("数量", len(wafLimits)))
|
|
|
+ // 3. 筛选出尚未被关闭的套餐
|
|
|
+ var plansToClose []model.GlobalLimit
|
|
|
+ for _, limit := range wafLimits {
|
|
|
+ isClosed, err := t.expiredRep.IsPlanClosed(ctx, int64(limit.HostId))
|
|
|
+ if err != nil {
|
|
|
+ t.logger.Error("检查Redis中套餐关闭状态失败", zap.Int("hostId", limit.HostId), zap.Error(err))
|
|
|
+ continue // 跳过这个,处理下一个
|
|
|
+ }
|
|
|
+ if !isClosed {
|
|
|
+ plansToClose = append(plansToClose, limit)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(plansToClose) == 0 {
|
|
|
+ t.logger.Info("没有新的到期套餐需要关闭")
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 对筛选出的套餐执行关闭操作
|
|
|
+ t.logger.Info("开始关闭新的到期WAF服务", zap.Int("数量", len(plansToClose)))
|
|
|
var allErrors *multierror.Error
|
|
|
|
|
|
var webIds []int
|
|
|
- for _, limit := range wafLimits {
|
|
|
+ for _, limit := range plansToClose {
|
|
|
webIds = append(webIds, limit.HostId)
|
|
|
}
|
|
|
|
|
|
-
|
|
|
if err := t.BanServer(ctx, webIds, false); err != nil {
|
|
|
- allErrors = multierror.Append(allErrors, fmt.Errorf("关闭hostId %d 的服务失败: %w", webIds, err))
|
|
|
- }
|
|
|
-
|
|
|
+ allErrors = multierror.Append(allErrors, fmt.Errorf("关闭hostId %v 的服务失败: %w", webIds, err))
|
|
|
+ } else {
|
|
|
+ // 服务关闭成功后,将这些套餐信息添加到 Redis
|
|
|
+ var expiredInfos []repository.ExpiredInfo
|
|
|
+ for _, limit := range plansToClose {
|
|
|
+ expiredInfos = append(expiredInfos, repository.ExpiredInfo{
|
|
|
+ HostID: int64(limit.HostId),
|
|
|
+ Expiry: time.Unix(limit.ExpiredAt, 0),
|
|
|
+ })
|
|
|
+ }
|
|
|
|
|
|
+ if len(expiredInfos) > 0 {
|
|
|
+ if err := t.expiredRep.AddClosePlans(ctx, expiredInfos...); err != nil {
|
|
|
+ allErrors = multierror.Append(allErrors, fmt.Errorf("添加已关闭套餐信息到Redis失败: %w", err))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
return allErrors.ErrorOrNil()
|
|
|
}
|
|
|
//对于到期7天内续费的产品需要进行恢复操作
|
|
|
|
|
|
-// RecoverStopPlan 对于到期7天内续费的产品进行恢复操作
|
|
|
func (t *wafTask) RecoverStopPlan(ctx context.Context) error {
|
|
|
// 1. 获取所有已过期(expired_at < now)但状态仍为 true 的 WAF 记录
|
|
|
// StopPlan 任务会禁用这些服务,但不会改变它们的 state
|
|
@@ -392,22 +425,28 @@ func (t *wafTask) RecoverStopPlan(ctx context.Context) error {
|
|
|
t.logger.Info("发现已续费、需要恢复的WAF服务", zap.Int("数量", len(renewalRequests)))
|
|
|
var allErrors *multierror.Error
|
|
|
|
|
|
-
|
|
|
var webIds []int
|
|
|
for _, req := range renewalRequests {
|
|
|
webIds = append(webIds, req.HostId)
|
|
|
}
|
|
|
|
|
|
-
|
|
|
if err := t.BanServer(ctx, webIds, true); err != nil {
|
|
|
- allErrors = multierror.Append(allErrors, fmt.Errorf("恢复hostId %d: 启用服务失败: %w", webIds, err))
|
|
|
+ allErrors = multierror.Append(allErrors, fmt.Errorf("恢复hostId %v: 启用服务失败: %w", webIds, err))
|
|
|
+ } else {
|
|
|
+ // 服务恢复成功后,从 Redis 中移除这些套餐的关闭记录
|
|
|
+ planIds := make([]int64, len(webIds))
|
|
|
+ for i, id := range webIds {
|
|
|
+ planIds[i] = int64(id)
|
|
|
+ }
|
|
|
+ if err := t.expiredRep.RemoveClosePlanIds(ctx, planIds...); err != nil {
|
|
|
+ allErrors = multierror.Append(allErrors, fmt.Errorf("从Redis移除已恢复的套餐失败: %w", err))
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- for _, req := range renewalRequests {
|
|
|
- if err := t.EditExpired(ctx, []RenewalRequest{req}); err != nil {
|
|
|
- allErrors = multierror.Append(allErrors, fmt.Errorf("恢复hostId %d: 更新数据库状态失败: %w", req.HostId, err))
|
|
|
+ if len(renewalRequests) > 0 {
|
|
|
+ // 统一执行续费和数据库更新操作
|
|
|
+ if err := t.EditExpired(ctx, renewalRequests); err != nil {
|
|
|
+ allErrors = multierror.Append(allErrors, fmt.Errorf("批量更新已恢复服务的数据库状态失败: %w", err))
|
|
|
}
|
|
|
}
|
|
|
|