[Cmii][ImageSync] - reformat agent-go - 2

This commit is contained in:
zeaslity
2024-03-29 14:46:11 +08:00
parent 1be48aaac2
commit 1d32d7dd04
6 changed files with 42 additions and 39 deletions

View File

@@ -73,31 +73,33 @@ func INIT(octopusAgentConfigFileName string, agentServerInfoConf string) chan bo
// 监听初始化连接中的信息 // 监听初始化连接中的信息
initFromServerQueue := &rabbitmq.RabbitQueue{ initFromServerQueue := &rabbitmq.RabbitQueue{
RabbitProp: initFromServerProp, RabbitProp: initFromServerProp,
RabbitConnectInfo: rabbitTCPConnectInfo,
} }
//defer initFromServerQueue.Close() //defer initFromServerQueue.Close()
// 建立连接 // 建立连接
initFromServerQueue.Connect() initFromServerQueue.Connect()
initForeverHandle := initFromServerQueue.Handle() initFromServerQueue.Handle()
buildAndSendInitMsgToServer(agentServerInfo, initToServerQueue) buildAndSendInitMsgToServer(agentServerInfo, initToServerQueue)
// receive from server // receive from server
for g.G.AgentHasRegister == false { for g.G.AgentHasRegister == false {
select { select {
case <-initFromServerQueue.ReceiveChan.InitRChan: case initFromServerMsg := <-initFromServerQueue.ReceiveChan.InitRChan:
initFromServerMsg := <-initFromServerQueue.ReceiveChan.InitRChan if handleInitMsgFromServer(initFromServerMsg, initToServerQueue, agentServerInfo) {
handleInitMsgFromServer(initFromServerMsg, initToServerQueue, agentServerInfo) break
}
default: default:
//log.Debug("") log.Debug("agent init not received from server ! start to waiting !")
time.Sleep(time.Second * 10) time.Sleep(time.Second * 10)
} }
} }
<-initForeverHandle //<-initForeverHandle
close(initFromServerQueue.ReceiveChan.InitRChan) //close(initFromServerQueue.ReceiveChan.InitRChan)
// 建立 运行时 RabbitMQ连接 // 建立 运行时 RabbitMQ连接
runtimeConnectorQueue := buildAndStartBusinessRuntimeQueue(a_agent.AgentServerInfoCache.TopicName) runtimeConnectorQueue := buildAndStartBusinessRuntimeQueue(a_agent.AgentServerInfoCache.TopicName)
@@ -194,7 +196,7 @@ func buildAndSendInitMsgToServer(agentServerInfo *a_agent.AgentServerInfo, initT
}) })
} }
func BuildOctopusTCPConnect(agentConfig *viper.Viper) rabbitmq.RabbitTCPConnectInfo { func BuildOctopusTCPConnect(agentConfig *viper.Viper) *rabbitmq.RabbitTCPConnectInfo {
host := agentConfig.GetString("spring.rabbitmq.host") host := agentConfig.GetString("spring.rabbitmq.host")
port := agentConfig.GetString("spring.rabbitmq.port") port := agentConfig.GetString("spring.rabbitmq.port")
username := agentConfig.GetString("spring.rabbitmq.username") username := agentConfig.GetString("spring.rabbitmq.username")
@@ -202,7 +204,7 @@ func BuildOctopusTCPConnect(agentConfig *viper.Viper) rabbitmq.RabbitTCPConnectI
virtualHost := agentConfig.GetString("spring.rabbitmq.virtual-host") virtualHost := agentConfig.GetString("spring.rabbitmq.virtual-host")
//todo //todo
return rabbitmq.RabbitTCPConnectInfo{ return &rabbitmq.RabbitTCPConnectInfo{
UserName: username, UserName: username,
Password: password, Password: password,
Host: host, Host: host,
@@ -227,7 +229,7 @@ func handleInitMsgFromServer(initFromServerMsg *rabbitmq.OctopusMessage, initToS
//initOctopusMessageDeliveries := initFromServerQueue.Read(false) //initOctopusMessageDeliveries := initFromServerQueue.Read(false)
log.Debug(fmt.Sprintf("message received from server is %s", &initFromServerMsg)) log.DebugF("message received from server is %s", initFromServerMsg)
var serverInfo a_agent.AgentServerInfo var serverInfo a_agent.AgentServerInfo

View File

@@ -1,9 +1,8 @@
package main package a_init
import ( import (
"wdd.io/agent-go/a_agent" "wdd.io/agent-go/a_agent"
"wdd.io/agent-go/a_executor" "wdd.io/agent-go/a_executor"
"wdd.io/agent-go/a_init"
"wdd.io/agent-go/a_status" "wdd.io/agent-go/a_status"
) )
@@ -42,8 +41,8 @@ func BastionModeInit() {
// re-get agentInfo from status module // re-get agentInfo from status module
agentInfo := a_status.ReportAgentInfo() agentInfo := a_status.ReportAgentInfo()
a_init.refreshAgentInfoByStatusInfo(agentInfo, agentServerInfo) refreshAgentInfoByStatusInfo(agentInfo, agentServerInfo)
a_init.BuildAgentOsOperator(agentInfo, agentServerInfo) BuildAgentOsOperator(agentInfo, agentServerInfo)
// install docker // install docker
agentOsOperator := a_executor.AgentOsOperatorCache agentOsOperator := a_executor.AgentOsOperatorCache

View File

@@ -21,7 +21,7 @@ func main() {
flag.Parse() flag.Parse()
if mode == "bastion" { if mode == "bastion" {
BastionModeInit() a_init.BastionModeInit()
return return
} }

View File

@@ -52,7 +52,7 @@ spring:
allow-bean-definition-overriding: true allow-bean-definition-overriding: true
rabbitmq: rabbitmq:
# host: 42.192.52.227 # host: 42.192.52.227
host: 192.168.35.71 host: 10.250.0.100
port: 20672 port: 20672
username: boge username: boge
password: boge8tingH password: boge8tingH

View File

@@ -15,7 +15,7 @@ type OctopusMsgHandler interface {
func (om *OctopusMessage) HandleMsg(rChan *RabbitReceiveChan) { func (om *OctopusMessage) HandleMsg(rChan *RabbitReceiveChan) {
// 实际执行 OM handle进程 // 实际执行 OM handle进程
log.Debug("接收到OctopusMessage, 开始处理!") log.DebugF("接收到OctopusMessage => %s, 开始处理!", om.OctopusMessageType)
doHandleOctopusMessage(om, rChan) doHandleOctopusMessage(om, rChan)
} }

View File

@@ -37,7 +37,7 @@ type RabbitQueue struct {
// 连接属性 // 连接属性
RabbitProp *ConnectProperty RabbitProp *ConnectProperty
// 底层连接tcp信息 // 底层连接tcp信息
RabbitConnectInfo RabbitTCPConnectInfo RabbitConnectInfo *RabbitTCPConnectInfo
// 返回消息队列 // 返回消息队列
ReceiveChan *RabbitReceiveChan ReceiveChan *RabbitReceiveChan
} }
@@ -79,7 +79,7 @@ var instance *amqp.Connection
var once sync.Once var once sync.Once
// 初始化 Singleton 实例的函数 // 初始化 Singleton 实例的函数
func createInstance(rabbitConnectInfo RabbitTCPConnectInfo) func() { func createInstance(rabbitConnectInfo *RabbitTCPConnectInfo) {
// 在这里进行 Singleton 的初始化操作 // 在这里进行 Singleton 的初始化操作
// 获取RabbitMQ的连接地址 // 获取RabbitMQ的连接地址
@@ -92,15 +92,16 @@ func createInstance(rabbitConnectInfo RabbitTCPConnectInfo) func() {
} }
instance = connection instance = connection
return nil
} }
// GetInstance 获取全局唯一的 Singleton 实例的函数 // GetInstance 获取全局唯一的 Singleton 实例的函数
func GetInstance(rabbitConnectInfo RabbitTCPConnectInfo) *amqp.Connection { func GetInstance(rabbitConnectInfo *RabbitTCPConnectInfo) *amqp.Connection {
// 使用 sync.Once 确保 createInstance 只会被调用一次 // 使用 sync.Once 确保 createInstance 只会被调用一次
// todo 理解 // todo 理解
once.Do(createInstance(rabbitConnectInfo)) once.Do(func() {
createInstance(rabbitConnectInfo)
})
return instance return instance
} }
@@ -185,24 +186,25 @@ func (r *RabbitQueue) Close() error {
func (r *RabbitQueue) Handle() chan bool { func (r *RabbitQueue) Handle() chan bool {
deliveries := r.Read(true) deliveries := r.Read(true)
forverHandle := make(chan bool) foreverHandle := make(chan bool)
// 死循环处理Octopus Message // 死循环处理Octopus Message
for delivery := range deliveries { P.Submit(func() {
for delivery := range deliveries {
var om *OctopusMessage var om *OctopusMessage
err := json.Unmarshal(delivery.Body, &om) err := json.Unmarshal(delivery.Body, &om)
if err != nil { if err != nil {
log.Error(fmt.Sprintf("octopus message convert to json is wrong! msg is => %s", delivery.Body)) log.Error(fmt.Sprintf("octopus message convert to json is wrong! msg is => %s", delivery.Body))
// 保存到某处 // 保存到某处
continue continue
}
// 策略模式 处理消息
P.Submit(func() {
om.HandleMsg(r.ReceiveChan)
})
} }
// 策略模式 处理消息 })
P.Submit(func() { return foreverHandle
om.HandleMsg(r.ReceiveChan)
})
}
return forverHandle
} }
// Send 向RabbitMQ中发送消息 // Send 向RabbitMQ中发送消息
@@ -257,7 +259,7 @@ func (r *RabbitQueue) Read(autoAck bool) <-chan amqp.Delivery {
return msgs return msgs
} }
func parseRabbitMQEndpoint(rabbitConnectInfo RabbitTCPConnectInfo) string { func parseRabbitMQEndpoint(rabbitConnectInfo *RabbitTCPConnectInfo) string {
var res strings.Builder var res strings.Builder