in tools/mig-scaler/list.go [75:157]
func (cmd *List) Execute(ctx context.Context) error {
client, err := NewClient(ctx, cmd.Workflow)
if err != nil {
return err
}
defer client.Close()
req := &execpb.ListExecutionsRequest{
Parent: cmd.Workflow.FullName(),
PageSize: 100,
View: execpb.ExecutionView_FULL,
}
var jobs []*Job
recent := make(map[MIGRef]*Job)
oldest := cmd.Workflow.Oldest()
it := client.c.ListExecutions(ctx, req)
for {
e, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatalf("error: could not fetch page: %v", err)
}
if e.StartTime.AsTime().Before(oldest) {
break
}
j, err := parseJob(e)
if err != nil {
log.Printf("warn: could not parse job details for \"%s\": %v", e.Name, err)
continue
}
if j.State == execpb.Execution_ACTIVE {
// include all active jobs, put these at the top of the list
jobs = append(jobs, j)
} else if !cmd.ActiveOnly {
// only include non-active jobs if ActiveOnly is false
if _, found := recent[j.MIG]; !found {
// only include the most recent non-active job for each MIG
recent[j.MIG] = j
}
}
}
// remove any MIGs from the recent list that have an active job
for _, j := range jobs {
delete(recent, j.MIG)
}
// add any remaining non-active jobs to the job list
for _, j := range recent {
jobs = append(jobs, j)
}
// sort the jobs, active first, then by when the job was submitted
sort.Slice(jobs, func(i, j int) bool {
x := jobs[i]
y := jobs[j]
// active first
if active(x.State) && !active(y.State) {
return true
}
if !active(x.State) && active(y.State) {
return false
}
// most recent first
return x.StartTime.After(y.StartTime)
})
if len(jobs) == 0 {
fmt.Println("No recent jobs found.")
} else {
cmd.Format(jobs, cmd.Detailed)
}
return nil
}