Skip to content

Commit 18678e0

Browse files
committed
Implementing Host Dynamic Debug Mode and throttled/conditional file logging
1 parent 45de917 commit 18678e0

27 files changed

+422
-126
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
$in = Get-Content $triggerInput
22
$json = $in | ConvertFrom-Json
3+
4+
Write-Output "PowerShell script processed queue message '$json'"
5+
36
$entity = [string]::Format('{{ "Status": 0, "Title": "PowerShell Table Entity for message {0}" }}', $json.id)
47
$entity | Out-File -Encoding Ascii $output

src/WebJobs.Script.WebHost/App_Start/WebApiConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void Register(HttpConfiguration config, WebHostSettings settings =
4747
var container = builder.Build();
4848
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
4949

50-
config.MessageHandlers.Add(new EnsureHostRunningHandler(config));
50+
config.MessageHandlers.Add(new WebScriptHostHandler(config));
5151

5252
// Web API configuration and services
5353

src/WebJobs.Script.WebHost/App_Start/WebHostResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private static ScriptHostConfiguration GetScriptHostConfiguration(string scriptP
182182
{
183183
RootScriptPath = scriptPath,
184184
RootLogPath = logPath,
185-
FileLoggingEnabled = true
185+
FileLoggingMode = FileLoggingMode.DebugOnly
186186
};
187187

188188
// If running on Azure Web App, derive the host ID from the site name

src/WebJobs.Script.WebHost/Controllers/AdminController.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
using System.Linq;
77
using System.Net;
88
using System.Net.Http;
9+
using System.Threading;
910
using System.Threading.Tasks;
1011
using System.Web.Http;
12+
using System.Web.Http.Controllers;
1113
using Microsoft.Azure.WebJobs.Script.Description;
1214
using Microsoft.Azure.WebJobs.Script.WebHost.Filters;
1315
using Microsoft.Azure.WebJobs.Script.WebHost.Models;
@@ -94,5 +96,15 @@ public HostStatus GetHostStatus()
9496

9597
return status;
9698
}
99+
100+
public override Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
101+
{
102+
// For all admin api requests, we'll update the ScriptHost debug timeout
103+
// For now, we'll enable debug mode on ANY admin requests. Since the Portal interacts through
104+
// the admin API this is sufficient for identifying when the Portal is connected.
105+
_scriptHostManager.Instance.NotifyDebug();
106+
107+
return base.ExecuteAsync(controllerContext, cancellationToken);
108+
}
97109
}
98110
}

src/WebJobs.Script.WebHost/Handlers/EnsureHostRunningHandler.cs renamed to src/WebJobs.Script.WebHost/Handlers/WebScriptHostHandler.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Linq;
56
using System.Net;
67
using System.Net.Http;
78
using System.Threading;
@@ -10,19 +11,20 @@
1011

