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

Default newly launched applications to auto suspend #4067

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
27 changes: 25 additions & 2 deletions internal/command/launch/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.MachineGuest != nil && compute.MachineGuest.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"},
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/preflight/apps_v2_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

Expand Down
2 changes: 1 addition & 1 deletion test/preflight/fly_launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
Loading