@@ -19,8 +19,12 @@ import (
19
19
"github.com/sirupsen/logrus"
20
20
)
21
21
22
- func StopGracefully (inst * store.Instance ) error {
22
+ func StopGracefully (ctx context. Context , inst * store.Instance ) error {
23
23
if inst .Status != store .StatusRunning {
24
+ if isRestart , ok := ctx .Value ("restart" ).(bool ); ok && isRestart {
25
+ logrus .Warn ("The instance is not running, continuing with the restart" )
26
+ return nil
27
+ }
24
28
return fmt .Errorf ("expected status %q, got %q (maybe use `limactl stop -f`?)" , store .StatusRunning , inst .Status )
25
29
}
26
30
@@ -31,7 +35,13 @@ func StopGracefully(inst *store.Instance) error {
31
35
}
32
36
33
37
logrus .Info ("Waiting for the host agent and the driver processes to shut down" )
34
- return waitForHostAgentTermination (context .TODO (), inst , begin )
38
+ err := waitForHostAgentTermination (ctx , inst , begin )
39
+ if err != nil {
40
+ return err
41
+ }
42
+
43
+ logrus .Info ("Waiting for the instance to shut down" )
44
+ return waitForInstanceShutdown (ctx , inst )
35
45
}
36
46
37
47
func waitForHostAgentTermination (ctx context.Context , inst * store.Instance , begin time.Time ) error {
@@ -64,6 +74,31 @@ func waitForHostAgentTermination(ctx context.Context, inst *store.Instance, begi
64
74
return nil
65
75
}
66
76
77
+ func waitForInstanceShutdown (ctx context.Context , inst * store.Instance ) error {
78
+ ctx2 , cancel := context .WithTimeout (ctx , 3 * time .Minute )
79
+ defer cancel ()
80
+
81
+ ticker := time .NewTicker (500 * time .Millisecond )
82
+ defer ticker .Stop ()
83
+
84
+ for {
85
+ select {
86
+ case <- ticker .C :
87
+ updatedInst , err := store .Inspect (inst .Name )
88
+ if err != nil {
89
+ return errors .New ("failed to inspect instance status: " + err .Error ())
90
+ }
91
+
92
+ if updatedInst .Status == store .StatusStopped {
93
+ logrus .Infof ("The instance %s has shut down" , updatedInst .Name )
94
+ return nil
95
+ }
96
+ case <- ctx2 .Done ():
97
+ return errors .New ("timed out waiting for instance to shut down after 3 minutes" )
98
+ }
99
+ }
100
+ }
101
+
67
102
func StopForcibly (inst * store.Instance ) {
68
103
if inst .DriverPID > 0 {
69
104
logrus .Infof ("Sending SIGKILL to the %s driver process %d" , inst .VMType , inst .DriverPID )
0 commit comments