@@ -345,7 +345,26 @@ private boolean checkTaskStatus(final HttpResponse response) throws IOException
345345 String type = pair .second ();
346346 String path = url .replace (apiURI .toString (), "" );
347347 if (type .equals ("RestoreSession" )) {
348- return checkIfRestoreSessionFinished (type , path );
348+ for (int j = 0 ; j < restoreTimeout ; j ++) {
349+ HttpResponse relatedResponse = get (path );
350+ RestoreSession session = parseRestoreSessionResponse (relatedResponse );
351+ if (session .getResult ().equals ("Success" )) {
352+ return true ;
353+ }
354+ if (session .getResult ().equalsIgnoreCase ("Failed" )) {
355+ String sessionUid = session .getUid ();
356+ LOG .error (String .format ("Failed to restore backup [%s] of VM [%s] due to [%s]." ,
357+ sessionUid , session .getVmDisplayName (),
358+ getRestoreVmErrorDescription (StringUtils .substringAfterLast (sessionUid , ":" ))));
359+ throw new CloudRuntimeException (String .format ("Restore job [%s] failed." , sessionUid ));
360+ }
361+ LOG .debug (String .format ("Waiting %s seconds, out of a total of %s seconds, for the backup process to finish." , j , restoreTimeout ));
362+ try {
363+ Thread .sleep (1000 );
364+ } catch (InterruptedException ignored ) {
365+ }
366+ }
367+ throw new CloudRuntimeException ("Related job type: " + type + " was not successful" );
349368 }
350369 }
351370 return true ;
@@ -930,6 +949,29 @@ public Pair<Boolean, String> restoreVMToDifferentLocation(String restorePointId,
930949 return new Pair <>(result .first (), restoreLocation );
931950 }
932951
952+ /**
953+ * Tries to retrieve the error's descripton of the Veeam restore task that errored.
954+ * @param uid Session uid in Veeam of restore process;
955+ * @return the description found in Veeam about the cause of error in restore process.
956+ */
957+ protected String getRestoreVmErrorDescription (String uid ) {
958+ LOG .debug (String .format ("Trying to find cause of error in restore process [%s]." , uid ));
959+ List <String > cmds = Arrays .asList (
960+ String .format ("$restoreUid = '%s'" , uid ),
961+ "$restore = Get-VBRRestoreSession -Id $restoreUid" ,
962+ "if ($restore) {" ,
963+ "Write-Output $restore.Description" ,
964+ "} else {" ,
965+ "Write-Output 'Cannot find restore session with provided uid $restoreUid'" ,
966+ "}"
967+ );
968+ Pair <Boolean , String > result = executePowerShellCommands (cmds );
969+ if (result != null && result .first ()) {
970+ return result .second ();
971+ }
972+ return String .format ("Failed to get description of failed restore session [%s]. Please contact an administrator." , uid );
973+ }
974+
933975 private boolean isLegacyServer () {
934976 return this .veeamServerVersion != null && (this .veeamServerVersion > 0 && this .veeamServerVersion < 11 );
935977 }
0 commit comments