-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.go
More file actions
126 lines (107 loc) · 3.07 KB
/
run.go
File metadata and controls
126 lines (107 loc) · 3.07 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
package batcha
import (
"context"
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/batch"
batchTypes "github.com/aws/aws-sdk-go-v2/service/batch/types"
)
// RunOption holds options for the run command.
type RunOption struct {
JobQueue string
JobName string
Parameters map[string]string
Wait bool
}
// Run submits a job using the latest active job definition.
func (app *App) Run(ctx context.Context, opt RunOption) error {
// Resolve job queue: CLI flag > config > error
if opt.JobQueue == "" {
opt.JobQueue = app.config.JobQueue
}
if opt.JobQueue == "" {
return fmt.Errorf("job queue is required: set job_queue in config or use --job-queue flag")
}
rendered, err := app.render(ctx)
if err != nil {
return err
}
converted := walkMap(rendered, toPascalCase)
name, _ := converted.(map[string]any)["JobDefinitionName"].(string)
if name == "" {
return fmt.Errorf("jobDefinitionName is required in job definition")
}
client, err := app.newBatchClient(ctx)
if err != nil {
return fmt.Errorf("failed to load AWS config: %w", err)
}
// Fetch the latest active revision ARN
out, err := client.DescribeJobDefinitions(ctx, &batch.DescribeJobDefinitionsInput{
JobDefinitionName: aws.String(name),
Status: aws.String("ACTIVE"),
})
if err != nil {
return fmt.Errorf("failed to describe job definitions: %w", err)
}
if len(out.JobDefinitions) == 0 {
return fmt.Errorf("no active job definition found for %q", name)
}
latest := pickLatestRevision(out.JobDefinitions)
jobName := opt.JobName
if jobName == "" {
jobName = name
}
input := &batch.SubmitJobInput{
JobDefinition: latest.JobDefinitionArn,
JobQueue: aws.String(opt.JobQueue),
JobName: aws.String(jobName),
}
if len(opt.Parameters) > 0 {
input.Parameters = opt.Parameters
}
result, err := client.SubmitJob(ctx, input)
if err != nil {
return fmt.Errorf("failed to submit job: %w", err)
}
fmt.Printf("Submitted job: %s (ID: %s)\n", aws.ToString(result.JobName), aws.ToString(result.JobId))
if !opt.Wait {
return nil
}
return app.waitForJob(ctx, client, aws.ToString(result.JobId))
}
func (app *App) waitForJob(ctx context.Context, client *batch.Client, jobID string) error {
fmt.Printf("Waiting for job %s...\n", jobID)
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
var lastStatus batchTypes.JobStatus
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
out, err := client.DescribeJobs(ctx, &batch.DescribeJobsInput{
Jobs: []string{jobID},
})
if err != nil {
return fmt.Errorf("failed to describe job: %w", err)
}
if len(out.Jobs) == 0 {
return fmt.Errorf("job %s not found", jobID)
}
job := out.Jobs[0]
if job.Status != lastStatus {
fmt.Printf(" %s\n", job.Status)
lastStatus = job.Status
}
switch job.Status {
case batchTypes.JobStatusSucceeded:
fmt.Println("Job succeeded.")
return nil
case batchTypes.JobStatusFailed:
reason := aws.ToString(job.StatusReason)
return fmt.Errorf("job failed: %s", reason)
}
}
}
}