@@ -409,6 +409,7 @@ public <T extends VMInstanceVO> boolean advanceExpunge(T vm, User caller, Accoun
409409 @ Override
410410 public boolean start () {
411411 _executor .scheduleAtFixedRate (new CleanupTask (), _cleanupInterval , _cleanupInterval , TimeUnit .SECONDS );
412+ cancelWorkItems (_nodeId );
412413 return true ;
413414 }
414415
@@ -446,6 +447,7 @@ public boolean configure(String name, Map<String, Object> xmlParams) throws Conf
446447 _nodeId = _clusterMgr .getId ();
447448
448449 _agentMgr .registerForHostEvents (this , true , true , true );
450+
449451 return true ;
450452 }
451453
@@ -1050,6 +1052,41 @@ public <T extends VMInstanceVO> T migrate(T vm, long srcHostId, DeployDestinatio
10501052 }
10511053 }
10521054
1055+ protected void cancelWorkItems (long nodeId ) {
1056+ GlobalLock scanLock = GlobalLock .getInternLock (this .getClass ().getName ());
1057+
1058+ try {
1059+ if (scanLock .lock (3 )) {
1060+ try {
1061+ List <ItWorkVO > works = _workDao .listWorkInProgressFor (nodeId );
1062+ for (ItWorkVO work : works ) {
1063+ s_logger .info ("Handling unfinished work item: " + work );
1064+ try {
1065+ VMInstanceVO vm = _vmDao .findById (work .getInstanceId ());
1066+ if (vm != null ) {
1067+ if (work .getType () == State .Starting ) {
1068+ _haMgr .scheduleRestart (vm , true );
1069+ } else if (work .getType () == State .Stopping ) {
1070+ _haMgr .scheduleStop (vm , vm .getHostId (), WorkType .CheckStop );
1071+ } else if (work .getType () == State .Migrating ) {
1072+ _haMgr .scheduleMigration (vm );
1073+ }
1074+ }
1075+ work .setStep (Step .Done );
1076+ _workDao .update (work .getId (), work );
1077+ } catch (Exception e ) {
1078+ s_logger .error ("Error while handling " + work , e );
1079+ }
1080+ }
1081+ } finally {
1082+ scanLock .unlock ();
1083+ }
1084+ }
1085+ } finally {
1086+ scanLock .releaseRef ();
1087+ }
1088+ }
1089+
10531090 @ Override
10541091 public boolean migrateAway (VirtualMachine .Type vmType , long vmId , long srcHostId ) throws InsufficientServerCapacityException {
10551092 VirtualMachineGuru <? extends VMInstanceVO > vmGuru = _vmGurus .get (vmType );
0 commit comments