Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 404 error #17

Merged
6 commits merged into from May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 20 additions & 4 deletions DeployClient/API.cs
Expand Up @@ -5,6 +5,7 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Script.Serialization;

namespace DeployClient
Expand Down Expand Up @@ -47,17 +48,32 @@ public static string CreateSession()
}
}

public static Dictionary<string, dynamic> GetSession(string sessionGuid)
public static bool GetSession(string sessionGuid, out Dictionary<string, dynamic> results)
{
string endpoint = string.Format("Remote/GetSession?sessionGuid={0}", sessionGuid);

JavaScriptSerializer jsonSer = new JavaScriptSerializer();

using (HttpClient client = BuildClient())
{
string json = client.GetStringAsync(endpoint).Result;

return jsonSer.Deserialize<Dictionary<string, dynamic>>(json);
bool success;
var httpResponse = client.GetAsync(endpoint).Result;
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
string json = httpResponse.Content.ReadAsStringAsync().Result;
results = jsonSer.Deserialize<Dictionary<string, dynamic>>(json);
success = true;
}
else if (httpResponse.StatusCode == HttpStatusCode.NotFound)
{
results = new Dictionary<string, dynamic>();
success = false;
}
else
{
throw new HttpException($"Invalid status code returned from remote api: {httpResponse.StatusCode}");
}
return success;
}
}

Expand Down
6 changes: 6 additions & 0 deletions DeployClient/CommandlineOptions.cs
Expand Up @@ -38,6 +38,12 @@ internal class CommandLineOptions
DefaultValue = null)]
public string EncryptionKey { get; set; }

[Option("installation-status-timeout",
Required = false,
HelpText = "The number of seconds to ignore 404 errors when checking installation status",
DefaultValue = 60)]
public double InstallationStatusTimeout { get; set; }

[HelpOption]
public string GetUsage()
{
Expand Down
1 change: 1 addition & 0 deletions DeployClient/DeployClient.csproj
Expand Up @@ -39,6 +39,7 @@
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down
48 changes: 39 additions & 9 deletions DeployClient/Program.cs
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Script.Serialization;

namespace DeployClient
Expand Down Expand Up @@ -132,19 +133,40 @@ static void Main(string[] args)

if (!API.Install(sessionGuid, out results))
{
DateTime abortTime = DateTime.Now.AddMinutes(10);
TimeSpan interval = new TimeSpan(0, 0, 0, 2);
Dictionary<string, dynamic> response;
var successfullyReachedApi = false;
DateTime apiNotFoundAbortTime = DateTime.Now.AddSeconds(Options.InstallationStatusTimeout);

// Attempt to get the status of the session from the remote api.
// This can fail shortly after an installation as the api has not yet been initialised,
// so attempt to get it for the given timespan.
do
{
// Get whether we can reach the api
successfullyReachedApi = API.GetSession(sessionGuid, out response);

if (!successfullyReachedApi)
{
// Api is returning a 404 - wait and try again
System.Threading.Thread.Sleep(interval);
}
} while (!successfullyReachedApi && DateTime.Now < apiNotFoundAbortTime);

// If the api couldn't be reached by the given time, something has gone wrong
if (!successfullyReachedApi)
{
throw new HttpException("Remote API returned status 404");
}

int status = -1;
string previousPrint = null;

DateTime abortTime = DateTime.Now.AddMinutes(10);

// While the process isn't complete and we haven't exceeded our abort time.
while (status < 2 && DateTime.Now < abortTime)
// Get the installation status from the API until it is complete or until the abort time is reached
do
{
// Get response.
Dictionary<string, dynamic> response = API.GetSession(sessionGuid);

// Is there a status key?
if (response.ContainsKey("Status"))
{
// Yes, get the status.
Expand Down Expand Up @@ -178,9 +200,17 @@ static void Main(string[] args)
break;
}

// Sleep.
System.Threading.Thread.Sleep(interval);
}

var success = API.GetSession(sessionGuid, out response);

// The api should not be returning a 404 status at this point
if (!success)
{
throw new HttpException("Remote API returned status 404");
}
} while (status < 2 && DateTime.Now < abortTime);

}
else
{
Expand Down