Skip to content

Commit

Permalink
Wait for one-off to stop
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Jan 9, 2023
1 parent 17e5a48 commit 0c30a03
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
44 changes: 44 additions & 0 deletions awscmd/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,50 @@ type OuputEcsRunTask struct {
ID string
}

func EcsTaskWait(ctx context.Context, input *InputEcsTaskWait, w io.Writer) (*OuputEcsTaskWait, error) {
sess, err := NewSession(input.Region)
if err != nil {
return nil, err
}
svc := ecs.New(sess)

attempt := 1
for {
result, err := svc.DescribeTasksWithContext(ctx, &ecs.DescribeTasksInput{
Cluster: aws.String(input.Cluster),
Tasks: []*string{aws.String(input.ARN)},
})
if err != nil {
return nil, err
}

task := result.Tasks[0]
status := *task.LastStatus

if status == "STOPPED" {
break
} else {
fmt.Fprintf(w, "Waiting to finish (`%s%s%s`).. (Check %d, retrying in 3s)\n",
ctc.ForegroundYellow, status, ctc.Reset,
attempt,
)
}

time.Sleep(3 * time.Second)
}

return &OuputEcsTaskWait{}, nil
}

type InputEcsTaskWait struct {
Region string
Cluster string
ARN string
}

type OuputEcsTaskWait struct {
}

func ecsRegisterTaskDefinition(svc *ecs.ECS, ctx context.Context, input *inputRegisterTaskDefinition, w io.Writer) (*outputRegisterTaskDefinition, error) {
fmt.Fprintf(w, " Fetching task definition\n")
taskDefinitionOut, err := svc.DescribeTaskDefinitionWithContext(ctx, &ecs.DescribeTaskDefinitionInput{
Expand Down
19 changes: 18 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,24 @@ func main() {
}
fmt.Fprintf(
c.App.Writer,
"%sNew task started ID: `%s`%s\n",
"%sNew task started: `%s`%s\n",
ctc.ForegroundGreen,
out.ID,
ctc.Reset,
)
waitInput := &awscmd.InputEcsTaskWait{
Region: c.String("region"),
Cluster: c.String("cluster"),
ARN: out.ARN,
}
_, err = awscmd.EcsTaskWait(context.TODO(), waitInput, c.App.Writer)
if err != nil {
return err
}

fmt.Fprintf(
c.App.Writer,
"%sTask `%s` has stopped%s\n",
ctc.ForegroundGreen,
out.ID,
ctc.Reset,
Expand Down

0 comments on commit 0c30a03

Please sign in to comment.