Skip to content

Commit 70d50e7

Browse files
committed
Reduce debounce time when app_offline.htm is created
1 parent 4f0f1e3 commit 70d50e7

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

src/WebJobs.Script.WebHost/FileMonitoringService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,15 @@ private void OnFileChanged(FileSystemEventArgs e)
144144
{
145145
// app_offline.htm has changed
146146
// when app_offline.htm is created, we trigger
147-
// a shutdown so when the host starts back up it
148-
// will be offline
147+
// a shutdown right away so when the host
148+
// starts back up it will be offline
149149
// when app_offline.htm is deleted, we trigger
150150
// a restart to bring the host back online
151151
changeDescription = "File";
152152
if (File.Exists(e.FullPath))
153153
{
154-
shutdown = true;
154+
TraceFileChangeRestart(changeDescription, e.ChangeType.ToString(), e.FullPath, isShutdown: true);
155+
Shutdown();
155156
}
156157
}
157158
else if (string.Compare(fileName, ScriptConstants.HostMetadataFileName, StringComparison.OrdinalIgnoreCase) == 0 ||

src/WebJobs.Script/Extensions/ActionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static Action<T> Debounce<T>(this Action<T> func, int milliseconds = 300)
2222
if (current == last)
2323
{
2424
// Only proceeed with the operation if there have been no
25-
// more events withing the specified time window (i.e. there
25+
// more events within the specified time window (i.e. there
2626
// is a quiet period)
2727
func(arg);
2828
}

test/WebJobs.Script.Tests/FileMonitoringServiceTests.cs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Text;
6+
using System.IO;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using Microsoft.Azure.WebJobs.Script.Eventing;
710
using Microsoft.Azure.WebJobs.Script.WebHost;
11+
using Microsoft.Extensions.Logging;
12+
using Microsoft.Extensions.Options;
13+
using Moq;
14+
using WebJobs.Script.Tests;
815
using Xunit;
916

1017
namespace Microsoft.Azure.WebJobs.Script.Tests
@@ -19,5 +26,66 @@ public static void GetRelativeDirectory_ReturnsExpectedDirectoryName(string path
1926
{
2027
Assert.Equal(expected, FileMonitoringService.GetRelativeDirectory(path, @"C:\Functions\Scripts"));
2128
}
29+
30+
[Theory]
31+
[InlineData("app_offline.htm", 150, true, false)]
32+
[InlineData("app_offline.htm", 10, true, false)]
33+
[InlineData("host.json", 0, false, false)]
34+
[InlineData("host.json", 200, false, false)]
35+
[InlineData("host.json", 1000, false, true)]
36+
public static async Task TestAppOfflineDebounceTime(string fileName, int delayInMs, bool expectShutdown, bool expectRestart)
37+
{
38+
using (var directory = new TempDirectory())
39+
{
40+
// Setup
41+
string tempDir = directory.Path;
42+
Directory.CreateDirectory(Path.Combine(tempDir, "Host"));
43+
File.Create(Path.Combine(tempDir, fileName));
44+
45+
var jobHostOptions = new ScriptJobHostOptions
46+
{
47+
RootLogPath = tempDir,
48+
RootScriptPath = tempDir,
49+
FileWatchingEnabled = true
50+
};
51+
var loggerFactory = new LoggerFactory();
52+
var mockWebHostEnvironment = new Mock<IScriptJobHostEnvironment>(MockBehavior.Loose);
53+
var mockEventManager = new ScriptEventManager();
54+
55+
// Act
56+
FileMonitoringService fileMonitoringService = new FileMonitoringService(new OptionsWrapper<ScriptJobHostOptions>(jobHostOptions),
57+
loggerFactory, mockEventManager, mockWebHostEnvironment.Object);
58+
await fileMonitoringService.StartAsync(new CancellationToken(canceled: false));
59+
60+
var offlineEventArgs = new FileSystemEventArgs(WatcherChangeTypes.Created, tempDir, fileName);
61+
FileEvent offlinefileEvent = new FileEvent("ScriptFiles", offlineEventArgs);
62+
63+
var randomFileEventArgs = new FileSystemEventArgs(WatcherChangeTypes.Created, tempDir, "random.txt");
64+
FileEvent randomFileEvent = new FileEvent("ScriptFiles", randomFileEventArgs);
65+
66+
mockEventManager.Publish(offlinefileEvent);
67+
await Task.Delay(delayInMs);
68+
mockEventManager.Publish(randomFileEvent);
69+
70+
// Test
71+
if (expectShutdown)
72+
{
73+
mockWebHostEnvironment.Verify(m => m.Shutdown());
74+
}
75+
else
76+
{
77+
mockWebHostEnvironment.Verify(m => m.Shutdown(), Times.Never);
78+
}
79+
80+
if (expectRestart)
81+
{
82+
mockWebHostEnvironment.Verify(m => m.RestartHost());
83+
}
84+
else
85+
{
86+
mockWebHostEnvironment.Verify(m => m.RestartHost(), Times.Never);
87+
}
88+
}
89+
}
2290
}
2391
}

0 commit comments

Comments
 (0)