Skip to content

Commit d53e68f

Browse files
0x2b3bfa0tasdomasdacbd
authored
Implement read --follow (#700)
* Implement `read --follow` * Avoid timeout on `--watch` * Simplify logic * Improve timeout logic * Apply suggestions from code review Co-authored-by: Domas Monkus <[email protected]> Co-authored-by: Domas Monkus <[email protected]> Co-authored-by: Daniel Barnes <[email protected]>
1 parent 110a740 commit d53e68f

File tree

4 files changed

+69
-55
lines changed

4 files changed

+69
-55
lines changed

cmd/leo/read/read.go

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package read
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"strings"
8+
"time"
79

810
"github.com/sirupsen/logrus"
911
"github.com/spf13/cobra"
@@ -12,12 +14,19 @@ import (
1214
"terraform-provider-iterative/task/common"
1315
)
1416

17+
type status string
18+
19+
const (
20+
statusQueued status = "queued"
21+
statusSucceeded status = "succeeded"
22+
statusFailed status = "failed"
23+
statusRunning status = "running"
24+
)
25+
1526
type Options struct {
1627
Parallelism int
1728
Timestamps bool
18-
Status bool
19-
Events bool
20-
Logs bool
29+
Follow bool
2130
}
2231

2332
func New(cloud *common.Cloud) *cobra.Command {
@@ -35,9 +44,7 @@ func New(cloud *common.Cloud) *cobra.Command {
3544

3645
cmd.Flags().IntVar(&o.Parallelism, "parallelism", 1, "parallelism")
3746
cmd.Flags().BoolVar(&o.Timestamps, "timestamps", false, "display timestamps")
38-
cmd.Flags().BoolVar(&o.Status, "status", true, "read status")
39-
cmd.Flags().BoolVar(&o.Logs, "logs", false, "read logs")
40-
cmd.MarkFlagsMutuallyExclusive("status", "logs")
47+
cmd.Flags().BoolVar(&o.Follow, "follow", false, "follow logs")
4148

4249
return cmd
4350
}
@@ -50,7 +57,6 @@ func (o *Options) Run(cmd *cobra.Command, args []string, cloud *common.Cloud) er
5057
}
5158

5259
ctx, cancel := context.WithTimeout(context.Background(), cloud.Timeouts.Read)
53-
defer cancel()
5460

5561
id, err := common.ParseIdentifier(args[0])
5662
if err != nil {
@@ -62,39 +68,70 @@ func (o *Options) Run(cmd *cobra.Command, args []string, cloud *common.Cloud) er
6268
return err
6369
}
6470

65-
if err := tsk.Read(ctx); err != nil {
66-
return err
67-
}
71+
var last int
72+
for {
73+
if err := tsk.Read(ctx); err != nil {
74+
return err
75+
}
6876

69-
switch {
70-
case o.Logs:
71-
return o.printLogs(ctx, tsk)
72-
case o.Status:
73-
return o.printStatus(ctx, tsk)
74-
}
77+
logs, err := o.getLogs(ctx, tsk)
78+
if err != nil {
79+
return err
80+
}
81+
82+
status, err := o.getStatus(ctx, tsk)
83+
if err != nil {
84+
return err
85+
}
7586

76-
return nil
87+
if delta := strings.Join(logs[last:], "\n"); delta != "" {
88+
fmt.Println(delta)
89+
last = len(logs)
90+
}
91+
92+
switch o.Follow {
93+
case true:
94+
// disable debug logs for subsequent iterations
95+
logrus.SetLevel(logrus.WarnLevel)
96+
// create a new context to reset timeout on every iteration
97+
ctx, cancel = context.WithTimeout(context.Background(), cloud.Timeouts.Read)
98+
defer cancel()
99+
case false:
100+
return nil
101+
}
102+
103+
switch status {
104+
case statusSucceeded:
105+
os.Exit(0)
106+
case statusFailed:
107+
os.Exit(1)
108+
default:
109+
time.Sleep(3 * time.Second)
110+
}
111+
}
77112
}
78113

