mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 18:47:57 -05:00
added contract to cancel restore plan and close the related connections (#522)
This commit is contained in:
@@ -87,4 +87,11 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery.Contracts
|
|||||||
RequestType<RestoreParams, RestorePlanResponse> Type =
|
RequestType<RestoreParams, RestorePlanResponse> Type =
|
||||||
RequestType<RestoreParams, RestorePlanResponse>.Create("disasterrecovery/restoreplan");
|
RequestType<RestoreParams, RestorePlanResponse>.Create("disasterrecovery/restoreplan");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class CancelRestorePlanRequest
|
||||||
|
{
|
||||||
|
public static readonly
|
||||||
|
RequestType<RestoreParams, bool> Type =
|
||||||
|
RequestType<RestoreParams, bool>.Create("disasterrecovery/cancelrestoreplan");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ using Microsoft.SqlTools.ServiceLayer.DisasterRecovery.Contracts;
|
|||||||
using Microsoft.SqlTools.ServiceLayer.DisasterRecovery.RestoreOperation;
|
using Microsoft.SqlTools.ServiceLayer.DisasterRecovery.RestoreOperation;
|
||||||
using Microsoft.SqlTools.ServiceLayer.FileBrowser;
|
using Microsoft.SqlTools.ServiceLayer.FileBrowser;
|
||||||
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
using Microsoft.SqlTools.ServiceLayer.TaskServices;
|
||||||
|
using Microsoft.SqlTools.Utility;
|
||||||
|
|
||||||
namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
||||||
{
|
{
|
||||||
@@ -114,6 +115,9 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
|||||||
// Create restore plan
|
// Create restore plan
|
||||||
serviceHost.SetRequestHandler(RestorePlanRequest.Type, HandleRestorePlanRequest);
|
serviceHost.SetRequestHandler(RestorePlanRequest.Type, HandleRestorePlanRequest);
|
||||||
|
|
||||||
|
// Cancel restore plan
|
||||||
|
serviceHost.SetRequestHandler(CancelRestorePlanRequest.Type, HandleCancelRestorePlanRequest);
|
||||||
|
|
||||||
// Create restore config
|
// Create restore config
|
||||||
serviceHost.SetRequestHandler(RestoreConfigInfoRequest.Type, HandleRestoreConfigInfoRequest);
|
serviceHost.SetRequestHandler(RestoreConfigInfoRequest.Type, HandleRestoreConfigInfoRequest);
|
||||||
|
|
||||||
@@ -164,6 +168,26 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handles a restore request
|
||||||
|
/// </summary>
|
||||||
|
internal async Task HandleCancelRestorePlanRequest(
|
||||||
|
RestoreParams restoreParams,
|
||||||
|
RequestContext<bool> requestContext)
|
||||||
|
{
|
||||||
|
bool result = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result = this.restoreDatabaseService.CancelRestorePlan(restoreParams);
|
||||||
|
await requestContext.SendResult(result);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.Write(LogLevel.Error, "Failed to cancel restore session. error: " + ex.Message);
|
||||||
|
await requestContext.SendResult(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handles a restore request
|
/// Handles a restore request
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -66,6 +66,26 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery.RestoreOperation
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Cancels existing restore plan
|
||||||
|
/// </summary>
|
||||||
|
public bool CancelRestorePlan(RestoreParams restoreParams)
|
||||||
|
{
|
||||||
|
RestoreDatabaseTaskDataObject restoreTaskObject = null;
|
||||||
|
string sessionId = restoreParams.SessionId;
|
||||||
|
if (!string.IsNullOrEmpty(sessionId) && sessions.TryGetValue(sessionId, out restoreTaskObject))
|
||||||
|
{
|
||||||
|
ServerConnection connection = restoreTaskObject?.Server?.ConnectionContext;
|
||||||
|
if (connection != null && connection.IsOpen)
|
||||||
|
{
|
||||||
|
connection.Disconnect();
|
||||||
|
}
|
||||||
|
sessions.TryRemove(sessionId, out restoreTaskObject);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a restore plan, The result includes the information about the backup set,
|
/// Creates a restore plan, The result includes the information about the backup set,
|
||||||
/// the files and the database to restore to
|
/// the files and the database to restore to
|
||||||
|
|||||||
@@ -439,6 +439,40 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DisasterRecovery
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CancelRestorePlanRequestShouldCancelSuccessfully()
|
||||||
|
{
|
||||||
|
await VerifyBackupFileCreated();
|
||||||
|
|
||||||
|
using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
|
||||||
|
{
|
||||||
|
TestConnectionResult connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);
|
||||||
|
|
||||||
|
string filePath = GetBackupFilePath(fullBackupFilePath);
|
||||||
|
|
||||||
|
RestoreParams restoreParams = new RestoreParams
|
||||||
|
{
|
||||||
|
BackupFilePaths = filePath,
|
||||||
|
OwnerUri = queryTempFile.FilePath
|
||||||
|
};
|
||||||
|
|
||||||
|
await RunAndVerify<RestorePlanResponse>(
|
||||||
|
test: (requestContext) => service.HandleRestorePlanRequest(restoreParams, requestContext),
|
||||||
|
verify: ((result) =>
|
||||||
|
{
|
||||||
|
restoreParams.SessionId = result.SessionId;
|
||||||
|
Assert.True(result.DbFiles.Any());
|
||||||
|
}));
|
||||||
|
|
||||||
|
await RunAndVerify<bool>(
|
||||||
|
test: (requestContext) => service.HandleCancelRestorePlanRequest(restoreParams, requestContext),
|
||||||
|
verify: ((result) =>
|
||||||
|
{
|
||||||
|
Assert.True(result);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task RestoreConfigInfoRequestShouldReturnResponse()
|
public async Task RestoreConfigInfoRequestShouldReturnResponse()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user