Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include go routines #304

Merged
merged 5 commits into from
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions internal/cluster/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,50 @@ import (

"github.com/buildkite/cli/v3/internal/graphql"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
"golang.org/x/sync/errgroup"
)

func QueryCluster(ctx context.Context, OrganizationSlug string, ClusterID string, f *factory.Factory) (*Cluster, error) {
q, err := graphql.GetClusterQueues(ctx, f.GraphQLClient, OrganizationSlug, ClusterID)
if err != nil {
fmt.Println("Unable to read Cluster Queues: ", err.Error())
return nil, err
return nil, fmt.Errorf("unable to read Cluster Queues: %s", err.Error())
}

ClusterDescription := q.Organization.Cluster.Description

cluster := Cluster{
OrganizationSlug: OrganizationSlug,
ClusterID: ClusterID,
Name: q.Organization.Cluster.Name,
Description: string(*ClusterDescription),
Queues: []Queue{},
Queues: make([]Queue, len(q.Organization.Cluster.Queues.Edges)),
}

for _, edge := range q.Organization.Cluster.Queues.Edges {
agent, err := graphql.GetClusterQueueAgent(ctx, f.GraphQLClient, OrganizationSlug, []string{edge.Node.Id})
if err != nil {
return nil, fmt.Errorf("unable to read Cluster Queue Agents: %s", err.Error())
}
eg, ctx := errgroup.WithContext(ctx)

for i, edge := range q.Organization.Cluster.Queues.Edges {

i, edge := i, edge
eg.Go(func() error {

agent, err := graphql.GetClusterQueueAgent(ctx, f.GraphQLClient, OrganizationSlug, []string{edge.Node.Id})
if err != nil {
return fmt.Errorf("unable to read Cluster Queue Agents %s: %s", edge.Node.Id, err.Error())
}

queue := Queue{
Id: edge.Node.Id,
Name: edge.Node.Key,
ActiveAgents: len(agent.Organization.Agents.Edges),
}
cluster.Queues = append(cluster.Queues, queue)
cluster.Queues[i] = Queue{
Id: edge.Node.Id,
Name: edge.Node.Key,
ActiveAgents: len(agent.Organization.Agents.Edges),
}

return nil
})

}

if err := eg.Wait(); err != nil {
return nil, err
}

return &cluster, nil
}