Skip to content

Commit b4313d4

Browse files
committed
Fixing self host startup issues
1 parent 4850cc1 commit b4313d4

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

src/WebJobs.Script/Host/ScriptHost.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected internal ScriptHost(IScriptHostEnvironment environment,
101101
#if FEATURE_NODE
102102
NodeFunctionInvoker.UnhandledException += OnUnhandledException;
103103
#endif
104-
TraceWriter = ScriptConfig.TraceWriter.WithDefaults(ScriptConstants.TraceSourceScriptHost, _instanceId);
104+
TraceWriter = ScriptConfig.TraceWriter;
105105
EventManager = eventManager;
106106

107107
_settingsManager = settingsManager ?? ScriptSettingsManager.Instance;
@@ -558,7 +558,7 @@ private void PreInitialize()
558558
// taking this snapshot once and reusing at various points during initialization allows us to
559559
// minimize disk operations
560560
_directorySnapshot = Directory.EnumerateDirectories(ScriptConfig.RootScriptPath).ToImmutableArray();
561-
TraceWriter.Verbose("Created directory snapshot.");
561+
TraceWriter?.Verbose("Created directory snapshot.");
562562
}
563563

564564
/// <summary>
@@ -800,6 +800,11 @@ private void ConfigureLogging()
800800
.Filter(p => { return true; })
801801
.Subscribe(HandleHostError);
802802

803+
if (TraceWriter != null)
804+
{
805+
TraceWriter = TraceWriter.WithDefaults(ScriptConstants.TraceSourceScriptHost, _instanceId);
806+
}
807+
803808
_hostConfig.Tracing.Tracers.Add(_traceMonitor);
804809
var hostTraceLevel = _hostConfig.Tracing.ConsoleLevel;
805810
if (ScriptConfig.FileLoggingMode != FileLoggingMode.Never)

test/WebJobs.Script.Tests.Integration/Host/ScriptHostManagerTests.cs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,33 @@ await TestHelpers.Await(() =>
484484
Assert.Equal(ScriptHostState.Running, hostManager.State);
485485
}
486486

487+
[Fact]
488+
public async Task RunAndBlock_SelfHost_Succeeds()
489+
{
490+
var traceWriter = new TestTraceWriter(TraceLevel.Verbose);
491+
ScriptHostConfiguration config = new ScriptHostConfiguration()
492+
{
493+
RootScriptPath = Environment.CurrentDirectory,
494+
TraceWriter = traceWriter,
495+
IsSelfHost = true
496+
};
497+
498+
TraceEvent[] traces = null;
499+
using (var manager = new ScriptHostManager(config, _settingsManager))
500+
{
501+
var tIgnore = Task.Run(() => manager.RunAndBlock());
502+
503+
await TestHelpers.Await(() =>
504+
{
505+
traces = traceWriter.GetTraces().ToArray();
506+
return manager.State == ScriptHostState.Error || traces.Any(p => p.Message.Contains("Job host started"));
507+
});
508+
509+
Assert.Equal(ScriptHostState.Running, manager.State);
510+
Assert.Equal(0, traces.Count(p => p.Level == TraceLevel.Error));
511+
}
512+
}
513+
487514
[Fact]
488515
public void IsHostHealthy_ReturnsExpectedResult()
489516
{
@@ -553,12 +580,13 @@ public async Task EmptyHost_StartsSuccessfully()
553580
};
554581
File.WriteAllText(Path.Combine(functionDir, ScriptConstants.HostMetadataFileName), hostConfig.ToString());
555582

583+
var testTraceWriter = new TestTraceWriter(TraceLevel.Info);
556584
ScriptHostConfiguration config = new ScriptHostConfiguration()
557585
{
558586
RootScriptPath = functionDir,
559587
RootLogPath = logDir,
560588
FileLoggingMode = FileLoggingMode.Always,
561-
TraceWriter = new TestTraceWriter(TraceLevel.Info)
589+
TraceWriter = testTraceWriter
562590
};
563591

564592
var eventManagerMock = new Mock<IScriptEventManager>();
@@ -577,15 +605,11 @@ public async Task EmptyHost_StartsSuccessfully()
577605
hostManager.Stop();
578606
Assert.Equal(ScriptHostState.Default, hostManager.State);
579607

580-
await Task.Delay(FileTraceWriter.LogFlushIntervalMs);
581-
582-
string hostLogFilePath = Directory.EnumerateFiles(Path.Combine(logDir, "Host")).Single();
583-
string hostLogs = File.ReadAllText(hostLogFilePath);
584-
585-
Assert.Contains("Generating 0 job function(s)", hostLogs);
586-
Assert.Contains("No job functions found.", hostLogs);
587-
Assert.Contains("Job host started", hostLogs);
588-
Assert.Contains("Job host stopped", hostLogs);
608+
var logs = testTraceWriter.GetTraces().Select(p => p.Message).ToArray();
609+
Assert.True(logs.Any(p => p == "Generating 0 job function(s)"));
610+
Assert.True(logs.Any(p => p.StartsWith("No job functions found.")));
611+
Assert.True(logs.Any(p => p == "Job host started"));
612+
Assert.True(logs.Any(p => p == "Job host stopped"));
589613
}
590614

591615
[Fact]

test/WebJobs.Script.Tests.Integration/Host/WebScriptHostManagerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ public Fixture()
395395
};
396396
foreach (string pattern in expectedPatterns)
397397
{
398-
Assert.True(EventGenerator.Events.Any(p => Regex.IsMatch(p, pattern)), $"Expected trace event {pattern} not found.");
398+
Assert.True(EventGenerator.Events.Any(p => Regex.IsMatch(p, pattern)), $"Expected trace event '{pattern}' not found.");
399399
}
400400
}
401401

0 commit comments

Comments
 (0)