From 387423e168fe16f69f6a24a9c68175f93543be40 Mon Sep 17 00:00:00 2001 From: Sam Ruby Date: Sun, 17 Nov 2024 13:23:52 -0500 Subject: [PATCH 1/3] Default newly launched applications to auto suspend Continue to default applications to auto stop if: * a GPU is present * more than 2G of RAM is requested --- internal/command/launch/launch.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/internal/command/launch/launch.go b/internal/command/launch/launch.go index c68a3c8786..addf1315a0 100644 --- a/internal/command/launch/launch.go +++ b/internal/command/launch/launch.go @@ -6,8 +6,10 @@ import ( "path/filepath" "strings" + "github.com/docker/go-units" fly "github.com/superfly/fly-go" "github.com/superfly/fly-go/flaps" + "github.com/superfly/flyctl/helpers" "github.com/superfly/flyctl/internal/appconfig" "github.com/superfly/flyctl/internal/command/launch/plan" "github.com/superfly/flyctl/internal/flag" @@ -169,12 +171,34 @@ func (state *launchState) updateConfig(ctx context.Context) { if state.env != nil { state.appConfig.SetEnvVariables(state.env) } + + state.appConfig.Compute = state.Plan.Compute + if state.Plan.HttpServicePort != 0 { + // default to autostop: suspend + autostop := fly.MachineAutostopSuspend + + // if any compute has a GPU or more than 2GB of memory, set autostop to stop + for _, compute := range state.appConfig.Compute { + if compute.GPUKind != "" { + autostop = fly.MachineAutostopStop + break + } + + if compute.Memory != "" { + mb, err := helpers.ParseSize(compute.Memory, units.RAMInBytes, units.MiB) + if err != nil || mb >= 2048 { + autostop = fly.MachineAutostopStop + break + } + } + } + if state.appConfig.HTTPService == nil { state.appConfig.HTTPService = &appconfig.HTTPService{ ForceHTTPS: true, AutoStartMachines: fly.Pointer(true), - AutoStopMachines: fly.Pointer(fly.MachineAutostopStop), + AutoStopMachines: fly.Pointer(autostop), MinMachinesRunning: fly.Pointer(0), Processes: []string{"app"}, } @@ -183,7 +207,6 @@ func (state *launchState) updateConfig(ctx context.Context) { } else { state.appConfig.HTTPService = nil } - state.appConfig.Compute = state.Plan.Compute } // createApp creates the fly.io app for the plan From 51ed6480e1b15f6cde1d3ac6e78184c9d7f7a3f4 Mon Sep 17 00:00:00 2001 From: Sam Ruby Date: Sun, 17 Nov 2024 15:22:18 -0500 Subject: [PATCH 2/3] check for compute.MachineGuest != nil --- internal/command/launch/launch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/command/launch/launch.go b/internal/command/launch/launch.go index addf1315a0..e47dfcdf1f 100644 --- a/internal/command/launch/launch.go +++ b/internal/command/launch/launch.go @@ -180,7 +180,7 @@ func (state *launchState) updateConfig(ctx context.Context) { // if any compute has a GPU or more than 2GB of memory, set autostop to stop for _, compute := range state.appConfig.Compute { - if compute.GPUKind != "" { + if compute.MachineGuest != nil && compute.MachineGuest.GPUKind != "" { autostop = fly.MachineAutostopStop break } From f9091ffe011263d165a2b0961b29acf2d7c588d7 Mon Sep 17 00:00:00 2001 From: Sam Ruby Date: Sun, 17 Nov 2024 18:33:16 -0500 Subject: [PATCH 3/3] adjust tests --- test/preflight/apps_v2_integration_test.go | 2 +- test/preflight/fly_launch_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/preflight/apps_v2_integration_test.go b/test/preflight/apps_v2_integration_test.go index 700e4083de..66fb8d2da1 100644 --- a/test/preflight/apps_v2_integration_test.go +++ b/test/preflight/apps_v2_integration_test.go @@ -51,7 +51,7 @@ func TestAppsV2Example(t *testing.T) { require.NotNil(t, firstMachine.Config.Services[0].Autostop) assert.Equal( - t, fly.MachineAutostopStop, *firstMachine.Config.Services[0].Autostop, + t, fly.MachineAutostopSuspend, *firstMachine.Config.Services[0].Autostop, "autostop must be enabled", ) diff --git a/test/preflight/fly_launch_test.go b/test/preflight/fly_launch_test.go index 1782004f78..6a11ad42a4 100644 --- a/test/preflight/fly_launch_test.go +++ b/test/preflight/fly_launch_test.go @@ -50,7 +50,7 @@ func TestFlyLaunchV2(t *testing.T) { "http_service": map[string]any{ "force_https": true, "internal_port": int64(8080), - "auto_stop_machines": "stop", + "auto_stop_machines": "suspend", "auto_start_machines": true, "min_machines_running": int64(0), "processes": []any{"app"},