Permalink
Browse files

add logstream client time limit

  • Loading branch information...
1 parent dad7327 commit 7c3ef494002bc8134646e4080f3ec442207e6597 @suwatch suwatch committed Jan 18, 2013
@@ -7,13 +7,14 @@ namespace Kudu.Contracts.Settings
public static class DeploymentSettingsExtension
{
public static TimeSpan DefaultCommandIdleTimeout = TimeSpan.FromSeconds(180);
+ public static TimeSpan DefaultLogStreamTimeout = TimeSpan.FromSeconds(1800);
public const TraceLevel DefaultTraceLevel = TraceLevel.Error;
public static TraceLevel GetTraceLevel(this IDeploymentSettingsManager settings)
{
string value = settings.GetValue(SettingsKeys.TraceLevel);
int level;
- if (!String.IsNullOrEmpty(value) && Int32.TryParse(value, out level))
+ if (Int32.TryParse(value, out level))
{
if (level <= (int)TraceLevel.Off)
{
@@ -36,14 +37,26 @@ public static TimeSpan GetCommandIdleTimeout(this IDeploymentSettingsManager set
{
string value = settings.GetValue(SettingsKeys.CommandIdleTimeout);
int seconds;
- if (!String.IsNullOrEmpty(value) && Int32.TryParse(value, out seconds))
+ if (Int32.TryParse(value, out seconds))
{
- return TimeSpan.FromSeconds(seconds);
+ return TimeSpan.FromSeconds(seconds >= 0 ? seconds : 0);
}
return DeploymentSettingsExtension.DefaultCommandIdleTimeout;
}
+ public static TimeSpan GetLogStreamTimeout(this IDeploymentSettingsManager settings)
+ {
+ string value = settings.GetValue(SettingsKeys.LogStreamTimeout);
+ int seconds;
+ if (Int32.TryParse(value, out seconds))
+ {
+ return TimeSpan.FromSeconds(seconds >= 0 ? seconds : 0);
+ }
+
+ return DeploymentSettingsExtension.DefaultLogStreamTimeout;
+ }
+
public static string GetGitUsername(this IDeploymentSettingsManager settings)
{
string value = settings.GetValue(SettingsKeys.GitUsername);
@@ -6,6 +6,7 @@ public static class SettingsKeys
public const string BuildArgs = "build_args";
public const string TraceLevel = "trace_level";
public const string CommandIdleTimeout = "command_idle_timeout";
+ public const string LogStreamTimeout = "logstream_timeout";
public const string SiteBuilderFactory = "site_builder_factory";
public const string GitUsername = "git.username";
public const string GitEmail = "git.email";
@@ -0,0 +1,98 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Web;
+using Kudu.Contracts.Settings;
+using Kudu.Core.SourceControl;
+using Kudu.Core.SourceControl.Git;
+using Kudu.Services.ServiceHookHandlers;
+using Moq;
+using Newtonsoft.Json.Linq;
+using Xunit;
+using Xunit.Extensions;
+
+namespace Kudu.Core.Test
+{
+ public class DeploymentSettingFacts
+ {
+ [Theory, ClassData(typeof(CommandIdleTimeoutData))]
+ public void CommandIdleTimeoutTests(string value, int expected)
+ {
+ // Act
+ MockDeploymentSettingsManager settings = new MockDeploymentSettingsManager();
+ settings.SetValue(SettingsKeys.CommandIdleTimeout, value);
+
+ // Assert
+ Assert.Equal(TimeSpan.FromSeconds(expected), settings.GetCommandIdleTimeout());
+ }
+
+ [Theory, ClassData(typeof(LogStreamTimeoutData))]
+ public void LogStreamTimeoutTests(string value, int expected)
+ {
+ // Act
+ MockDeploymentSettingsManager settings = new MockDeploymentSettingsManager();
+ settings.SetValue(SettingsKeys.LogStreamTimeout, value);
+
+ // Assert
+ Assert.Equal(TimeSpan.FromSeconds(expected), settings.GetLogStreamTimeout());
+ }
+
+ [Theory, ClassData(typeof(TraceLevelData))]
+ public void TraceLevelTests(string value, TraceLevel expected)
+ {
+ // Act
+ MockDeploymentSettingsManager settings = new MockDeploymentSettingsManager();
+ settings.SetValue(SettingsKeys.TraceLevel, value);
+
+ // Assert
+ Assert.Equal(expected, settings.GetTraceLevel());
+ }
+
+ class CommandIdleTimeoutData : SettingsData
+ {
+ protected override object DefaultValue
+ {
+ get { return (int)DeploymentSettingsExtension.DefaultCommandIdleTimeout.TotalSeconds; }
+ }
+ }
+
+ class LogStreamTimeoutData : SettingsData
+ {
+ protected override object DefaultValue
+ {
+ get { return (int)DeploymentSettingsExtension.DefaultLogStreamTimeout.TotalSeconds; }
+ }
+ }
+
+ class TraceLevelData : SettingsData
+ {
+ protected override object DefaultValue
+ {
+ get { return DeploymentSettingsExtension.DefaultTraceLevel; }
+ }
+ }
+
+ abstract class SettingsData : IEnumerable<object[]>
+ {
+ protected abstract object DefaultValue { get; }
+
+ public IEnumerator<object[]> GetEnumerator()
+ {
+ yield return new object[] { "0", 0 };
+ yield return new object[] { "-0", 0 };
+ yield return new object[] { "4", 4 };
+ yield return new object[] { "-4", 0 };
+ yield return new object[] { "", DefaultValue };
+ yield return new object[] { "a", DefaultValue };
+ yield return new object[] { " ", DefaultValue };
+ yield return new object[] { null, DefaultValue };
+ }
+
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+ }
+ }
+}
@@ -82,6 +82,7 @@
<Compile Include="Infrastructure\StringReaderFacts.cs" />
<Compile Include="Infrastructure\VsSolutionTest.cs" />
<Compile Include="Infrastructure\XmlUtilityFacts.cs" />
+ <Compile Include="DeploymentSettingsFacts.cs" />
<Compile Include="JsonSettingsFacts.cs" />
<Compile Include="MockDeploymentSettingsManager.cs" />
<Compile Include="NodeSiteEnablerFacts.cs" />
@@ -20,6 +20,7 @@ public class DeploymentSettingsManager : IDeploymentSettingsManager
{ SettingsKeys.Branch, "master" },
{ SettingsKeys.TraceLevel, ((int)DeploymentSettingsExtension.DefaultTraceLevel).ToString() },
{ SettingsKeys.CommandIdleTimeout, ((int)DeploymentSettingsExtension.DefaultCommandIdleTimeout.TotalSeconds).ToString() },
+ { SettingsKeys.LogStreamTimeout, ((int)DeploymentSettingsExtension.DefaultLogStreamTimeout.TotalSeconds).ToString() },
{ SettingsKeys.BuildArgs, "" }
};
@@ -943,7 +943,7 @@ public void RepoWithPrivateSubModuleTest()
}
}
- [Fact]
+ [Fact(Skip = "need to remove SimpleSleep.exe")]
public void HangProcessTest()
{
// Arrange
@@ -26,7 +26,6 @@ public class LogStreamManager
// Antares 3 mins timeout, heartbeat every mins keep alive.
private static string[] LogFileExtensions = new string[] { ".txt", ".log", ".htm" };
private static TimeSpan HeartbeatInterval = TimeSpan.FromMinutes(1);
- private static TimeSpan IdleTimeout = TimeSpan.FromMinutes(10);
private readonly object _thisLock = new object();
private readonly string _logPath;
@@ -38,7 +37,9 @@ public class LogStreamManager
private Dictionary<string, long> _logFiles;
private FileSystemWatcher _watcher;
private Timer _heartbeat;
- private DateTime lastTraceTime = DateTime.UtcNow;
+ private DateTime _lastTraceTime = DateTime.UtcNow;
+ private DateTime _startTime = DateTime.UtcNow;
+ private TimeSpan _timeout;
private string _filter;
private bool _enableTrace;
@@ -56,6 +57,7 @@ public class LogStreamManager
_settings = settings;
_environment = environment;
_shutdownDetector = shutdownDetector;
+ _timeout = settings.GetLogStreamTimeout();
_results = new List<ProcessRequestAsyncResult>();
}
@@ -179,14 +181,15 @@ private void OnHeartbeat(object state)
{
try
{
- TimeSpan ts = DateTime.UtcNow.Subtract(lastTraceTime);
- if (ts >= HeartbeatInterval)
+ TimeSpan ts = DateTime.UtcNow.Subtract(_startTime);
+ if (ts >= _timeout)
{
- if (ts >= IdleTimeout)
- {
- TerminateClient(String.Format(CultureInfo.CurrentCulture, Resources.LogStream_Idle, DateTime.UtcNow.ToString("s"), (int)ts.TotalMinutes, Environment.NewLine));
- }
- else
+ TerminateClient(String.Format(CultureInfo.CurrentCulture, Resources.LogStream_Timeout, DateTime.UtcNow.ToString("s"), (int)ts.TotalMinutes, Environment.NewLine));
+ }
+ else
+ {
+ ts = DateTime.UtcNow.Subtract(_lastTraceTime);
+ if (ts >= HeartbeatInterval)
{
NotifyClient(String.Format(CultureInfo.CurrentCulture, Resources.LogStream_Heartbeat, DateTime.UtcNow.ToString("s"), (int)ts.TotalMinutes, Environment.NewLine));
}
@@ -245,7 +248,7 @@ private void OnChanged(object sender, FileSystemEventArgs e)
if (lines.Count() > 0)
{
- lastTraceTime = DateTime.UtcNow;
+ _lastTraceTime = DateTime.UtcNow;
NotifyClient(lines);
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -219,8 +219,8 @@
<data name="LogStream_Heartbeat" xml:space="preserve">
<value>{0} No new trace in the past {1} min(s).{2}</value>
</data>
- <data name="LogStream_Idle" xml:space="preserve">
- <value>{0} Stream terminated due to no new trace in the past {1} min(s).{2}</value>
+ <data name="LogStream_Timeout" xml:space="preserve">
+ <value>{0} Stream terminated due to timeout {1} min(s).{2}</value>
</data>
<data name="LogStream_Welcome" xml:space="preserve">
<value>{0} Welcome, you are now connected to log-streaming service.{1}</value>

0 comments on commit 7c3ef49

Please sign in to comment.