79-
func (o *Options) printLogs(ctx context.Context, tsk task.Task) error {
114+
func (o *Options) getLogs(ctx context.Context, tsk task.Task) ([]string, error) {
80115
logs, err := tsk.Logs(ctx)
81116
if err != nil {
82-
return err
117+
return nil, err
83118
}
84119

120+
var result []string
121+
85122
for _, log := range logs {
86123
for _, line := range strings.Split(strings.Trim(log, "\n"), "\n") {
87124
if !o.Timestamps {
88125
_, line, _ = strings.Cut(line, " ")
89126
}
90-
fmt.Println(line)
127+
result = append(result, line)
91128
}
92129
}
93130

94-
return nil
131+
return result, nil
95132
}
96133

97-
func (o *Options) printStatus(ctx context.Context, tsk task.Task) error {
134+
func (o *Options) getStatus(ctx context.Context, tsk task.Task) (status, error) {
98135
for _, event := range tsk.Events(ctx) {
99136
line := fmt.Sprintf("%s: %s", event.Code, strings.Join(event.Description, " "))
100137
if o.Timestamps {
@@ -106,21 +143,21 @@ func (o *Options) printStatus(ctx context.Context, tsk task.Task) error {
106143

107144
status, err := tsk.Status(ctx)
108145
if err != nil {
109-
return err
146+
return "", err
110147
}
111148

112-
message := "queued"
149+
result := statusQueued
113150

114-
if status["succeeded"] >= o.Parallelism {
115-
message = "succeeded"
151+
if status[common.StatusCodeSucceeded] >= o.Parallelism {
152+
result = statusSucceeded
116153
}
117-
if status["failed"] > 0 {
118-
message = "failed"
154+
if status[common.StatusCodeFailed] > 0 {
155+
result = statusFailed
119156
}
120-
if status["running"] >= o.Parallelism {
121-
message = "running"
157+
if status[common.StatusCodeActive] >= o.Parallelism {
158+
result = statusRunning
122159
}
123160

124-
fmt.Println(message)
125-
return nil
161+
logrus.Debug(result)
162+
return result, nil
126163
}

task/aws/task.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,6 @@ func (t *Task) Delete(ctx context.Context) error {
299299
}
300300

301301
func (t *Task) Logs(ctx context.Context) ([]string, error) {
302-
if err := t.Read(ctx); err != nil {
303-
return nil, err
304-
}
305-
306302
return machine.Logs(ctx, t.DataSources.Credentials.Resource["RCLONE_REMOTE"])
307303
}
308304

@@ -346,10 +342,6 @@ func (t *Task) Events(ctx context.Context) []common.Event {
346342
}
347343

348344
func (t *Task) Status(ctx context.Context) (common.Status, error) {
349-
if err := t.Read(ctx); err != nil {
350-
return nil, err
351-
}
352-
353345
return machine.Status(ctx, t.DataSources.Credentials.Resource["RCLONE_REMOTE"], t.Attributes.Status)
354346
}
355347

task/az/task.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,6 @@ func (t *Task) Delete(ctx context.Context) error {
291291
}
292292

293293
func (t *Task) Logs(ctx context.Context) ([]string, error) {
294-
if err := t.Read(ctx); err != nil {
295-
return nil, err
296-
}
297-
298294
return machine.Logs(ctx, t.DataSources.Credentials.Resource["RCLONE_REMOTE"])
299295
}
300296

@@ -338,9 +334,6 @@ func (t *Task) Events(ctx context.Context) []common.Event {
338334
}
339335

340336
func (t *Task) Status(ctx context.Context) (common.Status, error) {
341-
if err := t.Read(ctx); err != nil {
342-
return nil, err
343-
}
344337
return machine.Status(ctx, t.DataSources.Credentials.Resource["RCLONE_REMOTE"], t.Attributes.Status)
345338
}
346339

task/gcp/task.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,6 @@ func (t *Task) Delete(ctx context.Context) error {
371371
}
372372

373373
func (t *Task) Logs(ctx context.Context) ([]string, error) {
374-
if err := t.Read(ctx); err != nil {
375-
return nil, err
376-
}
377-
378374
return machine.Logs(ctx, t.DataSources.Credentials.Resource["RCLONE_REMOTE"])
379375
}
380376

@@ -418,10 +414,6 @@ func (t *Task) Events(ctx context.Context) []common.Event {
418414
}
419415

420416
func (t *Task) Status(ctx context.Context) (common.Status, error) {
421-
if err := t.Read(ctx); err != nil {
422-
return nil, err
423-
}
424-
425417
return machine.Status(ctx, t.DataSources.Credentials.Resource["RCLONE_REMOTE"], t.Attributes.Status)
426418
}
427419

0 commit comments

Comments
 (0)