Skip to content

Commit 52f813f

Browse files
committed
Add context to StopGracefully
Signed-off-by: Oleksandr Redko <[email protected]>
1 parent a120468 commit 52f813f

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

cmd/limactl/stop.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ func stopAction(cmd *cobra.Command, args []string) error {
3939
if err != nil {
4040
return err
4141
}
42+
ctx := cmd.Context()
4243
if force {
4344
instance.StopForcibly(inst)
4445
} else {
45-
err = instance.StopGracefully(inst, false)
46+
err = instance.StopGracefully(ctx, inst, false)
4647
}
4748
// TODO: should we also reconcile networks if graceful stop returned an error?
4849
if err == nil {
49-
err = networks.Reconcile(cmd.Context(), "")
50+
err = networks.Reconcile(ctx, "")
5051
}
5152
return err
5253
}

pkg/instance/restart.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
const launchHostAgentForeground = false
1515

1616
func Restart(ctx context.Context, inst *store.Instance) error {
17-
if err := StopGracefully(inst, true); err != nil {
17+
if err := StopGracefully(ctx, inst, true); err != nil {
1818
return err
1919
}
2020

pkg/instance/stop.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"github.com/sirupsen/logrus"
2020
)
2121

22-
func StopGracefully(inst *store.Instance, isRestart bool) error {
22+
func StopGracefully(ctx context.Context, inst *store.Instance, isRestart bool) error {
2323
if inst.Status != store.StatusRunning {
2424
if isRestart {
2525
logrus.Warn("The instance is not running, continuing with the restart")
@@ -35,17 +35,17 @@ func StopGracefully(inst *store.Instance, isRestart bool) error {
3535
}
3636

3737
logrus.Info("Waiting for the host agent and the driver processes to shut down")
38-
err := waitForHostAgentTermination(context.TODO(), inst, begin)
38+
err := waitForHostAgentTermination(ctx, inst, begin)
3939
if err != nil {
4040
return err
4141
}
4242

4343
logrus.Info("Waiting for the instance to shut down")
44-
return waitForInstanceShutdown(context.TODO(), inst)
44+
return waitForInstanceShutdown(ctx, inst)
4545
}
4646

4747
func waitForHostAgentTermination(ctx context.Context, inst *store.Instance, begin time.Time) error {
48-
ctx2, cancel := context.WithTimeout(ctx, 3*time.Minute+10*time.Second)
48+
ctx, cancel := context.WithTimeout(ctx, 3*time.Minute+10*time.Second)
4949
defer cancel()
5050

5151
var receivedExitingEvent bool
@@ -63,7 +63,7 @@ func waitForHostAgentTermination(ctx context.Context, inst *store.Instance, begi
6363
haStdoutPath := filepath.Join(inst.Dir, filenames.HostAgentStdoutLog)
6464
haStderrPath := filepath.Join(inst.Dir, filenames.HostAgentStderrLog)
6565

66-
if err := hostagentevents.Watch(ctx2, haStdoutPath, haStderrPath, begin, onEvent); err != nil {
66+
if err := hostagentevents.Watch(ctx, haStdoutPath, haStderrPath, begin, onEvent); err != nil {
6767
return err
6868
}
6969

@@ -75,7 +75,7 @@ func waitForHostAgentTermination(ctx context.Context, inst *store.Instance, begi
7575
}
7676

7777
func waitForInstanceShutdown(ctx context.Context, inst *store.Instance) error {
78-
ctx2, cancel := context.WithTimeout(ctx, 3*time.Minute)
78+
ctx, cancel := context.WithTimeout(ctx, 3*time.Minute)
7979
defer cancel()
8080

8181
ticker := time.NewTicker(500 * time.Millisecond)
@@ -93,7 +93,7 @@ func waitForInstanceShutdown(ctx context.Context, inst *store.Instance) error {
9393
logrus.Infof("The instance %s has shut down", updatedInst.Name)
9494
return nil
9595
}
96-
case <-ctx2.Done():
96+
case <-ctx.Done():
9797
return errors.New("timed out waiting for instance to shut down after 3 minutes")
9898
}
9999
}

0 commit comments

Comments
 (0)