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

Merged
merged 6 commits into from
Jan 6, 2025
Merged
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
5 changes: 5 additions & 0 deletions internal/command/launch/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ func New() (cmd *cobra.Command) {
Name: "no-create",
Description: "Do not create an app, only generate configuration files",
},
flag.String{
Name: "auto-stop",
Description: "Automatically suspend the app after a period of inactivity. Valid values are 'off', 'stop', and 'suspend",
Default: "stop",
},
)

cmd.AddCommand(NewPlan())
Expand Down
33 changes: 31 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,40 @@ 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 {
autostop := fly.MachineAutostopStop
autostopFlag := flag.GetString(ctx, "auto-stop")

if autostopFlag == "off" {
autostop = fly.MachineAutostopOff
} else if autostopFlag == "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 +213,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
Loading