Prechádzať zdrojové kódy

perf(task): 提高任务执行频率并优化主机到期处理

-将任务执行频率从每小时一次改为每分钟一次
- 优化主机到期处理查询,提高效率- 调整结构体字段标记,确保正确映射数据库列
fusu 2 týždňov pred
rodič
commit
e1406d817f

+ 2 - 2
api/v1/host.go

@@ -37,6 +37,6 @@ type GlobalLimitConfigResponse struct {
 }
 
 type GetAlmostExpireHostResponse struct {
-	HostId   int
-	ExpiredAt int64
+	HostId   int `gorm:"column:id"`
+	ExpiredAt int64 `gorm:"column:nextduedate"`
 }

+ 1 - 1
internal/model/globallimit.go

@@ -13,7 +13,7 @@ type GlobalLimit struct {
 	GatewayGroupId  int
 	Comment         string
 	State         bool `gorm:"column:state" default:"true"`
-	ExpiredAt       int64
+	ExpiredAt       int64 `gorm:"column:expired_at"`
 	createdAt       time.Time
 	updatedAt       time.Time
 }

+ 4 - 2
internal/repository/host.go

@@ -98,7 +98,8 @@ func (r *hostRepository) GetAlmostExpired(ctx context.Context, hostId []int,addT
 	if err := r.DB(ctx).Table("shd_host").
 		Where("id IN ?", hostId).
 		Where("nextduedate < ?", expiredTime).
-		Find(&res).Error; err != nil {
+		Select("id", "nextduedate").
+		Scan(&res).Error; err != nil {
 		return nil, err
 	}
 	return res, nil
@@ -122,7 +123,8 @@ func (r *hostRepository) GetExpireTimeByHostId(ctx context.Context, hostIds []in
 	var res []v1.GetAlmostExpireHostResponse
 	if err := r.DB(ctx).Table("shd_host").
 		Where("id IN ?", hostIds).
-		Find(&res).Error; err != nil {
+		Select("id,nextduedate").
+		Scan(&res).Error; err != nil {
 		return nil, err
 	}
 	return res, nil

+ 4 - 4
internal/server/task.go

@@ -76,7 +76,7 @@ func (t *TaskServer) Start(ctx context.Context) error {
 
 
 
-	_, err := t.scheduler.Cron("0 * * * *").Do(func() {
+	_, err := t.scheduler.Cron("* * * * *").Do(func() {
 		err := t.wafTask.SynchronizationTime(ctx)
 		if err != nil {
 			t.log.Error("同步到期时间失败", zap.Error(err))
@@ -86,7 +86,7 @@ func (t *TaskServer) Start(ctx context.Context) error {
 		t.log.Error("同步到期时间注册任务失败", zap.Error(err))
 	}
 
-	_, err = t.scheduler.Cron("0 * * * *").Do(func() {
+	_, err = t.scheduler.Cron("* * * * *").Do(func() {
 		err := t.wafTask.StopPlan(ctx)
 		if err != nil {
 			t.log.Error("停止套餐失败", zap.Error(err))
@@ -97,7 +97,7 @@ func (t *TaskServer) Start(ctx context.Context) error {
 	}
 
 
-	_, err = t.scheduler.Cron("0 * * * *").Do(func() {
+	_, err = t.scheduler.Cron("* * * * *").Do(func() {
 		err := t.wafTask.RecoverRecentPlan(ctx)
 		if err != nil {
 			t.log.Error("续费失败", zap.Error(err))
@@ -108,7 +108,7 @@ func (t *TaskServer) Start(ctx context.Context) error {
 	}
 
 
-	_, err = t.scheduler.Cron("0 * * * *").Do(func() {
+	_, err = t.scheduler.Cron("* * * * *").Do(func() {
 		err := t.wafTask.CleanUpStaleRecords(ctx)
 		if err != nil {
 			t.log.Error("续费失败", zap.Error(err))