Skip to content

Commit 46377f8

Browse files
committed
Tailing logs from CloudWatch for executed process
1 parent ae150df commit 46377f8

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

pkg/ecs/exec.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ func (s *Service) Execute(cmd []string, wait bool, dockerImageTag string) {
122122
fmt.Printf("The task definition %s is used", taskDef)
123123
}
124124

125+
fmt.Println()
126+
125127
output, err := svc.RunTask(ctx, &ecs.RunTaskInput{
126128
Cluster: &s.Cluster,
127129
TaskDefinition: &taskDef,
@@ -146,24 +148,26 @@ func (s *Service) Execute(cmd []string, wait bool, dockerImageTag string) {
146148
}
147149

148150
executedTask := output.Tasks[0]
151+
var lastTimestamp *int64 = nil
152+
153+
fmt.Printf("Task %s executed", *executedTask.TaskArn)
154+
fmt.Println()
149155

150-
log.Printf("Task %s executed", *executedTask.TaskArn)
151156
if wait {
152157
for {
158+
logsOutput, _ := s.printProcessLogs(ctx, tdef.LogGroup, tdef.LogStreamPrefix, *executedTask.TaskArn, tdef.Name, lastTimestamp)
159+
lastTimestamp = &logsOutput.lastEventTimestamp
160+
153161
success, err := s.wait(ctx, svc, *executedTask.TaskArn)
154162
if err != nil {
155-
logsErr := s.printProcessLogs(ctx, tdef.LogGroup, tdef.LogStreamPrefix, *executedTask.TaskArn, tdef.Name)
156-
if logsErr != nil {
157-
log.Printf("Failed to fetch events from CloudWatch %v", logsErr)
158-
}
159163
log.Fatalln(err)
160164
}
161165

162166
if success {
163167
break
164168
}
165169

166-
time.Sleep(6 * time.Second)
170+
time.Sleep(5 * time.Second)
167171
}
168172

169173
fmt.Printf("task %s finished", *executedTask.TaskArn)

pkg/ecs/logs.go

+28-8
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,59 @@ package ecs
33
import (
44
"context"
55
"fmt"
6-
"log"
76

87
"github.com/aws/aws-sdk-go-v2/aws"
98
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
109
)
1110

11+
type printProcessLogsOutput struct {
12+
lastEventTimestamp int64
13+
}
14+
1215
func (s *Service) printProcessLogs(
1316
ctx context.Context, logGroupname string,
1417
logStreamPrefix string,
1518
taskArn string,
16-
name string) error {
17-
log.Printf("Loading logs for %s: %s", logGroupname, taskArn)
19+
name string,
20+
startTime *int64) (printProcessLogsOutput, error) {
1821

1922
cfg, err := s.initCfg()
2023
if err != nil {
21-
return fmt.Errorf("failed to initialize AWS configuration. (%w)", err)
24+
return printProcessLogsOutput{}, fmt.Errorf("failed to initialize AWS configuration. (%w)", err)
2225
}
2326

2427
processID := extractProcessID(taskArn)
2528
client := cloudwatchlogs.NewFromConfig(cfg)
2629

27-
output, err := client.FilterLogEvents(ctx, &cloudwatchlogs.FilterLogEventsInput{
30+
input := &cloudwatchlogs.FilterLogEventsInput{
2831
LogGroupName: aws.String(logGroupname),
2932
LogStreamNames: []string{fmt.Sprintf("%s/%s/%s", logStreamPrefix, name, processID)},
30-
})
33+
}
34+
35+
if startTime != nil {
36+
input.StartTime = startTime
37+
}
38+
39+
output, err := client.FilterLogEvents(ctx, input)
3140

3241
if err != nil {
33-
return fmt.Errorf("failed to filter log events (%w)", err)
42+
return printProcessLogsOutput{}, fmt.Errorf("failed to filter log events (%w)", err)
3443
}
3544

3645
for _, event := range output.Events {
3746
fmt.Println(*event.LogStreamName, *event.Message)
3847
}
3948

40-
return nil
49+
var lastEventTimestamp int64
50+
if len(output.Events) > 0 {
51+
lastEventTimestamp = *output.Events[len(output.Events)-1].Timestamp + 1
52+
} else if startTime != nil {
53+
lastEventTimestamp = *startTime
54+
} else {
55+
lastEventTimestamp = 0
56+
}
57+
58+
return printProcessLogsOutput{
59+
lastEventTimestamp: lastEventTimestamp,
60+
}, nil
4161
}

0 commit comments

Comments
 (0)