|
4 | 4 | "context"
|
5 | 5 | "fmt"
|
6 | 6 | "strings"
|
| 7 | + "time" |
7 | 8 |
|
8 | 9 | "github.com/sourcegraph/conc/pool"
|
9 | 10 | fly "github.com/superfly/fly-go"
|
@@ -60,16 +61,41 @@ func releaseLease(ctx context.Context, machine *fly.Machine) {
|
60 | 61 | io := iostreams.FromContext(ctx)
|
61 | 62 | flapsClient := flapsutil.ClientFromContext(ctx)
|
62 | 63 |
|
| 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 | + |
63 | 73 | if err := flapsClient.ReleaseLease(ctx, machine.ID, machine.LeaseNonce); err != nil {
|
64 | 74 | 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()) |
66 | 76 | }
|
67 | 77 | }
|
68 | 78 | }
|
69 | 79 |
|
70 | 80 | // AcquireLease works to acquire/attach a lease for the specified machine.
|
71 | 81 | // WARNING: Make sure you defer the lease release process.
|
72 | 82 | 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 | + |
73 | 99 | flapsClient := flapsutil.ClientFromContext(ctx)
|
74 | 100 |
|
75 | 101 | lease, err := flapsClient.AcquireLease(ctx, machine.ID, fly.IntPointer(120))
|
|
0 commit comments