[agent-go] 初步完成Executor部分的代码

This commit is contained in:
zeaslity
2023-03-24 15:09:22 +08:00
parent fe8a4a03fc
commit 1af25d3992
9 changed files with 328 additions and 31 deletions

View File

@@ -0,0 +1,46 @@
package rabbitmq
import (
"agent-go/g"
"fmt"
"github.com/streadway/amqp"
)
// Send 向RabbitMQ中发送消息
func Send(conn *RabbitMQConn, connProp *ConnectProperty, message []byte) {
// 往哪里发
channel := conn.Channel
// 发送
channel.Publish(
connProp.ExchangeName,
connProp.TopicKey,
false,
true,
amqp.Publishing{
ContentType: "text/plain",
Body: message,
},
)
}
func Read(conn *RabbitMQConn, connProp *ConnectProperty, autoAck bool) <-chan amqp.Delivery {
// 拿到特定的Channel
channel := conn.Channel
// 开始读取队列中的全部消息
msgs, err := channel.Consume(
connProp.QueueName, // 队列名称
g.G.AgentServerInfo.AgentTopicName, // 消费者名称
autoAck, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // arguments
)
if err != nil {
log.Error(fmt.Sprintf("Failed to register a consumer: %v", err))
}
return msgs
}

View File

@@ -1,20 +0,0 @@
package rabbitmq
import "github.com/streadway/amqp"
// Send 向RabbitMQ中发送消息
func Send(conn *RabbitMQConn, connProp *ConnectProperty, message []byte) {
// 往哪里发
channel := conn.Channel
// 发送
channel.Publish(
connProp.ExchangeName,
connProp.TopicKey,
false,
true,
amqp.Publishing{
ContentType: "text/plain",
Body: message,
},
)
}

View File

@@ -14,6 +14,16 @@ type OctopusMessage struct {
ACTime time.Time `json:"ac_time" format:"2023-03-21 16:38:30"`
}
type ExecutionMessage struct {
NeedResultReplay bool `json:"needResultReplay"`
DurationTask bool `json:"durationTask,default:false"`
Type string `json:"type"`
SingleLineCommand []string `json:"singleLineCommand"`
MultiLineCommand [][]string `json:"multiLineCommand"`
PipeLineCommand [][]string `json:"pipeLineCommand"`
ResultKey string `json:"resultKey"`
}
// BuildOctopusMsg 生成OctopusMessage
func (m *OctopusMessage) BuildOctopusMsg(omType string, content interface{}) *OctopusMessage {

View File

@@ -0,0 +1,96 @@
package rabbitmq
import (
"agent-go/executor"
"agent-go/g"
"encoding/json"
"fmt"
)
func HandleOMsg(initOMsgFromServer *OctopusMessage) {
agentTopicName := initOMsgFromServer.UUID
OctopusExchange := g.G.NacosConfig.GetString("octopus.message.octopus_exchange")
octopusConnectProp := &ConnectProperty{
ExchangeName: OctopusExchange,
QueueName: agentTopicName,
ExchangeType: g.QueueTopic,
TopicKey: agentTopicName + "*",
}
octopusConn, err := NewRabbitMQConn(octopusConnectProp)
if err != nil {
log.Error(fmt.Sprintf("Octopus Message Queue create Error ! => %v", octopusConnectProp))
panic(err)
}
// 开始接收消息
channel := octopusConn.Channel
deliveries, err := channel.Consume(
agentTopicName,
agentTopicName,
true,
false,
false,
false,
nil,
)
if err != nil {
return
}
// 死循环处理Ocotpus Message
for delivery := range deliveries {
var om *OctopusMessage
err := json.Unmarshal(delivery.Body, &om)
if err != nil {
log.Error("Octopus Message Parse Error !")
// 保存到某处
continue
}
// 策略模式 处理消息
doHandleOctopusMessage(om)
}
}
func doHandleOctopusMessage(octopusMessage *OctopusMessage) {
switch octopusMessage.Type {
case g.InitOmType:
go func() {}()
case g.ExecOmType:
go executorOMHandler(octopusMessage)
case g.StatusOmType:
go statusOMHandler(octopusMessage)
default:
go blackHoleOMHandler(octopusMessage)
}
}
func executorOMHandler(octopusMessage *OctopusMessage) {
executionMsgString := octopusMessage.Content.(string)
var executionMessage *ExecutionMessage
err := json.Unmarshal([]byte(executionMsgString), &executionMessage)
if err != nil {
return
}
// 交给后端的实际处理器处理, 再次策略
executor.Execute(octopusMessage, executionMessage)
}
func statusOMHandler(octopusMessage *OctopusMessage) {
}
func blackHoleOMHandler(octopusMessage *OctopusMessage) {
log.Error(fmt.Sprintf("octopusMessage type wrong! msg is => %v", octopusMessage))
}