Skip to content

Commit cd27b6e

Browse files
authored
Running synchronous processes (#1)
Using the -w / --wait parameter, it waits for the task to finish.
1 parent 6af8716 commit cd27b6e

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
RunECS is a tool for running one-off processes in an ECS cluster. The tool was created as a simple solution for occasional running of processes in the ECS cluster - e.g. various data migrations. Currently only the FARGATE launch type is supported.
44

5+
The process can be started asynchronously (does not wait for finish) or synchronously with the `-w` parameter (waits for task finish).
6+
57
## How to Use
68

79
The ECS cluster settings are located in the `~/.runecs.yml` file, which is located in the user's home directory. The default profile is called `default` and is automatically used unless explicitly specified otherwise.

cmd/main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import (
2626
)
2727

2828
var (
29-
profile string
30-
verbose bool
29+
profile string
30+
verbose bool
31+
execWait bool
3132
)
3233

3334
var runCmd = &cobra.Command{
@@ -36,7 +37,7 @@ var runCmd = &cobra.Command{
3637
Args: cobra.MinimumNArgs(1),
3738
Run: func(cmd *cobra.Command, args []string) {
3839
svc := initService()
39-
svc.Execute(args)
40+
svc.Execute(args, execWait)
4041
},
4142
}
4243

@@ -58,6 +59,8 @@ func init() {
5859
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "", false, "verbose output")
5960
rootCmd.CompletionOptions.DisableDefaultCmd = true
6061

62+
runCmd.PersistentFlags().BoolVarP(&execWait, "wait", "w", false, "wait for the task to finish")
63+
6164
rootCmd.AddCommand(runCmd)
6265
rootCmd.AddCommand(deregisterCmd)
6366
}

pkg/ecs/exec.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ecs
33
import (
44
"context"
55
"log"
6+
"time"
67

78
"github.com/aws/aws-sdk-go-v2/service/ecs"
89
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
@@ -29,6 +30,7 @@ func (s *Service) describeService(client *ecs.Client) (serviceDef, error) {
2930
}
3031

3132
def := resp.Services[0]
33+
log.Printf("Service '%s' loaded.", *def.ServiceName)
3234

3335
return serviceDef{
3436
Subnets: def.NetworkConfiguration.AwsvpcConfiguration.Subnets,
@@ -45,7 +47,7 @@ func (s *Service) describeTask(client *ecs.Client, taskArn *string) taskDef {
4547
}
4648
}
4749

48-
func (s *Service) Execute(cmd []string) {
50+
func (s *Service) Execute(cmd []string, wait bool) {
4951
cfg, err := s.initCfg()
5052
if err != nil {
5153
log.Fatalln(err)
@@ -84,4 +86,29 @@ func (s *Service) Execute(cmd []string) {
8486
}
8587

8688
log.Printf("Task %s executed.", *output.Tasks[0].TaskArn)
89+
if wait {
90+
for {
91+
success := s.wait(svc, *output.Tasks[0].TaskArn)
92+
if success {
93+
break
94+
}
95+
96+
time.Sleep(6 * time.Second)
97+
}
98+
99+
log.Printf("Task %s finished.", *output.Tasks[0].TaskArn)
100+
}
101+
}
102+
103+
func (s *Service) wait(client *ecs.Client, task string) bool {
104+
output, err := client.DescribeTasks(context.TODO(), &ecs.DescribeTasksInput{
105+
Cluster: &s.Cluster,
106+
Tasks: []string{task},
107+
})
108+
109+
if err != nil {
110+
log.Fatalln(err)
111+
}
112+
113+
return *output.Tasks[0].LastStatus == "STOPPED"
87114
}

0 commit comments

Comments
 (0)