Schema compare cancel operation (#826)

* First cut of schema compare cancel (private nuget)

* Update Dacfx nuget to a published version
This commit is contained in:
udeeshagautam
2019-06-14 18:19:36 -07:00
committed by GitHub
parent 432e054d55
commit 3e1f186891
13 changed files with 168 additions and 13 deletions

View File

@@ -0,0 +1,30 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
{
/// <summary>
/// Parameters for a schema compare cancel request
/// </summary>
public class SchemaCompareCancelParams
{
/// <summary>
/// Operation id of the schema compare operation
/// </summary>
public string OperationId { get; set; }
}
/// <summary>
/// Defines the Schema Compare cancel comparison request type
/// </summary>
class SchemaCompareCancellationRequest
{
public static readonly RequestType<SchemaCompareCancelParams, ResultStatus> Type =
RequestType<SchemaCompareCancelParams, ResultStatus>.Create("schemaCompare/cancel");
}
}

View File

@@ -57,7 +57,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
try
{
this.ScriptGenerationResult = this.ComparisonResult.GenerateScript(this.Parameters.TargetDatabaseName);
this.ScriptGenerationResult = this.ComparisonResult.GenerateScript(this.Parameters.TargetDatabaseName, this.CancellationToken);
// tests don't create a SqlTask, so only add the script when the SqlTask isn't null
if (this.SqlTask != null)
@@ -81,6 +81,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
// The schema compare public api doesn't currently take a cancellation token so the operation can't be cancelled
public void Cancel()
{
this.cancellation.Cancel();
}
/// <summary>

View File

@@ -49,7 +49,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
}
protected CancellationToken CancellationToken { get { return this.cancellation.Token; } }
/// <summary>
/// The error occurred during operation
/// </summary>
@@ -58,6 +58,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
// The schema compare public api doesn't currently take a cancellation token so the operation can't be cancelled
public void Cancel()
{
this.cancellation.Cancel();
}
/// <summary>
@@ -91,19 +92,31 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
comparison.Options = SchemaCompareUtils.CreateSchemaCompareOptions(this.Parameters.DeploymentOptions);
}
this.ComparisonResult = comparison.Compare();
// for testing
schemaCompareStarted?.Invoke(this, new EventArgs());
this.ComparisonResult = comparison.Compare(this.CancellationToken);
// try one more time if it didn't work the first time
if (!this.ComparisonResult.IsValid)
{
this.ComparisonResult = comparison.Compare();
this.ComparisonResult = comparison.Compare(this.CancellationToken);
}
// Since DacFx does not throw on schema comparison cancellation, throwing here explicitly to ensure consistency of behavior
if (!this.ComparisonResult.IsValid && this.CancellationToken.IsCancellationRequested)
{
throw new OperationCanceledException(this.CancellationToken);
}
this.Differences = new List<DiffEntry>();
foreach (SchemaDifference difference in this.ComparisonResult.Differences)
if (this.ComparisonResult.Differences != null)
{
DiffEntry diffEntry = SchemaCompareUtils.CreateDiffEntry(difference, null);
this.Differences.Add(diffEntry);
foreach (SchemaDifference difference in this.ComparisonResult.Differences)
{
DiffEntry diffEntry = SchemaCompareUtils.CreateDiffEntry(difference, null);
this.Differences.Add(diffEntry);
}
}
}
catch (Exception e)
@@ -113,5 +126,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
throw;
}
}
internal event EventHandler<EventArgs> schemaCompareStarted;
}
}

View File

@@ -54,7 +54,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
try
{
this.PublishResult = this.ComparisonResult.PublishChangesToTarget();
this.PublishResult = this.ComparisonResult.PublishChangesToTarget(this.CancellationToken);
}
catch (Exception e)
{
@@ -67,6 +67,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
// The schema compare public api doesn't currently take a cancellation token so the operation can't be cancelled
public void Cancel()
{
this.cancellation.Cancel();
}
}
}

View File

@@ -14,6 +14,7 @@ using Microsoft.SqlServer.Dac.Compare;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.Utility;
using System.Diagnostics;
using System.Threading;
namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
{
@@ -27,6 +28,8 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
private static readonly Lazy<SchemaCompareService> instance = new Lazy<SchemaCompareService>(() => new SchemaCompareService());
private Lazy<ConcurrentDictionary<string, SchemaComparisonResult>> schemaCompareResults =
new Lazy<ConcurrentDictionary<string, SchemaComparisonResult>>(() => new ConcurrentDictionary<string, SchemaComparisonResult>());
private Lazy<ConcurrentDictionary<string, Action>> currentComparisonCancellationAction =
new Lazy<ConcurrentDictionary<string, Action>>(() => new ConcurrentDictionary<string, Action>());
// For testability
internal Task CurrentSchemaCompareTask;
@@ -46,6 +49,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
public void InitializeService(ServiceHost serviceHost)
{
serviceHost.SetRequestHandler(SchemaCompareRequest.Type, this.HandleSchemaCompareRequest);
serviceHost.SetRequestHandler(SchemaCompareCancellationRequest.Type, this.HandleSchemaCompareCancelRequest);
serviceHost.SetRequestHandler(SchemaCompareGenerateScriptRequest.Type, this.HandleSchemaCompareGenerateScriptRequest);
serviceHost.SetRequestHandler(SchemaComparePublishChangesRequest.Type, this.HandleSchemaComparePublishChangesRequest);
serviceHost.SetRequestHandler(SchemaCompareIncludeExcludeNodeRequest.Type, this.HandleSchemaCompareIncludeExcludeNodeRequest);
@@ -78,6 +82,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
try
{
operation = new SchemaCompareOperation(parameters, sourceConnInfo, targetConnInfo);
currentComparisonCancellationAction.Value[operation.OperationId] = operation.Cancel;
operation.Execute(parameters.TaskExecutionMode);
// add result to dictionary of results
@@ -110,6 +115,40 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
}
}
/// <summary>
/// Handles schema compare cancel request
/// </summary>
/// <returns></returns>
public async Task HandleSchemaCompareCancelRequest(SchemaCompareCancelParams parameters, RequestContext<ResultStatus> requestContext)
{
try
{
Action cancelAction = null;
if (currentComparisonCancellationAction.Value.TryRemove(parameters.OperationId, out cancelAction))
{
if(cancelAction != null)
{
cancelAction.Invoke();
await requestContext.SendResult(new ResultStatus()
{
Success = true,
ErrorMessage = null
});
}
}
await requestContext.SendResult(new ResultStatus()
{
Success = false,
ErrorMessage = SR.SchemaCompareSessionNotFound
});
}
catch (Exception e)
{
await requestContext.SendError(e);
}
}
/// <summary>
/// Handles request for schema compare generate deploy script
/// </summary>