1112
namespace Microsoft.Azure.WebJobs.Script.WebHost.Handlers
1213
{
13-
public class EnsureHostRunningHandler : DelegatingHandler
14+
public class WebScriptHostHandler : DelegatingHandler
1415
{
15-
private readonly TimeSpan _hostTimeout = new TimeSpan(0, 0, 30);
16+
private readonly TimeSpan _hostTimeoutSeconds;
1617
private readonly int _hostRunningPollIntervalMs = 500;
1718
private readonly HttpConfiguration _config;
1819

19-
public EnsureHostRunningHandler(HttpConfiguration config)
20+
public WebScriptHostHandler(HttpConfiguration config, int hostTimeoutSeconds = 30)
2021
{
2122
if (config == null)
2223
{
2324
throw new ArgumentNullException("config");
2425
}
2526

27+
_hostTimeoutSeconds = new TimeSpan(0, 0, hostTimeoutSeconds);
2628
_config = config;
2729
}
2830

@@ -46,7 +48,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
4648
// initialize. This might happen if http requests come in while the
4749
// host is starting up for the first time, or if it is restarting.
4850
TimeSpan timeWaited = TimeSpan.Zero;
49-
while (!scriptHostManager.IsRunning && (timeWaited < _hostTimeout))
51+
while (!scriptHostManager.IsRunning && (timeWaited < _hostTimeoutSeconds))
5052
{
5153
await Task.Delay(_hostRunningPollIntervalMs);
5254
timeWaited += TimeSpan.FromMilliseconds(_hostRunningPollIntervalMs);

src/WebJobs.Script.WebHost/WebJobs.Script.WebHost.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@
317317
<DependentUpon>Global.asax</DependentUpon>
318318
</Compile>
319319
<Compile Include="GlobalSuppressions.cs" />
320-
<Compile Include="Handlers\EnsureHostRunningHandler.cs" />
320+
<Compile Include="Handlers\WebScriptHostHandler.cs" />
321321
<Compile Include="HostSecrets.cs" />
322322
<Compile Include="Models\FunctionInvocation.cs" />
323323
<Compile Include="Models\FunctionMetrics.cs" />

src/WebJobs.Script.WebHost/WebScriptHostManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static bool IsAzureEnvironment
5151

5252
private IDictionary<string, FunctionDescriptor> HttpFunctions { get; set; }
5353

54-
public bool Initialized
54+
public virtual bool Initialized
5555
{
5656
get
5757
{
@@ -179,7 +179,7 @@ public static void WarmUp(WebHostSettings settings)
179179
ScriptHostConfiguration config = new ScriptHostConfiguration
180180
{
181181
RootScriptPath = rootPath,
182-
FileLoggingEnabled = false,
182+
FileLoggingMode = FileLoggingMode.Never,
183183
RootLogPath = settings.LogPath,
184184
TraceWriter = traceWriter,
185185
FileWatchingEnabled = false

src/WebJobs.Script/Config/ScriptHostConfiguration.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public ScriptHostConfiguration()
1515
{
1616
HostConfig = new JobHostConfiguration();
1717
FileWatchingEnabled = true;
18+
FileLoggingMode = FileLoggingMode.Never;
1819
RootScriptPath = Environment.CurrentDirectory;
1920
RootLogPath = Path.Combine(Path.GetTempPath(), "Functions");
2021
}
@@ -48,11 +49,11 @@ public ScriptHostConfiguration()
4849
public bool FileWatchingEnabled { get; set; }
4950

5051
/// <summary>
51-
/// Gets or sets a value indicating whether logs should be written to disk.
52-
/// The default is false. When set to true, logs will be written to the directory
53-
/// specified by <see cref="RootLogPath"/>.
52+
/// Gets or sets a value governing when logs should be written to disk.
53+
/// When enabled, logs will be written to the directory specified by
54+
/// <see cref="RootLogPath"/>.
5455
/// </summary>
55-
public bool FileLoggingEnabled { get; set; }
56+
public FileLoggingMode FileLoggingMode { get; set; }
5657

5758
/// <summary>
5859
/// Gets the list of functions that should be run. This list can be used to filter

src/WebJobs.Script/Description/FunctionInvokerBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ internal FunctionInvokerBase(ScriptHost host, FunctionMetadata functionMetadata,
2727

2828
traceWriterFactory = traceWriterFactory ?? new FunctionTraceWriterFactory(functionMetadata.Name, Host.ScriptConfig);
2929
TraceWriter traceWriter = traceWriterFactory.Create();
30-
TraceWriter = new ConditionalTraceWriter(traceWriter, t => !(t.Properties?.ContainsKey(PrimaryHostTracePropertyName) ?? false) || Host.IsPrimary);
30+
31+
// Function file logging is only done conditionally
32+
TraceWriter = traceWriter.Conditional(t => Host.FileLoggingEnabled && (!(t.Properties?.ContainsKey(PrimaryHostTracePropertyName) ?? false) || Host.IsPrimary));
3133
}
3234

3335
protected static IDictionary<string, object> PrimaryHostTraceProperties => _primaryHostTraceProperties;

src/WebJobs.Script/Diagnostics/ConditionalTraceWriter.cs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ namespace Microsoft.Azure.WebJobs.Script
1010
public sealed class ConditionalTraceWriter : TraceWriter
1111
{
1212
private readonly Func<TraceEvent, bool> _predicate;
13-
private readonly FailedConditionTraceBehavior _failedConditionBehavior;
1413

15-
public ConditionalTraceWriter(TraceWriter innerWriter, Func<TraceEvent, bool> predicate, FailedConditionTraceBehavior failedConditionBehavior = FailedConditionTraceBehavior.TraceAsVerbose)
14+
public ConditionalTraceWriter(TraceWriter innerWriter, Func<TraceEvent, bool> predicate)
1615
: base(innerWriter?.Level ?? TraceLevel.Off)
1716
{
1817
if (innerWriter == null)
@@ -27,7 +26,6 @@ public ConditionalTraceWriter(TraceWriter innerWriter, Func<TraceEvent, bool> pr
2726

2827
InnerWriter = innerWriter;
2928
_predicate = predicate;
30-
_failedConditionBehavior = failedConditionBehavior;
3129
}
3230

3331
public TraceWriter InnerWriter { get; }
@@ -38,33 +36,6 @@ public override void Trace(TraceEvent traceEvent)
3836
{
3937
InnerWriter.Trace(traceEvent);
4038
}
41-
else if (_failedConditionBehavior == FailedConditionTraceBehavior.TraceAsVerbose)
42-
{
43-
var eventClone = CloneEvent(traceEvent);
44-
45-
// To improve troubleshooting, instead of completely suppressing the event, we'll downgrade
46-
// it to 'Verbose' and add a prefix to the message.
47-
eventClone.Message = $"[Supressed '{traceEvent.Level}' trace] {traceEvent.Message}";
48-
eventClone.Level = TraceLevel.Verbose;
49-
50-
InnerWriter.Trace(eventClone);
51-
}
52-
}
53-
54-
private static TraceEvent CloneEvent(TraceEvent traceEvent)
55-
{
56-
var result = new TraceEvent(traceEvent.Level, traceEvent.Message,
57-
traceEvent.Source, traceEvent.Exception)
58-
{
59-
Timestamp = traceEvent.Timestamp
60-
};
61-
62-
foreach (var property in traceEvent.Properties)
63-
{
64-
result.Properties.Add(property);
65-
}
66-
67-
return result;
6839
}
6940
}
7041
}

0 commit comments

Comments
 (0)