-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathcommands.taskqueue.go
More file actions
413 lines (373 loc) · 12.4 KB
/
commands.taskqueue.go
File metadata and controls
413 lines (373 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package temporalcli
import (
"fmt"
"time"
"github.com/fatih/color"
"github.com/temporalio/cli/internal/printer"
commonpb "go.temporal.io/api/common/v1"
"go.temporal.io/api/enums/v1"
"go.temporal.io/api/taskqueue/v1"
"go.temporal.io/api/workflowservice/v1"
"go.temporal.io/sdk/client"
"go.temporal.io/server/common/tqid"
)
const taskQueueUnversioned = "UNVERSIONED"
type taskQueueReachabilityRowType struct {
BuildID string `json:"buildId"`
Reachability string `json:"reachability"`
}
type pollerRowType struct {
BuildID string `json:"buildId"`
TaskQueueType string `json:"taskQueueType"`
Identity string `json:"identity"`
LastAccessTime time.Time `json:"lastAccessTime"`
RatePerSecond float64 `json:"ratePerSecond"`
}
type statsRowType struct {
BuildID string `json:"buildId"`
TaskQueueType string `json:"taskQueueType"`
ApproximateBacklogCount int64 `json:"approximateBacklogCount"`
ApproximateBacklogAge string `json:"approximateBacklogAge"`
BacklogIncreaseRate float32 `json:"backlogIncreaseRate"`
TasksAddRate float32 `json:"tasksAddRate"`
TasksDispatchRate float32 `json:"tasksDispatchRate"`
}
type taskQueueDescriptionType struct {
Reachability []taskQueueReachabilityRowType `json:"reachability"`
Pollers []pollerRowType `json:"pollers"`
Stats []statsRowType `json:"stats"`
}
func reachabilityToStr(reachability client.BuildIDTaskReachability) (string, error) {
switch reachability {
case client.BuildIDTaskReachabilityUnspecified:
return "unspecified", nil
case client.BuildIDTaskReachabilityReachable:
return "reachable", nil
case client.BuildIDTaskReachabilityClosedWorkflowsOnly:
return "closedWorkflowsOnly", nil
case client.BuildIDTaskReachabilityUnreachable:
return "unreachable", nil
default:
return "", fmt.Errorf("unrecognized reachability type: %d", reachability)
}
}
func descriptionToReachabilityRows(taskQueueDescription client.TaskQueueDescription) ([]taskQueueReachabilityRowType, error) {
var rRows []taskQueueReachabilityRowType
// Unversioned queue first
val, ok := taskQueueDescription.VersionsInfo[client.UnversionedBuildID]
if ok {
reachability, err := reachabilityToStr(val.TaskReachability)
if err != nil {
return nil, err
}
rRows = append(rRows, taskQueueReachabilityRowType{
BuildID: taskQueueUnversioned,
Reachability: reachability,
})
}
for k, val := range taskQueueDescription.VersionsInfo {
if k != client.UnversionedBuildID {
reachability, err := reachabilityToStr(val.TaskReachability)
if err != nil {
return nil, err
}
rRows = append(rRows, taskQueueReachabilityRowType{
BuildID: k,
Reachability: reachability,
})
}
}
return rRows, nil
}
func taskQueueTypeToStr(taskQueueType client.TaskQueueType) (string, error) {
switch taskQueueType {
case client.TaskQueueTypeUnspecified:
return "unspecified", nil
case client.TaskQueueTypeWorkflow:
return "workflow", nil
case client.TaskQueueTypeActivity:
return "activity", nil
case client.TaskQueueTypeNexus:
return "nexus", nil
default:
return "", fmt.Errorf("unrecognized task queue type: %d", taskQueueType)
}
}
func buildIDToPollerRows(pRows []pollerRowType, buildID string, typesInfo map[client.TaskQueueType]client.TaskQueueTypeInfo) ([]pollerRowType, error) {
for t, info := range typesInfo {
taskQueueType, err := taskQueueTypeToStr(t)
if err != nil {
return pRows, err
}
for _, p := range info.Pollers {
pRows = append(pRows, pollerRowType{
BuildID: buildID,
TaskQueueType: taskQueueType,
Identity: p.Identity,
LastAccessTime: p.LastAccessTime,
RatePerSecond: p.RatePerSecond,
})
}
}
return pRows, nil
}
func descriptionToPollerRows(taskQueueDescription client.TaskQueueDescription) ([]pollerRowType, error) {
var pRows []pollerRowType
var err error
// Unversioned queue first
val, ok := taskQueueDescription.VersionsInfo[client.UnversionedBuildID]
if ok {
pRows, err = buildIDToPollerRows(pRows, taskQueueUnversioned, val.TypesInfo)
if err != nil {
return nil, err
}
}
for k, val := range taskQueueDescription.VersionsInfo {
if k != client.UnversionedBuildID {
pRows, err = buildIDToPollerRows(pRows, k, val.TypesInfo)
if err != nil {
return nil, err
}
}
}
return pRows, nil
}
func buildIDToStatsRows(statsRows []statsRowType, buildID string, typesInfo map[client.TaskQueueType]client.TaskQueueTypeInfo) ([]statsRowType, error) {
for t, info := range typesInfo {
taskQueueType, err := taskQueueTypeToStr(t)
if err != nil {
return statsRows, err
}
stats := statsRowType{
BuildID: buildID,
TaskQueueType: taskQueueType,
}
if info.Stats != nil {
stats.ApproximateBacklogCount = info.Stats.ApproximateBacklogCount
stats.BacklogIncreaseRate = info.Stats.BacklogIncreaseRate
stats.TasksAddRate = info.Stats.TasksAddRate
stats.TasksDispatchRate = info.Stats.TasksDispatchRate
stats.ApproximateBacklogAge = formatDuration(info.Stats.ApproximateBacklogAge)
}
statsRows = append(statsRows, stats)
}
return statsRows, nil
}
func descriptionToStatsRows(taskQueueDescription client.TaskQueueDescription) ([]statsRowType, error) {
var statsRows []statsRowType
var err error
// Unversioned queue first
val, ok := taskQueueDescription.VersionsInfo[client.UnversionedBuildID]
if ok {
statsRows, err = buildIDToStatsRows(statsRows, taskQueueUnversioned, val.TypesInfo)
if err != nil {
return nil, err
}
}
// Versioned queues
for k, val := range taskQueueDescription.VersionsInfo {
if k != client.UnversionedBuildID {
statsRows, err = buildIDToStatsRows(statsRows, k, val.TypesInfo)
if err != nil {
return nil, err
}
}
}
return statsRows, nil
}
func taskQueueDescriptionToRows(taskQueueDescription client.TaskQueueDescription, reportReachability bool, disableStats bool) (taskQueueDescriptionType, error) {
var rRows []taskQueueReachabilityRowType
var statsRows []statsRowType
if reportReachability {
var err error
rRows, err = descriptionToReachabilityRows(taskQueueDescription)
if err != nil {
return taskQueueDescriptionType{}, err
}
}
if !disableStats {
var err error
statsRows, err = descriptionToStatsRows(taskQueueDescription)
if err != nil {
return taskQueueDescriptionType{}, err
}
}
pRows, err := descriptionToPollerRows(taskQueueDescription)
if err != nil {
return taskQueueDescriptionType{}, err
}
return taskQueueDescriptionType{
Reachability: rRows,
Pollers: pRows,
Stats: statsRows,
}, nil
}
func printTaskQueueDescription(cctx *CommandContext, taskQueueDescription client.TaskQueueDescription, reportReachability bool, disableStats bool) error {
descRows, err := taskQueueDescriptionToRows(taskQueueDescription, reportReachability, disableStats)
if err != nil {
return fmt.Errorf("creating task queue description rows failed: %w", err)
}
if !cctx.JSONOutput {
if reportReachability {
cctx.Printer.Println(color.MagentaString("Task Reachability:"))
err = cctx.Printer.PrintStructured(descRows.Reachability, printer.StructuredOptions{Table: &printer.TableOptions{}})
if err != nil {
return fmt.Errorf("displaying reachability failed: %w", err)
}
}
if !cctx.JSONOutput {
if !disableStats {
cctx.Printer.Println(color.MagentaString("Task Queue Statistics:"))
err = cctx.Printer.PrintStructured(descRows.Stats, printer.StructuredOptions{Table: &printer.TableOptions{}})
if err != nil {
return fmt.Errorf("displaying task queue statistics failed: %w", err)
}
}
}
cctx.Printer.Println(color.MagentaString("Pollers:"))
return cctx.Printer.PrintStructured(descRows.Pollers, printer.StructuredOptions{Table: &printer.TableOptions{}})
}
// json output
return cctx.Printer.PrintStructured(descRows, printer.StructuredOptions{})
}
func (c *TemporalTaskQueueDescribeCommand) run(cctx *CommandContext, args []string) error {
// Call describe
cl, err := dialClient(cctx, &c.Parent.ClientOptions)
if err != nil {
return err
}
defer cl.Close()
var taskQueueType enums.TaskQueueType
switch c.TaskQueueType.Value {
case "workflow":
taskQueueType = enums.TASK_QUEUE_TYPE_WORKFLOW
case "activity":
taskQueueType = enums.TASK_QUEUE_TYPE_ACTIVITY
default:
return fmt.Errorf("unrecognized task queue type: %q", c.TaskQueueType.Value)
}
taskQueue, err := tqid.NewTaskQueueFamily(c.Parent.Namespace, c.TaskQueue)
if err != nil {
return fmt.Errorf("failed to parse task queue name: %w", err)
}
partitions := c.Partitions
type statusWithPartition struct {
Partition int `json:"partition"`
taskqueue.TaskQueueStatus
}
type pollerWithPartition struct {
Partition int `json:"partition"`
taskqueue.PollerInfo
// copy this out to display nicer in table or card, but not json
Versioning *commonpb.WorkerVersionCapabilities `json:"-"`
}
var statuses []*statusWithPartition
var pollers []*pollerWithPartition
var config *taskqueue.TaskQueueConfig
// TODO: remove this when the server does partition fan-out
for p := 0; p < partitions; p++ {
resp, err := cl.WorkflowService().DescribeTaskQueue(cctx, &workflowservice.DescribeTaskQueueRequest{
Namespace: c.Parent.Namespace,
TaskQueue: &taskqueue.TaskQueue{
Name: taskQueue.TaskQueue(taskQueueType).NormalPartition(p).RpcName(),
Kind: enums.TASK_QUEUE_KIND_NORMAL,
},
TaskQueueType: taskQueueType,
IncludeTaskQueueStatus: true,
ReportConfig: c.ReportConfig,
})
if err != nil {
return fmt.Errorf("unable to describe task queue: %w", err)
}
statuses = append(statuses, &statusWithPartition{
Partition: p,
TaskQueueStatus: *resp.TaskQueueStatus,
})
for _, pi := range resp.Pollers {
pollers = append(pollers, &pollerWithPartition{
Partition: p,
PollerInfo: *pi,
Versioning: pi.WorkerVersionCapabilities,
})
}
// Capture config from the first partition (they should all be the same)
if p == 0 && resp.Config != nil {
config = resp.Config
}
}
// For JSON, we'll just dump the proto
if cctx.JSONOutput {
output := map[string]any{
"taskQueues": statuses,
"pollers": pollers,
}
// Include config if requested
if c.ReportConfig && config != nil {
output["config"] = config
}
return cctx.Printer.PrintStructured(output, printer.StructuredOptions{})
}
// For text, we will use a table for pollers
cctx.Printer.Println(color.MagentaString("Pollers:"))
items := make([]struct {
Identity string
LastAccessTime time.Time
RatePerSecond float64
}, len(pollers))
for i, poller := range pollers {
items[i].Identity = poller.Identity
items[i].LastAccessTime = poller.LastAccessTime.AsTime()
items[i].RatePerSecond = poller.RatePerSecond
}
err = cctx.Printer.PrintStructured(items, printer.StructuredOptions{Table: &printer.TableOptions{}})
if err != nil {
return err
}
// Display config if requested
if c.ReportConfig && config != nil {
cctx.Printer.Println(color.MagentaString("\nTask Queue Configuration:"))
return printTaskQueueConfig(cctx, config)
}
return nil
}
func (c *TemporalTaskQueueListPartitionCommand) run(cctx *CommandContext, args []string) error {
cl, err := dialClient(cctx, &c.Parent.ClientOptions)
if err != nil {
return err
}
defer cl.Close()
request := &workflowservice.ListTaskQueuePartitionsRequest{
Namespace: c.Parent.Namespace,
TaskQueue: &taskqueue.TaskQueue{
Name: c.TaskQueue,
Kind: enums.TASK_QUEUE_KIND_NORMAL,
},
}
resp, err := cl.WorkflowService().ListTaskQueuePartitions(cctx, request)
if err != nil {
return fmt.Errorf("unable to list task queues: %w", err)
}
if cctx.JSONOutput {
return cctx.Printer.PrintStructured(resp, printer.StructuredOptions{})
}
var items []*taskqueue.TaskQueuePartitionMetadata
cctx.Printer.Println(color.MagentaString("Workflow Task Queue Partitions\n"))
for _, e := range resp.WorkflowTaskQueuePartitions {
items = append(items, e)
}
_ = cctx.Printer.PrintStructured(items, printer.StructuredOptions{Table: &printer.TableOptions{}})
items = items[:0]
cctx.Printer.Println(color.MagentaString("\nActivity Task Queue Partitions\n"))
for _, e := range resp.ActivityTaskQueuePartitions {
items = append(items, e)
}
_ = cctx.Printer.PrintStructured(items, printer.StructuredOptions{Table: &printer.TableOptions{}})
return nil
}
// Helper function to truncate strings
func truncateString(s string, maxLength int) string {
if len(s) <= maxLength {
return s
}
return s[:maxLength-3] + "..."
}