Skip to content

Commit 4bb4c7d

Browse files
author
Kazuyoshi Kato
committed
fix: avoid client.GetOrganizationBySlug which queries too much fields
stage.Org() calls client.GetOrganizationBySlug() which queries too much fields, and times out occasionally. This change adds stateCompact() which queries less and more reliable.
1 parent 154fb98 commit 4bb4c7d

File tree

8 files changed

+74
-18
lines changed

8 files changed

+74
-18
lines changed

gql/generated.go

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gql/genqclient.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ fragment OrganizationData on Organization {
140140
paidPlan
141141
addOnSsoLink
142142
provisionsBetaExtensions
143+
name
144+
billable
143145
}
144146

145147
fragment AppData on App {

internal/command/launch/describe_plan.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strings"
77

88
"github.com/samber/lo"
9-
fly "github.com/superfly/fly-go"
109
"github.com/superfly/flyctl/internal/command/launch/plan"
1110
"github.com/superfly/flyctl/internal/command/mpg"
1211
"github.com/superfly/flyctl/internal/command/redis"
@@ -54,17 +53,17 @@ func describeSupabasePostgresPlan(p *plan.SupabasePostgresPlan, launchPlan *plan
5453
return fmt.Sprintf("(Supabase) %s in %s", p.GetDbName(launchPlan), p.GetRegion(launchPlan)), nil
5554
}
5655

57-
func describeRedisPlan(ctx context.Context, p plan.RedisPlan, org *fly.Organization) (string, error) {
56+
func describeRedisPlan(ctx context.Context, p plan.RedisPlan) (string, error) {
5857

5958
switch provider := p.Provider().(type) {
6059
case *plan.UpstashRedisPlan:
61-
return describeUpstashRedisPlan(ctx, provider, org)
60+
return describeUpstashRedisPlan(ctx, provider)
6261
}
6362
return descriptionNone, nil
6463
}
6564

66-
func describeUpstashRedisPlan(ctx context.Context, p *plan.UpstashRedisPlan, org *fly.Organization) (string, error) {
67-
plan, err := redis.DeterminePlan(ctx, org)
65+
func describeUpstashRedisPlan(ctx context.Context, p *plan.UpstashRedisPlan) (string, error) {
66+
plan, err := redis.DeterminePlan(ctx)
6867
if err != nil {
6968
return "<plan not found, this is an error>", fmt.Errorf("redis plan not found: %w", err)
7069
}

internal/command/launch/launch.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ func (state *launchState) Launch(ctx context.Context) error {
4040
return err
4141
}
4242

43-
org, err := state.Org(ctx)
43+
org, err := state.orgCompact(ctx)
4444
if err != nil {
4545
return err
4646
}
47-
if !planValidateHighAvailability(ctx, state.Plan, org, !state.warnedNoCcHa) {
47+
if !planValidateHighAvailability(ctx, state.Plan, org.Billable, !state.warnedNoCcHa) {
4848
state.Plan.HighAvailability = false
4949
state.warnedNoCcHa = true
5050
}
@@ -338,12 +338,13 @@ func (state *launchState) updateConfig(ctx context.Context) {
338338
// createApp creates the fly.io app for the plan
339339
func (state *launchState) createApp(ctx context.Context) (flapsutil.FlapsClient, *fly.App, error) {
340340
apiClient := flyutil.ClientFromContext(ctx)
341-
org, err := state.Org(ctx)
341+
342+
org, err := state.orgCompact(ctx)
342343
if err != nil {
343344
return nil, nil, err
344345
}
345346
app, err := apiClient.CreateApp(ctx, fly.CreateAppInput{
346-
OrganizationID: org.ID,
347+
OrganizationID: org.Id,
347348
Name: state.Plan.AppName,
348349
PreferredRegion: &state.Plan.RegionCode,
349350
Machines: true,

internal/command/launch/plan_builder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func buildManifest(ctx context.Context, parentConfig *appconfig.Config, recovera
220220
warnedNoCcHa: false,
221221
}
222222

223-
if planValidateHighAvailability(ctx, lp, org, true) {
223+
if planValidateHighAvailability(ctx, lp, org.Billable, true) {
224224
buildCache.warnedNoCcHa = true
225225
}
226226

@@ -799,8 +799,8 @@ func determineCompute(ctx context.Context, config *appconfig.Config, srcInfo *sc
799799
return []*appconfig.Compute{guestToCompute(guest)}, reason, nil
800800
}
801801

802-
func planValidateHighAvailability(ctx context.Context, p *plan.LaunchPlan, org *fly.Organization, print bool) bool {
803-
if !org.Billable && p.HighAvailability {
802+
func planValidateHighAvailability(ctx context.Context, p *plan.LaunchPlan, billable, print bool) bool {
803+
if !billable && p.HighAvailability {
804804
if print {
805805
fmt.Fprintln(iostreams.FromContext(ctx).ErrOut, "Warning: This organization has no payment method, turning off high availability")
806806
}

internal/command/launch/state.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ func cacheGrab[T any](cache map[string]interface{}, key string, cb func() (T, er
5656
return val, nil
5757
}
5858

59+
func (state *launchState) orgCompact(ctx context.Context) (*gql.GetOrganizationOrganization, error) {
60+
client := flyutil.ClientFromContext(ctx).GenqClient()
61+
res, err := gql.GetOrganization(ctx, client, state.Plan.OrgSlug)
62+
if err != nil {
63+
return nil, fmt.Errorf("failed to get org %q for state: %w", state.Plan.OrgSlug, err)
64+
}
65+
return &res.Organization, nil
66+
}
67+
5968
func (state *launchState) Org(ctx context.Context) (*fly.Organization, error) {
6069
apiClient := flyutil.ClientFromContext(ctx)
6170
return cacheGrab(state.cache, "org,"+state.Plan.OrgSlug, func() (*fly.Organization, error) {
@@ -103,7 +112,7 @@ func (state *launchState) PlanSummary(ctx context.Context) (string, error) {
103112
guestStr += fmt.Sprintf(", %d more", len(state.appConfig.Compute)-1)
104113
}
105114

106-
org, err := state.Org(ctx)
115+
org, err := state.orgCompact(ctx)
107116
if err != nil {
108117
return "", err
109118
}
@@ -118,7 +127,7 @@ func (state *launchState) PlanSummary(ctx context.Context) (string, error) {
118127
return "", err
119128
}
120129

121-
redisStr, err := describeRedisPlan(ctx, state.Plan.Redis, org)
130+
redisStr, err := describeRedisPlan(ctx, state.Plan.Redis)
122131
if err != nil {
123132
return "", err
124133
}
@@ -183,7 +192,7 @@ func (state *launchState) validateExtensions(ctx context.Context) error {
183192
io := iostreams.FromContext(ctx)
184193
noConfirm := !io.IsInteractive() || flag.GetBool(ctx, "now")
185194

186-
org, err := state.Org(ctx)
195+
org, err := state.orgCompact(ctx)
187196
if err != nil {
188197
return err
189198
}

internal/command/launch/webui.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (state *launchState) EditInWebUi(ctx context.Context) error {
9797
region = r
9898
}
9999

100-
org, err := state.Org(ctx)
100+
org, err := state.orgCompact(ctx)
101101
if err != nil {
102102
return fmt.Errorf("failed to get organization: %w", err)
103103
}

internal/command/redis/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func Create(ctx context.Context, org *fly.Organization, name string, region *fly
146146
}
147147
}
148148

149-
plan, err := DeterminePlan(ctx, org)
149+
plan, err := DeterminePlan(ctx)
150150
if err != nil {
151151
return nil, err
152152
}
@@ -216,7 +216,7 @@ func ProvisionDatabase(ctx context.Context, org *fly.Organization, config RedisC
216216
return &response.CreateAddOn.AddOn, nil
217217
}
218218

219-
func DeterminePlan(ctx context.Context, org *fly.Organization) (*gql.ListAddOnPlansAddOnPlansAddOnPlanConnectionNodesAddOnPlan, error) {
219+
func DeterminePlan(ctx context.Context) (*gql.ListAddOnPlansAddOnPlansAddOnPlanConnectionNodesAddOnPlan, error) {
220220
client := flyutil.ClientFromContext(ctx)
221221

222222
planId := redisPlanPayAsYouGo

0 commit comments

Comments
 (0)