|
1 | 1 | package agentutils |
2 | 2 |
|
3 | | -import ( |
4 | | - "sync" |
5 | | -) |
6 | | - |
7 | 3 | // Agent事件 |
8 | 4 | type AgentEvent struct { |
9 | 5 | Name string `json:"name"` |
10 | 6 | Data interface{} `json:"data"` |
11 | 7 | } |
12 | 8 |
|
13 | | -var eventQueueMap = map[string]map[chan *AgentEvent]*AgentState{} // agentId => { chan => State } |
14 | | -var eventQueueLocker = sync.Mutex{} |
15 | | - |
16 | | -// 新Agent事件 |
| 9 | +// 获取新对象 |
17 | 10 | func NewAgentEvent(name string, data interface{}) *AgentEvent { |
18 | 11 | return &AgentEvent{ |
19 | 12 | Name: name, |
20 | 13 | Data: data, |
21 | 14 | } |
22 | 15 | } |
23 | | - |
24 | | -// 等待Agent事件 |
25 | | -func WaitAgentQueue(agentId string, agentVersion string, osName string, speed float64, ip string, c chan *AgentEvent) { |
26 | | - eventQueueLocker.Lock() |
27 | | - defer eventQueueLocker.Unlock() |
28 | | - _, found := eventQueueMap[agentId] |
29 | | - if found { |
30 | | - eventQueueMap[agentId][c] = &AgentState{ |
31 | | - Version: agentVersion, |
32 | | - OsName: osName, |
33 | | - Speed: speed, |
34 | | - IP: ip, |
35 | | - IsAvailable: true, |
36 | | - } |
37 | | - } else { |
38 | | - eventQueueMap[agentId] = map[chan *AgentEvent]*AgentState{ |
39 | | - c: { |
40 | | - Version: agentVersion, |
41 | | - OsName: osName, |
42 | | - Speed: speed, |
43 | | - IP: ip, |
44 | | - IsAvailable: true, |
45 | | - }, |
46 | | - } |
47 | | - } |
48 | | -} |
49 | | - |
50 | | -// 禁用Channel |
51 | | -func DisableAgentQueue(agentId string, c chan *AgentEvent) { |
52 | | - eventQueueLocker.Lock() |
53 | | - defer eventQueueLocker.Unlock() |
54 | | - m, found := eventQueueMap[agentId] |
55 | | - if found { |
56 | | - state, ok := m[c] |
57 | | - if ok { |
58 | | - state.IsAvailable = false |
59 | | - } |
60 | | - } |
61 | | -} |
62 | | - |
63 | | -// 删除Agent |
64 | | -func RemoveAgentQueue(agentId string, c chan *AgentEvent) { |
65 | | - eventQueueLocker.Lock() |
66 | | - defer eventQueueLocker.Unlock() |
67 | | - _, ok := eventQueueMap[agentId] |
68 | | - if ok { |
69 | | - delete(eventQueueMap[agentId], c) |
70 | | - |
71 | | - if len(eventQueueMap[agentId]) == 0 { |
72 | | - delete(eventQueueMap, agentId) |
73 | | - } |
74 | | - } |
75 | | -} |
76 | | - |
77 | | -// 发送Agent事件 |
78 | | -func PostAgentEvent(agentId string, event *AgentEvent) { |
79 | | - eventQueueLocker.Lock() |
80 | | - defer eventQueueLocker.Unlock() |
81 | | - m, found := eventQueueMap[agentId] |
82 | | - if found { |
83 | | - for c, state := range m { |
84 | | - if state.IsAvailable { |
85 | | - c <- event |
86 | | - } |
87 | | - } |
88 | | - } |
89 | | -} |
90 | | - |
91 | | -// 检查Agent是否正在运行 |
92 | | -func CheckAgentIsWaiting(agentId string) (state *AgentState, isWaiting bool) { |
93 | | - eventQueueLocker.Lock() |
94 | | - defer eventQueueLocker.Unlock() |
95 | | - queue, _ := eventQueueMap[agentId] |
96 | | - if len(queue) > 0 { |
97 | | - for _, v := range queue { |
98 | | - return v, true |
99 | | - } |
100 | | - } |
101 | | - return nil, false |
102 | | -} |
0 commit comments