Skip to content

Commit

Permalink
add support pending operation marker
Browse files Browse the repository at this point in the history
  • Loading branch information
suwatch committed Oct 21, 2017
1 parent 82fc021 commit 790af48
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
13 changes: 11 additions & 2 deletions Kudu.Console/Program.cs
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Kudu.Console.Services;
using Kudu.Contracts.Infrastructure;
using Kudu.Contracts.Settings;
Expand Down Expand Up @@ -163,8 +164,16 @@ private static int Main(string[] args)
{
try
{
deploymentManager.DeployAsync(gitRepository, changeSet: null, deployer: deployer, clean: false)
.Wait();
// although the api is called DeployAsync, most expensive works are done synchronously.
// need to launch separate task to go async explicitly (consistent with FetchDeploymentManager)
var deploymentTask = Task.Run(async () => await deploymentManager.DeployAsync(gitRepository, changeSet: null, deployer: deployer, clean: false));

#pragma warning disable 4014
// Track pending task
PostDeploymentHelper.TrackPendingOperation(deploymentTask, TimeSpan.Zero);
#pragma warning restore 4014

deploymentTask.Wait();

if (PostDeploymentHelper.IsAutoSwapEnabled())
{
Expand Down
5 changes: 5 additions & 0 deletions Kudu.Core/Deployment/FetchDeploymentManager.cs
Expand Up @@ -369,6 +369,11 @@ private bool ShouldDeploy(IRepository repository, DeploymentInfoBase deploymentI
}
});

#pragma warning disable 4014
// Track pending task
PostDeploymentHelper.TrackPendingOperation(deploymentTask, TimeSpan.Zero);
#pragma warning restore 4014

// When the frontend/ARM calls /deploy with isAsync=true, it starts polling
// the deployment status immediately, so it's important that the temp deployment
// is created before we return.
Expand Down
47 changes: 46 additions & 1 deletion Kudu.Core/Helpers/PostDeploymentHelper.cs
Expand Up @@ -12,7 +12,7 @@
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using Kudu.Contracts.Settings;
using Microsoft.Win32.SafeHandles;
using Kudu.Core.Infrastructure;

namespace Kudu.Core.Helpers
{
Expand Down Expand Up @@ -394,6 +394,51 @@ public static void RunPostDeploymentScripts(TraceListener tracer)
}
}

/// <summary>
/// As long as the task was not completed, we will keep updating the marker file.
/// The routine completes when either task completes or timeout.
/// If task is completed, we will remove the marker.
/// If timeout, we will leave the stale marker.
/// </summary>
public static async Task TrackPendingOperation(Task task, TimeSpan timeout)
{
const int DefaultTimeoutMinutes = 30;
const int DefaultUpdateMarkerIntervalMS = 10000;
const string MarkerFilePath = @"%TEMP%\SCMPendingOperation.txt";

// only applicable to azure env
if (!Environment.IsAzureEnvironment())
{
return;
}

if (timeout <= TimeSpan.Zero || timeout >= TimeSpan.FromMinutes(DefaultTimeoutMinutes))
{
// track at most N mins by default
timeout = TimeSpan.FromMinutes(DefaultTimeoutMinutes);
}

var start = DateTime.UtcNow;
var markerFile = System.Environment.ExpandEnvironmentVariables(MarkerFilePath);
while (start.Add(timeout) >= DateTime.UtcNow)
{
// create or update marker timestamp
OperationManager.SafeExecute(() => File.WriteAllText(markerFile, start.ToString("o")));

var cancelation = new CancellationTokenSource();
var delay = Task.Delay(DefaultUpdateMarkerIntervalMS, cancelation.Token);
var completed = await Task.WhenAny(delay, task);
if (completed != delay)
{
cancelation.Cancel();
break;
}
}

// remove marker
OperationManager.SafeExecute(() => File.Delete(markerFile));
}

private static void ExecuteScript(string file)
{
var fi = new FileInfo(file);
Expand Down
16 changes: 14 additions & 2 deletions Kudu.Core/SiteExtensions/SiteExtensionManager.cs
Expand Up @@ -18,6 +18,7 @@
using Kudu.Contracts.SiteExtensions;
using Kudu.Contracts.Tracing;
using Kudu.Core.Deployment.Generator;
using Kudu.Core.Helpers;
using Kudu.Core.Infrastructure;
using Kudu.Core.Settings;
using Kudu.Core.Tracing;
Expand Down Expand Up @@ -210,8 +211,19 @@ public async Task<SiteExtensionInfo> GetLocalExtension(string id, bool checkLate
return info;
}

// <inheritdoc />
public async Task<SiteExtensionInfo> InstallExtension(string id, string version, string feedUrl, SiteExtensionInfo.SiteExtensionType type, ITracer tracer, string installationArgs = null)
public Task<SiteExtensionInfo> InstallExtension(string id, string version, string feedUrl, SiteExtensionInfo.SiteExtensionType type, ITracer tracer, string installationArgs = null)
{
var installationTask = InstallExtensionCore(id, version, feedUrl, type, tracer, installationArgs);

#pragma warning disable 4014
// Track pending task
PostDeploymentHelper.TrackPendingOperation(installationTask, TimeSpan.Zero);
#pragma warning restore 4014

return installationTask;
}

private async Task<SiteExtensionInfo> InstallExtensionCore(string id, string version, string feedUrl, SiteExtensionInfo.SiteExtensionType type, ITracer tracer, string installationArgs)
{
try
{
Expand Down

0 comments on commit 790af48

Please sign in to comment.