@@ -1276,13 +1276,27 @@ protected boolean cleanup(VirtualMachineGuru guru, VirtualMachineProfile profile
12761276 s_logger .debug ("Cleaning up resources for the vm " + vm + " in " + state + " state" );
12771277 try {
12781278 if (state == State .Starting ) {
1279- Step step = work .getStep ();
1280- if (step == Step .Starting && !cleanUpEvenIfUnableToStop ) {
1281- s_logger .warn ("Unable to cleanup vm " + vm + "; work state is incorrect: " + step );
1282- return false ;
1283- }
1279+ if (work != null ) {
1280+ Step step = work .getStep ();
1281+ if (step == Step .Starting && !cleanUpEvenIfUnableToStop ) {
1282+ s_logger .warn ("Unable to cleanup vm " + vm + "; work state is incorrect: " + step );
1283+ return false ;
1284+ }
1285+
1286+ if (step == Step .Started || step == Step .Starting || step == Step .Release ) {
1287+ if (vm .getHostId () != null ) {
1288+ if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop , false )) {
1289+ s_logger .warn ("Failed to stop vm " + vm + " in " + State .Starting + " state as a part of cleanup process" );
1290+ return false ;
1291+ }
1292+ }
1293+ }
12841294
1285- if (step == Step .Started || step == Step .Starting || step == Step .Release ) {
1295+ if (step != Step .Release && step != Step .Prepare && step != Step .Started && step != Step .Starting ) {
1296+ s_logger .debug ("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step );
1297+ return true ;
1298+ }
1299+ } else {
12861300 if (vm .getHostId () != null ) {
12871301 if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop , false )) {
12881302 s_logger .warn ("Failed to stop vm " + vm + " in " + State .Starting + " state as a part of cleanup process" );
@@ -1291,10 +1305,6 @@ protected boolean cleanup(VirtualMachineGuru guru, VirtualMachineProfile profile
12911305 }
12921306 }
12931307
1294- if (step != Step .Release && step != Step .Prepare && step != Step .Started && step != Step .Starting ) {
1295- s_logger .debug ("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step );
1296- return true ;
1297- }
12981308 } else if (state == State .Stopping ) {
12991309 if (vm .getHostId () != null ) {
13001310 if (!sendStop (guru , profile , cleanUpEvenIfUnableToStop , false )) {
@@ -1446,20 +1456,10 @@ private void advanceStop(VMInstanceVO vm, boolean cleanUpEvenIfUnableToStop) thr
14461456 if (!cleanUpEvenIfUnableToStop ) {
14471457 throw new CloudRuntimeException ("We cannot stop " + vm + " when it is in state " + vm .getState ());
14481458 }
1449- boolean doCleanup = false ;
1459+ boolean doCleanup = true ;
14501460 if (s_logger .isDebugEnabled ()) {
14511461 s_logger .debug ("Unable to transition the state but we're moving on because it's forced stop" );
14521462 }
1453- if ((state == State .Starting ) || (state == State .Migrating ) || (state == State .Stopping )) {
1454- if (work != null ) {
1455- doCleanup = true ;
1456- } else {
1457- if (s_logger .isDebugEnabled ()) {
1458- s_logger .debug ("Unable to cleanup VM: " + vm + " ,since outstanding work item is not found" );
1459- }
1460- throw new CloudRuntimeException ("Work item not found, We cannot stop " + vm + " when it is in state " + vm .getState ());
1461- }
1462- }
14631463
14641464 if (doCleanup ) {
14651465 if (cleanup (vmGuru , new VirtualMachineProfileImpl (vm ), work , Event .StopRequested , cleanUpEvenIfUnableToStop )) {
0 commit comments