func()

in tools/mig-scaler/client.go [86:129]


func (client *Client) findActiveJobs(ctx context.Context, mig MIGRef) ([]string, error) {
	var active []string
	req := &execpb.ListExecutionsRequest{
		Parent:   client.w.FullName(),
		PageSize: 100,
		View:     execpb.ExecutionView_BASIC,
	}

	oldest := client.w.Oldest()
	it := client.c.ListExecutions(ctx, req)
	for {
		e, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return active, fmt.Errorf("could not fetch page: %w", err)
		}
		if e.StartTime.AsTime().Before(oldest) {
			break
		}

		// This is N+1 queries, but we're expecting only a couple of active
		// executions, while there might a full page of executions in other
		// states. Currently assuming this will use less bandwidth than fetching
		// a full page of executions with their entire JSON request payload.
		if e.State == execpb.Execution_ACTIVE {
			e, err = client.getExecutionFull(ctx, e.Name)
			if err != nil {
				return active, fmt.Errorf("could not fetch execution \"%s\": %w", e.Name, err)
			}

			j, err := parseJob(e)
			if err != nil {
				log.Printf("warn: could not parse job details for \"%s\": %v", e.Name, err)
			} else if mig == j.MIG {
				// expects both mig and j.MIG to be normalized
				active = append(active, e.Name)
			}
		}
	}

	return active, nil
}