[agent-go] 初步完成Executor部分的代码
This commit is contained in:
46
agent-go/rabbitmq/MessageReaderWriter.go
Normal file
46
agent-go/rabbitmq/MessageReaderWriter.go
Normal 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
|
||||
}
|
||||
@@ -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,
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
|
||||
96
agent-go/rabbitmq/OctopusMsgHandler.go
Normal file
96
agent-go/rabbitmq/OctopusMsgHandler.go
Normal 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))
|
||||
}
|
||||
Reference in New Issue
Block a user