Permalink
Browse files

Keep fetching while there's more work to do for the FetchHandler.

- Create marker file if there's a deployment in progress
  and check that it exists after the current deployment is complete.
  • Loading branch information...
1 parent f4cfa6d commit 2d7d44422e8af6ab0041ec267fcee33e75cafa25 @davidfowl davidfowl committed Aug 27, 2012
@@ -48,22 +48,25 @@ internal static string EnsureDirectory(IFileSystem fileSystem, string path)
return path;
}
- public static void DeleteFileSafe(string path)
+ public static bool DeleteFileSafe(string path)
{
- DeleteFileSafe(new FileSystem(), path);
+ return DeleteFileSafe(new FileSystem(), path);
}
- internal static void DeleteFileSafe(IFileSystem fileSystem, string path)
+ internal static bool DeleteFileSafe(IFileSystem fileSystem, string path)
{
try
{
if (fileSystem.File.Exists(path))
{
fileSystem.File.Delete(path);
+ return true;
}
}
catch (UnauthorizedAccessException) { }
catch (FileNotFoundException) { }
+
+ return false;
}
private static void DeleteFileSystemInfo(FileSystemInfoBase fileSystemInfo)
@@ -3,6 +3,8 @@
using System.Linq;
using System.Net;
using System.Net.Http;
+using System.Threading;
+using System.Threading.Tasks;
using Kudu.Client.Deployment;
using Kudu.Core.Deployment;
using Kudu.FunctionalTests.Infrastructure;
@@ -314,6 +316,54 @@ public void DeployingBranchThatExists()
});
}
+ [Fact]
+ public void PullApiTestConsecutivePushesGetQueued()
+ {
+ string payload = @"{ ""oldRef"": ""0000000000000000000"", ""newRef"": ""ad21595c668f3de813463df17c04a3b23065fedc"", ""url"": ""https://github.com/KuduApps/RepoWithMultipleBranches.git"", ""deployer"" : ""me!"", branch: ""test"" }";
+ string appName = KuduUtils.GetRandomWebsiteName("PullApiTestPushesGetQueued");
+
+ ApplicationManager.Run(appName, appManager =>
+ {
+ var handler = new HttpClientHandler
+ {
+ Credentials = appManager.DeploymentManager.Credentials
+ };
+
+ var client = new HttpClient(handler)
+ {
+ BaseAddress = new Uri(appManager.ServiceUrl),
+ Timeout = TimeSpan.FromMinutes(5)
+ };
+
+ var post = new Dictionary<string, string>
+ {
+ { "payload", payload }
+ };
+
+ Task<HttpResponseMessage> responseTask = null;
+
+ // Do another post in parallel a second after
+ new Thread(() =>
+ {
+ Thread.Sleep(1000);
+
+ // Ideally we'd push something else to github but at least this exercises the code path
+ responseTask = client.PostAsync("deploy", new FormUrlEncodedContent(post));
+
+ }).Start();
+
+ client.PostAsync("deploy", new FormUrlEncodedContent(post)).Wait();
+
+ var results = appManager.DeploymentManager.GetResultsAsync().Result.ToList();
+ Assert.NotNull(responseTask);
+ Assert.Equal(HttpStatusCode.Conflict, responseTask.Result.StatusCode);
+ Assert.Equal(1, results.Count);
+ Assert.Equal(DeployStatus.Success, results[0].Status);
+ KuduAssert.VerifyUrl(appManager.SiteUrl, "Master branch");
+ Assert.Equal("me!", results[0].Deployer);
+ });
+ }
+
private class FakeMessageHandler : DelegatingHandler
{
public Uri Url { get; set; }
@@ -106,7 +106,7 @@ public void ProcessRequest(HttpContext context)
{
// Create a marker file that indicates if there's another deployment to pull
// because there was a deployment in progress.
- using (_tracer.Step("Creating maker file"))
+ using (_tracer.Step("Creating pending deployment maker file"))
{
// REVIEW: This makes the assumption that the repository url is the same.
// If it isn't the result would be buggy either way.
@@ -129,33 +129,51 @@ private bool MarkerFileExists()
return File.Exists(MarkerFilePath);
}
- private void DeleteMarkerFile()
+ private bool DeleteMarkerFile()
{
- FileSystemHelpers.DeleteFileSafe(MarkerFilePath);
+ return FileSystemHelpers.DeleteFileSafe(MarkerFilePath);
}
private void PerformDeployment(RepositoryInfo repositoryInfo, string targetBranch)
{
- using (_tracer.Step("Performing fetch based deployment"))
+ bool hasPendingDeployment;
+
+ do
{
- _gitServer.Initialize(_configuration);
- _gitServer.SetReceiveInfo(repositoryInfo.OldRef, repositoryInfo.NewRef, targetBranch);
- _gitServer.FetchWithoutConflict(repositoryInfo.RepositoryUrl, "external", targetBranch);
- _deploymentManager.Deploy(repositoryInfo.Deployer);
+ hasPendingDeployment = false;
- if (MarkerFileExists())
+ using (_tracer.Step("Performing fetch based deployment"))
{
- using (_tracer.Step("Marker file exists"))
+ // Configure the repository
+ _gitServer.Initialize(_configuration);
+
+ // Setup the receive info (this is important to know if branches were deleted etc)
+ _gitServer.SetReceiveInfo(repositoryInfo.OldRef, repositoryInfo.NewRef, targetBranch);
+
+ // Fetch from url
+ _gitServer.FetchWithoutConflict(repositoryInfo.RepositoryUrl, "external", targetBranch);
+
+ // Perform the actual deployment
+ _deploymentManager.Deploy(repositoryInfo.Deployer);
+
+ if (MarkerFileExists())
{
- using (_tracer.Step("Deleting marker file"))
+ _tracer.Trace("Pending deployment marker file exists");
+
+ hasPendingDeployment = DeleteMarkerFile();
+
+ if (hasPendingDeployment)
{
- DeleteMarkerFile();
+ _tracer.Trace("Deleted marker file");
+ }
+ else
+ {
+ _tracer.TraceError("Failed to delete marker file");
}
-
- PerformDeployment(repositoryInfo, targetBranch);
}
}
- }
+
+ } while (hasPendingDeployment);
}
private void TracePayload(string json)

0 comments on commit 2d7d444

Please sign in to comment.