-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtask_status.go
More file actions
197 lines (179 loc) · 4.95 KB
/
Copy pathtask_status.go
File metadata and controls
197 lines (179 loc) · 4.95 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
package cli
import (
"fmt"
"strings"
"time"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
)
func (r *RootCmd) taskStatus() *serpent.Command {
var (
formatter = cliui.NewOutputFormatter(
cliui.TableFormat(
[]taskStatusRow{},
[]string{
"state changed",
"status",
"healthy",
"state",
"message",
},
),
cliui.ChangeFormatterData(
cliui.JSONFormat(),
func(data any) (any, error) {
rows, ok := data.([]taskStatusRow)
if !ok {
return nil, xerrors.Errorf("expected []taskStatusRow, got %T", data)
}
if len(rows) != 1 {
return nil, xerrors.Errorf("expected exactly 1 row, got %d", len(rows))
}
return rows[0], nil
},
),
)
watchArg bool
watchIntervalArg time.Duration
)
cmd := &serpent.Command{
Short: "Show the status of a task.",
Long: FormatExamples(
Example{
Description: "Show the status of a given task.",
Command: "coder task status task1",
},
Example{
Description: "Watch the status of a given task until it completes (idle or stopped).",
Command: "coder task status task1 --watch",
},
),
Use: "status",
Aliases: []string{"stat"},
Options: serpent.OptionSet{
{
Default: "false",
Description: "Watch the task status output. This will stream updates to the terminal until the underlying workspace is stopped.",
Flag: "watch",
Name: "watch",
Value: serpent.BoolOf(&watchArg),
},
{
Default: "1s",
Description: "Interval to poll the task for updates. Only used in tests.",
Hidden: true,
Flag: "watch-interval",
Name: "watch-interval",
Value: serpent.DurationOf(&watchIntervalArg),
},
},
Middleware: serpent.Chain(
serpent.RequireNArgs(1),
),
Handler: func(i *serpent.Invocation) error {
client, err := r.InitClient(i)
if err != nil {
return err
}
ctx := i.Context()
identifier := i.Args[0]
task, err := client.TaskByIdentifier(ctx, identifier)
if err != nil {
return err
}
tsr := toStatusRow(task, r.clock.Now())
out, err := formatter.Format(ctx, []taskStatusRow{tsr})
if err != nil {
return xerrors.Errorf("format task status: %w", err)
}
_, _ = fmt.Fprintln(i.Stdout, out)
if !watchArg || taskWatchIsEnded(task) {
return nil
}
t := time.NewTicker(watchIntervalArg)
defer t.Stop()
// TODO: implement streaming updates instead of polling
lastStatusRow := tsr
for range t.C {
task, err := client.TaskByID(ctx, task.ID)
if err != nil {
return err
}
// Only print if something changed
newStatusRow := toStatusRow(task, r.clock.Now())
if !taskStatusRowEqual(lastStatusRow, newStatusRow) {
out, err := formatter.Format(ctx, []taskStatusRow{newStatusRow})
if err != nil {
return xerrors.Errorf("format task status: %w", err)
}
// hack: skip the extra column header from formatter
if formatter.FormatID() != cliui.JSONFormat().ID() {
out = strings.SplitN(out, "\n", 2)[1]
}
_, _ = fmt.Fprintln(i.Stdout, out)
}
if taskWatchIsEnded(task) {
return nil
}
lastStatusRow = newStatusRow
}
return nil
},
}
formatter.AttachOptions(&cmd.Options)
return cmd
}
func taskWatchIsEnded(task codersdk.Task) bool {
if task.WorkspaceStatus == codersdk.WorkspaceStatusStopped {
return true
}
if task.WorkspaceAgentHealth == nil || !task.WorkspaceAgentHealth.Healthy {
return false
}
if task.WorkspaceAgentLifecycle == nil || task.WorkspaceAgentLifecycle.Starting() || task.WorkspaceAgentLifecycle.ShuttingDown() {
return false
}
if task.CurrentState == nil || task.CurrentState.State == codersdk.TaskStateWorking {
return false
}
return true
}
type taskStatusRow struct {
codersdk.Task `table:"r,recursive_inline"`
ChangedAgo string `json:"-" table:"state changed"`
Healthy bool `json:"-" table:"healthy"`
}
func taskStatusRowEqual(r1, r2 taskStatusRow) bool {
return r1.Status == r2.Status &&
r1.Healthy == r2.Healthy &&
taskStateEqual(r1.CurrentState, r2.CurrentState)
}
func toStatusRow(task codersdk.Task, now time.Time) taskStatusRow {
tsr := taskStatusRow{
Task: task,
ChangedAgo: now.Sub(task.UpdatedAt).Truncate(time.Second).String() + " ago",
}
tsr.Healthy = task.WorkspaceAgentHealth != nil &&
task.WorkspaceAgentHealth.Healthy &&
task.WorkspaceAgentLifecycle != nil &&
!task.WorkspaceAgentLifecycle.Starting() &&
!task.WorkspaceAgentLifecycle.ShuttingDown()
if task.CurrentState != nil {
tsr.ChangedAgo = now.Sub(task.CurrentState.Timestamp).Truncate(time.Second).String() + " ago"
}
return tsr
}
func taskStateEqual(se1, se2 *codersdk.TaskStateEntry) bool {
var s1, m1, s2, m2 string
if se1 != nil {
s1 = string(se1.State)
m1 = se1.Message
}
if se2 != nil {
s2 = string(se2.State)
m2 = se2.Message
}
return s1 == s2 && m1 == m2
}