Files
iotlcm/server/internal/logic/agent/agent_role_queue.go
T
MiFaZhan 59eb34976a chore: 初始开源发布
iotlcm-admin-svr:管理后台服务。

- Go module:iotlcm → git.5iot.com/iotlcm-public/iotlcm-admin-svr(729 文件)
- 凭据与地址脱敏:JWT signingKey(含代码里的 MustGet 默认值)、
  MySQL DSN、Redis 口令、告警邮件发件人邮箱全部改为占位;
  私网 IP → 文档地址段;库名 xiujie_admin → iotlcm
- 真实手机号/邮箱 → 合法 11 位编造号 / example.com
- 移除内部资产:CI 触发器、过时 DDL(sql/db_iotlcm.sql)、
  私有依赖 go get 行、明文 registry 登录口令
- 启动 banner 统一 IOTLCM 方块字 + 社区惯例署名
- Dockerfile 改用公共基础镜像
- 拼错的 RADEME.md → README.md,README 重写
- 需要真实数据库的用例改为可用 IOTLCM_TEST_WITH_DB=1 显式启用
- 补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes

数据库 schema 不随本仓发布(生产 DDL 已过时),README 已说明。
2026-10-23 14:48:30 +08:00

84 lines
3.3 KiB
Go

package agent
import (
"context"
"encoding/json"
"time"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/cmd/inithttp"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/consts"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/rabbitmq"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
localqueue "git.5iot.com/iotlcm-public/iotlcm-admin-svr/utility/queue"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
type agentRoleSyncQueue struct{}
func init() {
inithttp.RegisterHttpInitFunc("agentRoleSyncQueue", func(ctx context.Context) error {
if rabbitmq.Enabled(ctx) {
if err := rabbitmq.DeclareQueue(ctx, consts.QueueAgentRoleSyncDeadLetter); err != nil {
return err
}
return rabbitmq.ConsumeJSON[model.AgentRoleSyncMessage](ctx, consts.QueueAgentRoleSync, "iotlcm-agent-role-sync", handleAgentRoleSyncMessage)
}
if err := localqueue.NewConsumer(gctx.New(), &agentRoleSyncQueue{}, &localqueue.Config{Workers: 2}); err != nil {
return err
}
return localqueue.NewProducer(gctx.New(), localqueue.QueueTypeMemory, consts.QueueAgentRoleSync, &localqueue.Config{})
})
}
func publishAgentRoleSync(ctx context.Context, message *model.AgentRoleSyncMessage) error {
if rabbitmq.Enabled(ctx) {
return rabbitmq.Publish(ctx, consts.QueueAgentRoleSync, message)
}
return localqueue.Push(ctx, consts.QueueAgentRoleSync, message)
}
func handleAgentRoleSyncMessage(ctx context.Context, message model.AgentRoleSyncMessage) error {
if err := service.AgentRole().SyncInheritedRole(ctx, &message); err != nil {
g.Log().Errorf(ctx, "agent role sync failed, sourceRoleId:%d, retry:%d, err:%v", message.SourceRoleId, message.RetryCount, err)
if message.RetryCount < 3 {
message.RetryCount++
return rabbitmq.PublishDelayed(ctx, consts.QueueAgentRoleSyncRetry, consts.QueueAgentRoleSync, time.Duration(message.RetryCount)*time.Second, message)
}
deadLetter := model.AgentRoleSyncDeadLetterMessage{Message: message, Error: err.Error(), FailedAt: time.Now().Format(time.RFC3339)}
if publishErr := rabbitmq.Publish(ctx, consts.QueueAgentRoleSyncDeadLetter, deadLetter); publishErr != nil {
return publishErr
}
g.Log().Errorf(ctx, "agent role sync moved to dead-letter queue, sourceRoleId:%d, err:%v", message.SourceRoleId, err)
return nil
}
g.Log().Infof(ctx, "agent role sync completed, sourceRoleId:%d", message.SourceRoleId)
return nil
}
func (*agentRoleSyncQueue) GetTopic() string { return consts.QueueAgentRoleSync }
func (*agentRoleSyncQueue) GetType() localqueue.QueueType { return localqueue.QueueTypeMemory }
func (*agentRoleSyncQueue) Handle(ctx context.Context, payload localqueue.Payload) error {
var message model.AgentRoleSyncMessage
if err := json.Unmarshal(payload.Data, &message); err != nil {
return err
}
var err error
for attempt := 0; attempt <= 3; attempt++ {
err = service.AgentRole().SyncInheritedRole(ctx, &message)
if err == nil {
g.Log().Infof(ctx, "agent role sync completed, sourceRoleId:%d", message.SourceRoleId)
return nil
}
if attempt < 3 {
time.Sleep(time.Duration(attempt+1) * time.Second)
}
}
g.Log().Errorf(ctx, "agent role sync failed after retries, sourceRoleId:%d, err:%v", message.SourceRoleId, err)
return err
}