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 4 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
3 changes: 3 additions & 0 deletions DeployClient/App.config
Expand Up @@ -19,6 +19,9 @@
<setting name="EncryptionKey" serializeAs="String">
<value />
</setting>
<setting name="InstallationStatusTimeout" serializeAs="String">
<value />
</setting>
</DeployClient.Properties.Settings>
</applicationSettings>
</configuration>
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 = null)]
public double? InstallationStatusTimeout { get; set; }

[HelpOption]
public string GetUsage()
{
Expand Down
56 changes: 47 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 ?? 60);

// 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 Expand Up @@ -230,6 +260,14 @@ private static void GetSettings(string[] args)
{
Options.EncryptionKey = Properties.Settings.Default.EncryptionKey;
}
if (Options.InstallationStatusTimeout == null)
{
double settingsFileTimeout;
if (!string.IsNullOrWhiteSpace(Properties.Settings.Default.InstallationStatusTimeout) && double.TryParse(Properties.Settings.Default.InstallationStatusTimeout, out settingsFileTimeout))
{
Options.InstallationStatusTimeout = settingsFileTimeout;
}
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion DeployClient/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions DeployClient/Properties/Settings.settings
Expand Up @@ -11,5 +11,8 @@
<Setting Name="EncryptionKey" Type="System.String" Scope="Application">
<Value Profile="(Default)" />
</Setting>
<Setting Name="InstallationStatusTimeout" Type="System.String" Scope="Application">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>