Skip to content

Commit 47ae293

Browse files
authored
Machine lease UX improvements (#3815)
* bump fly-go for lease context fix * machine lease improvements - print status update if we're still waiting on lease after 2s - release leases even if command has been aborted * improve deploy log messages around lease acquisition * skip acquiring nonce if we already have it * fix panic in postgres import
1 parent 47de058 commit 47ae293

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ require (
7272
github.com/spf13/pflag v1.0.5
7373
github.com/spf13/viper v1.19.0
7474
github.com/stretchr/testify v1.9.0
75-
github.com/superfly/fly-go v0.1.21
75+
github.com/superfly/fly-go v0.1.22-0.20240802153530-1487eea07345
7676
github.com/superfly/graphql v0.2.4
7777
github.com/superfly/lfsc-go v0.1.1
7878
github.com/superfly/macaroon v0.2.14-0.20240702184853-b8ac52a1fc77
@@ -272,5 +272,5 @@ require (
272272
replace (
273273
github.com/loadsmart/calver-go => github.com/ndarilek/calver-go v0.0.0-20230710153822-893bbd83a936
274274
github.com/nats-io/nats.go => github.com/btoews/nats.go v0.0.0-20240401180931-476bea7f4158
275-
github.com/superfly/fly-go => github.com/superfly/fly-go v0.1.21
275+
github.com/superfly/fly-go => github.com/superfly/fly-go v0.1.22-0.20240802153530-1487eea07345
276276
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
629629
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
630630
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
631631
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
632-
github.com/superfly/fly-go v0.1.21 h1:+UEmy+4jZ0f/lUWxF1u/y54qbodOCqcrvuL92ANMePo=
633-
github.com/superfly/fly-go v0.1.21/go.mod h1:JQke/BwoZqrWurqYkypSlcSo7bIUgCI3eVnqMC6AUj0=
632+
github.com/superfly/fly-go v0.1.22-0.20240802153530-1487eea07345 h1:4/khSPMwvoXgTyj8cKTu54dSFuIPkK0sTrMysTYa7+4=
633+
github.com/superfly/fly-go v0.1.22-0.20240802153530-1487eea07345/go.mod h1:JQke/BwoZqrWurqYkypSlcSo7bIUgCI3eVnqMC6AUj0=
634634
github.com/superfly/graphql v0.2.4 h1:Av8hSk4x8WvKJ6MTnEwrLknSVSGPc7DWpgT3z/kt3PU=
635635
github.com/superfly/graphql v0.2.4/go.mod h1:CVfDl31srm8HnJ9udwLu6hFNUW/P6GUM2dKcG1YQ8jc=
636636
github.com/superfly/lfsc-go v0.1.1 h1:dGjLgt81D09cG+aR9lJZIdmonjZSR5zYCi7s54+ZU2Q=

internal/command/deploy/plan.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,14 @@ func (md *machineDeployment) acquireLeases(ctx context.Context, machineTuples []
321321
} else {
322322
return nil
323323
}
324-
sl.LogStatus(statuslogger.StatusRunning, fmt.Sprintf("Acquiring lease for %s", machine.ID))
325324

326-
if machine.LeaseNonce == "" {
327-
sl.LogStatus(statuslogger.StatusRunning, fmt.Sprintf("Waiting for job %s", machine.ID))
325+
if machine.LeaseNonce != "" {
326+
sl.LogStatus(statuslogger.StatusRunning, fmt.Sprintf("Already have lease for %s", machine.ID))
327+
return nil
328328
}
329329

330+
sl.LogStatus(statuslogger.StatusRunning, fmt.Sprintf("Acquiring lease for %s", machine.ID))
331+
330332
lease, err := md.acquireMachineLease(ctx, machine.ID)
331333
if err != nil {
332334
sl.LogStatus(statuslogger.StatusFailure, fmt.Sprintf("Failed to acquire lease for %s: %v", machine.ID, err))
@@ -336,7 +338,7 @@ func (md *machineDeployment) acquireLeases(ctx context.Context, machineTuples []
336338
machine.LeaseNonce = lease.Data.Nonce
337339
lm := mach.NewLeasableMachine(md.flapsClient, md.io, machine, false)
338340
lm.StartBackgroundLeaseRefresh(ctx, md.leaseTimeout, md.leaseDelayBetween)
339-
sl.LogStatus(statuslogger.StatusRunning, fmt.Sprintf("Waiting for job %s", machine.ID))
341+
sl.LogStatus(statuslogger.StatusRunning, fmt.Sprintf("Acquired lease for %s", machine.ID))
340342
return nil
341343
})
342344
}

internal/command/postgres/import.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ func runImport(ctx context.Context) error {
115115
return fmt.Errorf("no machines are available on this app %s", appName)
116116
}
117117
leader, _ := machinesNodeRoles(ctx, machines)
118+
if leader == nil {
119+
return fmt.Errorf("no active leader found")
120+
}
118121
machineID := leader.ID
119122

120123
// Resolve region

internal/machine/lease.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"strings"
7+
"time"
78

89
"github.com/sourcegraph/conc/pool"
910
fly "github.com/superfly/fly-go"
@@ -60,16 +61,41 @@ func releaseLease(ctx context.Context, machine *fly.Machine) {
6061
io := iostreams.FromContext(ctx)
6162
flapsClient := flapsutil.ClientFromContext(ctx)
6263

64+
// remove the cancel from ctx so we can still releases leases if the command was aborted
65+
if ctx.Err() != nil {
66+
var cancel context.CancelFunc
67+
ctx, cancel = context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
68+
defer cancel()
69+
70+
fmt.Fprintf(io.Out, "Releasing lease for machine %s...\n", machine.ID)
71+
}
72+
6373
if err := flapsClient.ReleaseLease(ctx, machine.ID, machine.LeaseNonce); err != nil {
6474
if !strings.Contains(err.Error(), "lease not found") {
65-
fmt.Fprintf(io.Out, "failed to release lease for machine %s: %s", machine.ID, err.Error())
75+
fmt.Fprintf(io.Out, "failed to release lease for machine %s: %s\n", machine.ID, err.Error())
6676
}
6777
}
6878
}
6979

7080
// AcquireLease works to acquire/attach a lease for the specified machine.
7181
// WARNING: Make sure you defer the lease release process.
7282
func AcquireLease(ctx context.Context, machine *fly.Machine) (*fly.Machine, releaseLeaseFunc, error) {
83+
// if we haven't gotten the lease after 2s, we print a message so users
84+
// aren't left wondering.
85+
abortStatusUpdate := make(chan struct{})
86+
defer close(abortStatusUpdate)
87+
go func() {
88+
timer := time.NewTimer(2 * time.Second)
89+
defer timer.Stop()
90+
91+
select {
92+
case <-timer.C:
93+
io := iostreams.FromContext(ctx)
94+
fmt.Fprintf(io.Out, "Waiting on lease for machine %s...\n", machine.ID)
95+
case <-abortStatusUpdate:
96+
}
97+
}()
98+
7399
flapsClient := flapsutil.ClientFromContext(ctx)
74100

75101
lease, err := flapsClient.AcquireLease(ctx, machine.ID, fly.IntPointer(120))

0 commit comments

Comments
 (0)