@@ -12,7 +12,7 @@ import (
12
12
)
13
13
14
14
var (
15
- DefaultRequestTimeout = 10 * time .Second
15
+ DefaultRequestTimeout = 5 * time .Second
16
16
)
17
17
18
18
type ControlAPIClient struct {
@@ -31,7 +31,7 @@ func (c *ControlAPIClient) Auction(namespace string, tags map[string]string) ([]
31
31
resp := []* nodegen.AuctionResponseJson {}
32
32
auctionRespInbox := nats .NewInbox ()
33
33
34
- _ , err := c .nc .Subscribe (auctionRespInbox , func (m * nats.Msg ) {
34
+ s , err := c .nc .Subscribe (auctionRespInbox , func (m * nats.Msg ) {
35
35
envelope := new (models.Envelope [nodegen.AuctionResponseJson ])
36
36
err := json .Unmarshal (m .Data , envelope )
37
37
if err != nil {
@@ -43,19 +43,25 @@ func (c *ControlAPIClient) Auction(namespace string, tags map[string]string) ([]
43
43
if err != nil {
44
44
return nil , err
45
45
}
46
+ defer func () {
47
+ err = s .Drain ()
48
+ if err != nil {
49
+ c .logger .Error ("failed to drain subscription" , slog .Any ("err" , err ))
50
+ }
51
+ }()
46
52
47
53
req := nodegen.AuctionRequestJson {
48
54
AuctionId : nuid .New ().Next (),
49
55
Tags : nodegen.AuctionRequestJsonTags {
50
56
Tags : tags ,
51
57
},
52
58
}
53
- req_b , err := json .Marshal (req )
59
+ reqB , err := json .Marshal (req )
54
60
if err != nil {
55
61
return nil , err
56
62
}
57
63
58
- err = c .nc .PublishRequest (models .AuctionRequestSubject (namespace ), auctionRespInbox , req_b )
64
+ err = c .nc .PublishRequest (models .AuctionRequestSubject (namespace ), auctionRespInbox , reqB )
59
65
if err != nil {
60
66
return nil , err
61
67
}
@@ -68,7 +74,7 @@ func (c *ControlAPIClient) Ping() ([]*nodegen.NodePingResponseJson, error) {
68
74
resp := []* nodegen.NodePingResponseJson {}
69
75
pingRespInbox := nats .NewInbox ()
70
76
71
- _ , err := c .nc .Subscribe (pingRespInbox , func (m * nats.Msg ) {
77
+ s , err := c .nc .Subscribe (pingRespInbox , func (m * nats.Msg ) {
72
78
envelope := new (models.Envelope [nodegen.NodePingResponseJson ])
73
79
err := json .Unmarshal (m .Data , envelope )
74
80
if err != nil {
@@ -80,6 +86,12 @@ func (c *ControlAPIClient) Ping() ([]*nodegen.NodePingResponseJson, error) {
80
86
if err != nil {
81
87
return nil , err
82
88
}
89
+ defer func () {
90
+ err = s .Drain ()
91
+ if err != nil {
92
+ c .logger .Error ("failed to drain subscription" , slog .Any ("err" , err ))
93
+ }
94
+ }()
83
95
84
96
err = c .nc .PublishRequest (models .PingSubject (), pingRespInbox , nil )
85
97
if err != nil {
@@ -104,8 +116,8 @@ func (c *ControlAPIClient) DirectPing(nodeId string) (*nodegen.NodePingResponseJ
104
116
return resp , nil
105
117
}
106
118
107
- func (c * ControlAPIClient ) FindWorkload (inType , namespace , workloadId string ) (* nodegen.WorkloadPingResponseJson , error ) {
108
- msg , err := c .nc .Request (models .WorkloadPingRequestSubject (inType , namespace , workloadId ), nil , DefaultRequestTimeout )
119
+ func (c * ControlAPIClient ) FindWorkload (namespace , workloadId string ) (* nodegen.WorkloadPingResponseJson , error ) {
120
+ msg , err := c .nc .Request (models .WorkloadPingRequestSubject (namespace , workloadId ), nil , DefaultRequestTimeout )
109
121
if err != nil {
110
122
return nil , err
111
123
}
@@ -119,12 +131,44 @@ func (c *ControlAPIClient) FindWorkload(inType, namespace, workloadId string) (*
119
131
return & envelope .Data , nil
120
132
}
121
133
134
+ func (c * ControlAPIClient ) ListWorkloads (namespace string ) ([]nodegen.WorkloadSummary , error ) {
135
+ workloadsInbox := nats .NewInbox ()
136
+
137
+ var ret []nodegen.WorkloadSummary
138
+ s , err := c .nc .Subscribe (workloadsInbox , func (m * nats.Msg ) {
139
+ envelope := new (models.Envelope [[]nodegen.WorkloadSummary ])
140
+ err := json .Unmarshal (m .Data , envelope )
141
+ if err != nil {
142
+ c .logger .Error ("failed to unmarshal workloads response" , slog .Any ("err" , err ), slog .String ("data" , string (m .Data )))
143
+ return
144
+ }
145
+ ret = append (ret , envelope .Data ... )
146
+ })
147
+ if err != nil {
148
+ return nil , err
149
+ }
150
+ defer func () {
151
+ err = s .Drain ()
152
+ if err != nil {
153
+ c .logger .Error ("failed to drain subscription" , slog .Any ("err" , err ))
154
+ }
155
+ }()
156
+
157
+ err = c .nc .PublishRequest (models .NamespacePingRequestSubject (namespace ), workloadsInbox , nil )
158
+ if err != nil {
159
+ return nil , err
160
+ }
161
+
162
+ time .Sleep (5 * time .Second )
163
+ return ret , nil
164
+ }
165
+
122
166
func (c * ControlAPIClient ) AuctionDeployWorkload (namespace , bidderId string , req nodegen.StartWorkloadRequestJson ) (* nodegen.StartWorkloadResponseJson , error ) {
123
- req_b , err := json .Marshal (req )
167
+ reqB , err := json .Marshal (req )
124
168
if err != nil {
125
169
return nil , err
126
170
}
127
- msg , err := c .nc .Request (models .AuctionDeployRequestSubject (namespace , bidderId ), req_b , DefaultRequestTimeout )
171
+ msg , err := c .nc .Request (models .AuctionDeployRequestSubject (namespace , bidderId ), reqB , 30 * time . Second )
128
172
if err != nil {
129
173
return nil , err
130
174
}
@@ -139,12 +183,12 @@ func (c *ControlAPIClient) AuctionDeployWorkload(namespace, bidderId string, req
139
183
}
140
184
141
185
func (c * ControlAPIClient ) DeployWorkload (namespace , nodeId string , req nodegen.StartWorkloadRequestJson ) (* nodegen.StartWorkloadResponseJson , error ) {
142
- req_b , err := json .Marshal (req )
186
+ reqB , err := json .Marshal (req )
143
187
if err != nil {
144
188
return nil , err
145
189
}
146
190
147
- msg , err := c .nc .Request (models .DirectDeploySubject (nodeId ), req_b , DefaultRequestTimeout )
191
+ msg , err := c .nc .Request (models .DirectDeploySubject (nodeId ), reqB , 30 * time . Second )
148
192
if err != nil {
149
193
return nil , err
150
194
}
@@ -158,13 +202,8 @@ func (c *ControlAPIClient) DeployWorkload(namespace, nodeId string, req nodegen.
158
202
return & envelope .Data , nil
159
203
}
160
204
161
- func (c * ControlAPIClient ) UndeployWorkload (namespace , nodeId , workloadId string , req nodegen.StopWorkloadRequestJson ) (* nodegen.StopWorkloadResponseJson , error ) {
162
- req_b , err := json .Marshal (req )
163
- if err != nil {
164
- return nil , err
165
- }
166
-
167
- msg , err := c .nc .Request (models .UndeployRequestSubject (namespace , nodeId ), req_b , DefaultRequestTimeout )
205
+ func (c * ControlAPIClient ) UndeployWorkload (namespace , workloadId string ) (* nodegen.StopWorkloadResponseJson , error ) {
206
+ msg , err := c .nc .Request (models .UndeployRequestSubject (namespace , workloadId ), nil , DefaultRequestTimeout )
168
207
if err != nil {
169
208
return nil , err
170
209
}
@@ -178,8 +217,13 @@ func (c *ControlAPIClient) UndeployWorkload(namespace, nodeId, workloadId string
178
217
return & envelope .Data , nil
179
218
}
180
219
181
- func (c * ControlAPIClient ) GetInfo (nodeId , namespace string ) (* nodegen.NodeInfoResponseJson , error ) {
182
- msg , err := c .nc .Request (models .InfoRequestSubject (namespace , nodeId ), nil , DefaultRequestTimeout )
220
+ func (c * ControlAPIClient ) GetInfo (nodeId string , req nodegen.NodeInfoRequestJson ) (* nodegen.NodeInfoResponseJson , error ) {
221
+ reqB , err := json .Marshal (req )
222
+ if err != nil {
223
+ return nil , err
224
+ }
225
+
226
+ msg , err := c .nc .Request (models .InfoSubject (nodeId ), reqB , DefaultRequestTimeout )
183
227
if err != nil {
184
228
return nil , err
185
229
}
@@ -198,12 +242,12 @@ func (c *ControlAPIClient) SetLameDuck(nodeId string, delay time.Duration) (*nod
198
242
Delay : delay .String (),
199
243
}
200
244
201
- req_b , err := json .Marshal (req )
245
+ reqB , err := json .Marshal (req )
202
246
if err != nil {
203
247
return nil , err
204
248
}
205
249
206
- msg , err := c .nc .Request (models .LameduckSubject (nodeId ), req_b , DefaultRequestTimeout )
250
+ msg , err := c .nc .Request (models .LameduckSubject (nodeId ), reqB , DefaultRequestTimeout )
207
251
if err != nil {
208
252
return nil , err
209
253
}
@@ -217,9 +261,9 @@ func (c *ControlAPIClient) SetLameDuck(nodeId string, delay time.Duration) (*nod
217
261
return & envelope .Data , nil
218
262
}
219
263
220
- func (c * ControlAPIClient ) MonitorLogs (workloadId , level string ) (chan []byte , error ) {
264
+ func (c * ControlAPIClient ) MonitorLogs (namespace , workloadId , level string ) (chan []byte , error ) {
221
265
subject := models .LOGS_SUBJECT
222
- f_subject , err := subject .Filter (workloadId , level )
266
+ f_subject , err := subject .Filter (namespace , workloadId , level )
223
267
if err != nil {
224
268
return nil , err
225
269
}
@@ -235,9 +279,9 @@ func (c *ControlAPIClient) MonitorLogs(workloadId, level string) (chan []byte, e
235
279
return ret , nil
236
280
}
237
281
238
- func (c * ControlAPIClient ) MonitorEvents (workloadId , eventType string ) (chan * json.RawMessage , error ) {
282
+ func (c * ControlAPIClient ) MonitorEvents (namespace , workloadId , eventType string ) (chan * json.RawMessage , error ) {
239
283
subject := models .EVENTS_SUBJECT
240
- f_subject , err := subject .Filter (workloadId , eventType )
284
+ f_subject , err := subject .Filter (namespace , workloadId , eventType )
241
285
if err != nil {
242
286
return nil , err
243
287
}
@@ -264,12 +308,12 @@ func (c *ControlAPIClient) CopyWorkload(workloadId, namespace string, targetXkey
264
308
NewTargetXkey : targetXkey ,
265
309
}
266
310
267
- req_b , err := json .Marshal (req )
311
+ reqB , err := json .Marshal (req )
268
312
if err != nil {
269
313
return nil , err
270
314
}
271
315
272
- msg , err := c .nc .Request (models .CloneWorkloadRequestSubject (namespace , workloadId ), req_b , DefaultRequestTimeout )
316
+ msg , err := c .nc .Request (models .CloneWorkloadRequestSubject (namespace , workloadId ), reqB , DefaultRequestTimeout )
273
317
if err != nil {
274
318
return nil , err
275
319
}
0 commit comments