From 99a35cb48152e0a4635c81a733157a784fa20a28 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sun, 13 Aug 2023 15:26:59 +0800 Subject: [PATCH] =?UTF-8?q?DNS=E4=BB=BB=E5=8A=A1=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/models/dns/dns_task_dao.go | 16 ++++++++++------ internal/db/models/dns/dns_task_model.go | 20 ++++++++++++++++++++ internal/setup/sql.json | 6 +++++- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/internal/db/models/dns/dns_task_dao.go b/internal/db/models/dns/dns_task_dao.go index d7c94034..0ba83088 100644 --- a/internal/db/models/dns/dns_task_dao.go +++ b/internal/db/models/dns/dns_task_dao.go @@ -61,11 +61,12 @@ func (this *DNSTaskDAO) CreateDNSTask(tx *dbs.Tx, clusterId int64, serverId int6 "error": "", "version": time.Now().UnixNano(), }, maps.Map{ - "updatedAt": time.Now().Unix(), - "isDone": false, - "isOk": false, - "error": "", - "version": time.Now().UnixNano(), + "updatedAt": time.Now().Unix(), + "isDone": false, + "isOk": false, + "error": "", + "version": time.Now().UnixNano(), + "countFails": 0, }) if err != nil { return err @@ -108,7 +109,7 @@ func (this *DNSTaskDAO) CreateDomainTask(tx *dbs.Tx, domainId int64, taskType DN // FindAllDoingTasks 查找所有正在执行的任务 func (this *DNSTaskDAO) FindAllDoingTasks(tx *dbs.Tx) (result []*DNSTask, err error) { _, err = this.Query(tx). - Attr("isDone", 0). + Where("(isDone=0 OR (isDone=1 AND isOk=0 AND countFails<3))"). // 3 = retry times Asc("version"). AscPk(). Slice(&result). @@ -171,6 +172,7 @@ func (this *DNSTaskDAO) UpdateDNSTaskError(tx *dbs.Tx, taskId int64, err string) op.IsDone = true op.Error = err op.IsOk = false + op.CountFails = dbs.SQL("countFails+1") return this.Save(tx, op) } @@ -197,6 +199,7 @@ func (this *DNSTaskDAO) UpdateDNSTaskDone(tx *dbs.Tx, taskId int64, taskVersion op.Id = taskId op.IsDone = true op.IsOk = true + op.CountFails = 0 op.Error = "" return this.Save(tx, op) } @@ -219,6 +222,7 @@ func (this *DNSTaskDAO) UpdateClusterDNSTasksDone(tx *dbs.Tx, clusterId int64, m Set("isDone", true). Set("isOk", true). Set("error", ""). + Set("countFails", 0). UpdateQuickly() } diff --git a/internal/db/models/dns/dns_task_model.go b/internal/db/models/dns/dns_task_model.go index f1108b44..08cf01b2 100644 --- a/internal/db/models/dns/dns_task_model.go +++ b/internal/db/models/dns/dns_task_model.go @@ -1,5 +1,23 @@ package dns +import "github.com/iwind/TeaGo/dbs" + +const ( + DNSTaskField_Id dbs.FieldName = "id" // ID + DNSTaskField_ClusterId dbs.FieldName = "clusterId" // 集群ID + DNSTaskField_ServerId dbs.FieldName = "serverId" // 服务ID + DNSTaskField_NodeId dbs.FieldName = "nodeId" // 节点ID + DNSTaskField_DomainId dbs.FieldName = "domainId" // 域名ID + DNSTaskField_RecordName dbs.FieldName = "recordName" // 记录名 + DNSTaskField_Type dbs.FieldName = "type" // 任务类型 + DNSTaskField_UpdatedAt dbs.FieldName = "updatedAt" // 更新时间 + DNSTaskField_IsDone dbs.FieldName = "isDone" // 是否已完成 + DNSTaskField_IsOk dbs.FieldName = "isOk" // 是否成功 + DNSTaskField_Error dbs.FieldName = "error" // 错误信息 + DNSTaskField_Version dbs.FieldName = "version" // 版本 + DNSTaskField_CountFails dbs.FieldName = "countFails" // 尝试失败次数 +) + // DNSTask DNS更新任务 type DNSTask struct { Id uint64 `field:"id"` // ID @@ -14,6 +32,7 @@ type DNSTask struct { IsOk bool `field:"isOk"` // 是否成功 Error string `field:"error"` // 错误信息 Version uint64 `field:"version"` // 版本 + CountFails uint32 `field:"countFails"` // 尝试失败次数 } type DNSTaskOperator struct { @@ -29,6 +48,7 @@ type DNSTaskOperator struct { IsOk any // 是否成功 Error any // 错误信息 Version any // 版本 + CountFails any // 尝试失败次数 } func NewDNSTaskOperator() *DNSTaskOperator { diff --git a/internal/setup/sql.json b/internal/setup/sql.json index cbb1cd26..97fb864d 100644 --- a/internal/setup/sql.json +++ b/internal/setup/sql.json @@ -87506,7 +87506,7 @@ "name": "edgeDNSTasks", "engine": "InnoDB", "charset": "utf8mb4_general_ci", - "definition": "CREATE TABLE `edgeDNSTasks` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `clusterId` int(11) unsigned DEFAULT '0' COMMENT '集群ID',\n `serverId` int(11) unsigned DEFAULT '0' COMMENT '服务ID',\n `nodeId` int(11) unsigned DEFAULT '0' COMMENT '节点ID',\n `domainId` int(11) unsigned DEFAULT '0' COMMENT '域名ID',\n `recordName` varchar(255) DEFAULT NULL COMMENT '记录名',\n `type` varchar(255) DEFAULT NULL COMMENT '任务类型',\n `updatedAt` bigint(11) unsigned DEFAULT '0' COMMENT '更新时间',\n `isDone` tinyint(1) unsigned DEFAULT '0' COMMENT '是否已完成',\n `isOk` tinyint(1) unsigned DEFAULT '0' COMMENT '是否成功',\n `error` varchar(1024) DEFAULT NULL COMMENT '错误信息',\n `version` bigint(20) unsigned DEFAULT '0' COMMENT '版本',\n PRIMARY KEY (`id`),\n UNIQUE KEY `uniqueId` (`clusterId`,`serverId`,`nodeId`,`domainId`,`recordName`,`type`) USING BTREE,\n KEY `isDone` (`isDone`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='DNS更新任务'", + "definition": "CREATE TABLE `edgeDNSTasks` (\n `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n `clusterId` int(11) unsigned DEFAULT '0' COMMENT '集群ID',\n `serverId` int(11) unsigned DEFAULT '0' COMMENT '服务ID',\n `nodeId` int(11) unsigned DEFAULT '0' COMMENT '节点ID',\n `domainId` int(11) unsigned DEFAULT '0' COMMENT '域名ID',\n `recordName` varchar(255) DEFAULT NULL COMMENT '记录名',\n `type` varchar(255) DEFAULT NULL COMMENT '任务类型',\n `updatedAt` bigint(11) unsigned DEFAULT '0' COMMENT '更新时间',\n `isDone` tinyint(1) unsigned DEFAULT '0' COMMENT '是否已完成',\n `isOk` tinyint(1) unsigned DEFAULT '0' COMMENT '是否成功',\n `error` varchar(1024) DEFAULT NULL COMMENT '错误信息',\n `version` bigint(20) unsigned DEFAULT '0' COMMENT '版本',\n `countFails` int(11) unsigned DEFAULT '0' COMMENT '尝试失败次数',\n PRIMARY KEY (`id`),\n UNIQUE KEY `uniqueId` (`clusterId`,`serverId`,`nodeId`,`domainId`,`recordName`,`type`) USING BTREE,\n KEY `isDone` (`isDone`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='DNS更新任务'", "fields": [ { "name": "id", @@ -87555,6 +87555,10 @@ { "name": "version", "definition": "bigint(20) unsigned DEFAULT '0' COMMENT '版本'" + }, + { + "name": "countFails", + "definition": "int(11) unsigned DEFAULT '0' COMMENT '尝试失败次数'" } ], "indexes": [