Skip to content

Commit

Permalink
Use lease-on-create instead of acquire-post-create
Browse files Browse the repository at this point in the history
  • Loading branch information
dangra committed Apr 21, 2023
1 parent b27028a commit 8e7ed87
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
3 changes: 2 additions & 1 deletion api/machine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Machine struct {
Config *MachineConfig `json:"config,omitempty"`
Events []*MachineEvent `json:"events,omitempty"`
Checks []*MachineCheckStatus `json:"checks,omitempty"`
LeaseNonce string
LeaseNonce string `json:"nonce,omitempty"`
}

func (m *Machine) FullImageRef() string {
Expand Down Expand Up @@ -538,6 +538,7 @@ type LaunchMachineInput struct {
Region string `json:"region,omitempty"`
Config *MachineConfig `json:"config,omitempty"`
SkipLaunch bool `json:"skip_launch,omitempty"`
LeaseTTL int `json:"lease_ttl,omitempty"`
// Client side only
SkipHealthChecks bool
}
Expand Down
17 changes: 8 additions & 9 deletions internal/command/deploy/machines_deploymachinesapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ func (md *machineDeployment) updateExistingMachines(ctx context.Context, updateE
fmt.Fprintf(md.io.ErrOut, "Continuing after error: %s\n", err)
}

// Acquire a lease on the new machine to ensure external factors can't stop or update it
// while we wait for its state and/or health checks
launchInput.LeaseTTL = int(md.waitTimeout.Seconds())

newMachineRaw, err := md.flapsClient.Launch(ctx, *launchInput)
if err != nil {
if md.strategy != "immediate" {
Expand All @@ -218,10 +222,6 @@ func (md *machineDeployment) updateExistingMachines(ctx context.Context, updateE

lm = machine.NewLeasableMachine(md.flapsClient, md.io, newMachineRaw)
fmt.Fprintf(md.io.ErrOut, " %s Created machine %s\n", indexStr, md.colorize.Bold(lm.FormattedMachineId()))
// FIXME: Workaround while support for acquiring lease along machine creation is implemented on Flaps API
if err := lm.AcquireLease(ctx, 10*time.Minute); err != nil {
return err
}
defer lm.ReleaseLease(ctx)

} else {
Expand Down Expand Up @@ -277,6 +277,10 @@ func (md *machineDeployment) spawnMachineInGroup(ctx context.Context, groupName
return "", fmt.Errorf("error creating machine configuration: %w", err)
}

// Acquire a lease on the new machine to ensure external factors can't stop or update it
// while we wait for its state and/or health checks
launchInput.LeaseTTL = int(md.waitTimeout.Seconds())

newMachineRaw, err := md.flapsClient.Launch(ctx, *launchInput)
if err != nil {
relCmdWarning := ""
Expand All @@ -288,11 +292,6 @@ func (md *machineDeployment) spawnMachineInGroup(ctx context.Context, groupName

lm := machine.NewLeasableMachine(md.flapsClient, md.io, newMachineRaw)
fmt.Fprintf(md.io.ErrOut, " Machine %s was created\n", md.colorize.Bold(lm.FormattedMachineId()))

// FIXME: Workaround while support for acquiring lease along machine creation is implemented on Flaps API
if err := lm.AcquireLease(ctx, 10*time.Minute); err != nil {
return "", err
}
defer lm.ReleaseLease(ctx)

// Don't wait for Standby machines, they are created but not started
Expand Down

0 comments on commit 8e7ed87

Please sign in to comment.