Skip to content

Commit

Permalink
Retry publishing to improve reliability. Fixes #108
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmelsayed committed May 24, 2017
1 parent 839ccee commit b2e05d2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/Azure.Functions.Cli/Actions/AzureActions/PublishFunctionApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,33 @@ public override async Task RunAsync()
ColoredConsole.WriteLine(WarningColor($"Publish {functionAppRoot} contents to an Azure Function App. Locally deleted files are not removed from destination."));
ColoredConsole.WriteLine("Getting site publishing info...");
var functionApp = await _armManager.GetFunctionAppAsync(FunctionAppName);
using (var client = await GetRemoteZipClient(new Uri($"https://{functionApp.ScmUri}")))
using (var request = new HttpRequestMessage())
await RetryHelper.Retry(async () =>
{
request.Method = HttpMethod.Put;
request.RequestUri = new Uri("api/zip/site/wwwroot", UriKind.Relative);
request.Headers.IfMatch.Add(EntityTagHeaderValue.Any);
ColoredConsole.WriteLine("Creating archive for current directory...");
request.Content = CreateZip(functionAppRoot);
ColoredConsole.WriteLine("Uploading archive...");
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
response = await client.PostAsync("api/functions/synctriggers", content: null);
response.EnsureSuccessStatusCode();
ColoredConsole.WriteLine("Upload completed successfully.");
}
using (var client = await GetRemoteZipClient(new Uri($"https://{functionApp.ScmUri}")))
using (var request = new HttpRequestMessage(HttpMethod.Put, new Uri("api/zip/site/wwwroot", UriKind.Relative)))
{
request.Headers.IfMatch.Add(EntityTagHeaderValue.Any);
ColoredConsole.WriteLine("Creating archive for current directory...");
request.Content = CreateZip(functionAppRoot);
ColoredConsole.WriteLine("Uploading archive...");
var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
throw new CliException($"Error uploading archive ({response.StatusCode}).");
}
response = await client.PostAsync("api/functions/synctriggers", content: null);
if(!response.IsSuccessStatusCode)
{
throw new CliException($"Error calling sync triggers ({response.StatusCode}).");
}
ColoredConsole.WriteLine("Upload completed successfully.");
}
}, 2);
}

private static StreamContent CreateZip(string path)
Expand Down
1 change: 1 addition & 0 deletions src/Azure.Functions.Cli/Azure.Functions.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Helpers\DebuggerHelper.cs" />
<Compile Include="Helpers\PlatformHelper.cs" />
<Compile Include="Helpers\RetryHelper.cs" />
<Compile Include="Helpers\ScriptHostHelpers.cs" />
<Compile Include="Helpers\SecurityHelpers.cs" />
<Compile Include="Interfaces\IAction.cs" />
Expand Down
34 changes: 34 additions & 0 deletions src/Azure.Functions.Cli/Helpers/RetryHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Threading.Tasks;
using Colors.Net;
using static Azure.Functions.Cli.Common.OutputTheme;

namespace Azure.Functions.Cli.Helpers
{
internal class RetryHelper
{
public static async Task Retry(Func<Task> func, int retryCount)
{
var totalRetries = retryCount;
while (true)
{
try
{
await func();
return;
}
catch (Exception e)
{
if (retryCount <= 0)
{
throw e;
}
retryCount--;
ColoredConsole.Error.WriteLine(ErrorColor(e.Message));
ColoredConsole.Error.WriteLine(ErrorColor($"Retry: {totalRetries - retryCount} of {totalRetries}"));
}
await Task.Delay(1000);
}
}
}
}

0 comments on commit b2e05d2

Please sign in to comment.