-
Notifications
You must be signed in to change notification settings - Fork 441
/
FileMonitoringService.cs
438 lines (384 loc) · 17.4 KB
/
FileMonitoringService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Logging;
using Microsoft.Azure.WebJobs.Script.Eventing;
using Microsoft.Azure.WebJobs.Script.Eventing.File;
using Microsoft.Azure.WebJobs.Script.IO;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using IApplicationLifetime = Microsoft.AspNetCore.Hosting.IApplicationLifetime;
namespace Microsoft.Azure.WebJobs.Script.WebHost
{
public class FileMonitoringService : IFileMonitoringService, IDisposable
{
private readonly ScriptJobHostOptions _scriptOptions;
private readonly IScriptEventManager _eventManager;
private readonly IApplicationLifetime _applicationLifetime;
private readonly IScriptHostManager _scriptHostManager;
private readonly IEnvironment _environment;
private readonly string _hostLogPath;
private readonly ILogger _logger;
private readonly ILogger<FileMonitoringService> _typedLogger;
private readonly IList<IDisposable> _eventSubscriptions = new List<IDisposable>();
private readonly Func<Task> _restart;
private readonly Action _shutdown;
private readonly ImmutableArray<string> _rootDirectorySnapshot;
private AutoRecoveringFileSystemWatcher _debugModeFileWatcher;
private AutoRecoveringFileSystemWatcher _diagnosticModeFileWatcher;
private FileWatcherEventSource _fileEventSource;
private bool _restartScheduled;
private bool _shutdownScheduled;
private long _restartRequested;
private bool _disposed = false;
private bool _watchersStopped = false;
private object _stopWatchersLock = new object();
private long _suspensionRequestsCount = 0;
public FileMonitoringService(IOptions<ScriptJobHostOptions> scriptOptions, ILoggerFactory loggerFactory, IScriptEventManager eventManager, IApplicationLifetime applicationLifetime, IScriptHostManager scriptHostManager, IEnvironment environment)
{
_scriptOptions = scriptOptions.Value;
_eventManager = eventManager;
_applicationLifetime = applicationLifetime;
_scriptHostManager = scriptHostManager;
_hostLogPath = Path.Combine(_scriptOptions.RootLogPath, "Host");
_logger = loggerFactory.CreateLogger(LogCategories.Startup);
_environment = environment;
// Use this for newer logs as we can't change existing categories of log messages
_typedLogger = loggerFactory.CreateLogger<FileMonitoringService>();
// If a file change should result in a restart, we debounce the event to
// ensure that only a single restart is triggered within a specific time window.
// This allows us to deal with a large set of file change events that might
// result from a bulk copy/unzip operation. In such cases, we only want to
// restart after ALL the operations are complete and there is a quiet period.
_restart = RestartAsync;
_restart = _restart.Debounce(500);
_shutdown = Shutdown;
_shutdown = _shutdown.Debounce(milliseconds: 500);
_rootDirectorySnapshot = GetDirectorySnapshot();
}
internal ImmutableArray<string> GetDirectorySnapshot()
{
if (_scriptOptions.RootScriptPath != null)
{
try
{
return Directory.EnumerateDirectories(_scriptOptions.RootScriptPath).ToImmutableArray();
}
catch (DirectoryNotFoundException)
{
_logger.LogInformation($"Unable to get directory snapshot. No directory present at {_scriptOptions.RootScriptPath}");
}
}
return ImmutableArray<string>.Empty;
}
public Task StartAsync(CancellationToken cancellationToken)
{
InitializeFileWatchers();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
StopFileWatchers();
return Task.CompletedTask;
}
public IDisposable SuspendRestart(bool autoRestart)
{
return new SuspendRestartRequest(this, autoRestart);
}
private void ResumeRestartIfScheduled()
{
if (_restartScheduled)
{
using (System.Threading.ExecutionContext.SuppressFlow())
{
_typedLogger.LogDebug("Resuming scheduled restart.");
Task.Run(async () => await ScheduleRestartAsync());
}
}
}
private async Task ScheduleRestartAsync(bool shutdown)
{
_restartScheduled = true;
if (shutdown)
{
_shutdownScheduled = true;
}
await ScheduleRestartAsync();
}
private async Task ScheduleRestartAsync()
{
if (Interlocked.Read(ref _suspensionRequestsCount) > 0)
{
_logger.LogDebug("Restart requested while currently suspended. Ignoring request.");
}
else
{
if (_shutdownScheduled)
{
_shutdown();
}
else
{
await _restart();
}
}
}
/// <summary>
/// Initialize file and directory change monitoring.
/// </summary>
private void InitializeFileWatchers()
{
if (_scriptOptions.FileWatchingEnabled)
{
_fileEventSource = new FileWatcherEventSource(_eventManager, EventSources.ScriptFiles, _scriptOptions.RootScriptPath);
_eventSubscriptions.Add(_eventManager.OfType<FileEvent>()
.Where(f => string.Equals(f.Source, EventSources.ScriptFiles, StringComparison.Ordinal))
.Subscribe(e => OnFileChanged(e.FileChangeArguments)));
_logger.LogDebug("File event source initialized.");
}
_eventSubscriptions.Add(_eventManager.OfType<HostRestartEvent>()
.Subscribe((msg) => ScheduleRestartAsync(false)
.ContinueWith(t => _logger.LogCritical(t.Exception.Message),
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously)));
// Delay starting up for logging and debug file watchers to avoid long start up times
Utility.ExecuteAfterColdStartDelay(_environment, InitializeSecondaryFileWatchers);
}
/// <summary>
/// Initializes the file and directory monitoring that does not need to happen as part of a Host startup
/// These watchers can be started after a delay to avoid startup performance issue
/// </summary>
private void InitializeSecondaryFileWatchers()
{
if (_watchersStopped)
{
return;
}
lock (_stopWatchersLock)
{
if (!_watchersStopped)
{
FileUtility.EnsureDirectoryExists(_hostLogPath);
_debugModeFileWatcher = new AutoRecoveringFileSystemWatcher(_hostLogPath, ScriptConstants.DebugSentinelFileName,
includeSubdirectories: false, changeTypes: WatcherChangeTypes.Created | WatcherChangeTypes.Changed);
_debugModeFileWatcher.Changed += OnDebugModeFileChanged;
_logger.LogDebug("Debug file watch initialized.");
_diagnosticModeFileWatcher = new AutoRecoveringFileSystemWatcher(_hostLogPath, ScriptConstants.DiagnosticSentinelFileName,
includeSubdirectories: false, changeTypes: WatcherChangeTypes.Created | WatcherChangeTypes.Changed);
_diagnosticModeFileWatcher.Changed += OnDiagnosticModeFileChanged;
_logger.LogDebug("Diagnostic file watch initialized.");
}
}
}
private void StopFileWatchers()
{
if (_watchersStopped)
{
return;
}
lock (_stopWatchersLock)
{
if (_watchersStopped)
{
return;
}
_typedLogger.LogDebug("Stopping file watchers.");
_fileEventSource?.Dispose();
if (_debugModeFileWatcher != null)
{
_debugModeFileWatcher.Changed -= OnDebugModeFileChanged;
_debugModeFileWatcher.Dispose();
}
if (_diagnosticModeFileWatcher != null)
{
_diagnosticModeFileWatcher.Changed -= OnDiagnosticModeFileChanged;
_diagnosticModeFileWatcher.Dispose();
}
foreach (var subscription in _eventSubscriptions)
{
subscription.Dispose();
}
_watchersStopped = true;
}
}
/// <summary>
/// Whenever the debug sentinel file changes we update our debug timeout
/// </summary>
private void OnDebugModeFileChanged(object sender, FileSystemEventArgs e)
{
if (!_disposed)
{
_eventManager.Publish(new DebugNotification(nameof(FileMonitoringService), DateTime.UtcNow));
}
}
/// <summary>
/// Whenever the diagnostic sentinel file changes we update our debug timeout
/// </summary>
private void OnDiagnosticModeFileChanged(object sender, FileSystemEventArgs e)
{
if (!_disposed)
{
_eventManager.Publish(new DiagnosticNotification(nameof(FileMonitoringService), DateTime.UtcNow));
}
}
private void OnFileChanged(FileSystemEventArgs e)
{
// We will perform a host restart in the following cases:
// - the file change was under one of the configured watched directories (e.g. node_modules, shared code directories, etc.)
// - the host.json file was changed
// - a function.json file was changed
// - a proxies.json file was changed
// - a function directory was added/removed/renamed
// A full host shutdown is performed when an assembly (.dll, .exe) in a watched directory is modified
string changeDescription = string.Empty;
string directory = GetRelativeDirectory(e.FullPath, _scriptOptions.RootScriptPath);
string fileName = Path.GetFileName(e.Name);
bool shutdown = false;
if (_scriptOptions.WatchDirectories.Contains(directory))
{
changeDescription = "Watched directory";
}
else if (string.Equals(fileName, ScriptConstants.AppOfflineFileName, StringComparison.OrdinalIgnoreCase))
{
// app_offline.htm has changed
// when app_offline.htm is created, we trigger
// a shutdown right away so when the host
// starts back up it will be offline
// when app_offline.htm is deleted, we trigger
// a restart to bring the host back online
changeDescription = "File";
if (File.Exists(e.FullPath))
{
TraceFileChangeRestart(changeDescription, e.ChangeType.ToString(), e.FullPath, isShutdown: true);
Shutdown();
}
}
else if (_scriptOptions.WatchFiles.Any(f => string.Equals(fileName, f, StringComparison.OrdinalIgnoreCase)))
{
changeDescription = "File";
}
else if ((e.ChangeType == WatcherChangeTypes.Deleted || Directory.Exists(e.FullPath))
&& !_rootDirectorySnapshot.SequenceEqual(Directory.EnumerateDirectories(_scriptOptions.RootScriptPath)))
{
// Check directory snapshot only if "Deleted" change or if directory changed
changeDescription = "Directory";
}
if (!string.IsNullOrEmpty(changeDescription))
{
string fileExtension = Path.GetExtension(fileName);
if (!string.IsNullOrEmpty(fileExtension) && ScriptConstants.AssemblyFileTypes.Contains(fileExtension, StringComparer.OrdinalIgnoreCase))
{
shutdown = true;
}
TraceFileChangeRestart(changeDescription, e.ChangeType.ToString(), e.FullPath, shutdown);
ScheduleRestartAsync(shutdown).ContinueWith(t => _logger.LogError(t.Exception, $"Error restarting host (full shutdown: {shutdown})"),
TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted);
}
}
private void TraceFileChangeRestart(string changeDescription, string changeType, string path, bool isShutdown)
{
string fileChangeMsg = string.Format(CultureInfo.InvariantCulture, "{0} change of type '{1}' detected for '{2}'", changeDescription, changeType, path);
_logger.LogInformation(fileChangeMsg);
string action = isShutdown ? "shutdown" : "restart";
string signalMessage = $"Host configuration has changed. Signaling {action}";
_logger.LogInformation(signalMessage);
}
internal static string GetRelativeDirectory(string path, string scriptRoot)
{
if (path.StartsWith(scriptRoot))
{
string directory = path.Substring(scriptRoot.Length).TrimStart(Path.DirectorySeparatorChar);
int idx = directory.IndexOf(Path.DirectorySeparatorChar);
if (idx != -1)
{
directory = directory.Substring(0, idx);
}
return directory;
}
return string.Empty;
}
private Task RestartAsync()
{
if (!_shutdownScheduled && Interlocked.Exchange(ref _restartRequested, 1) == 0)
{
return _scriptHostManager.RestartHostAsync();
}
return Task.CompletedTask;
}
private void Shutdown()
{
_applicationLifetime.StopApplication();
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
StopFileWatchers();
}
_disposed = true;
}
}
public void Dispose()
{
Dispose(true);
}
internal static async Task SetAppOfflineState(string rootPath, bool offline)
{
string path = Path.Combine(rootPath, ScriptConstants.AppOfflineFileName);
bool offlineFileExists = File.Exists(path);
if (offline && !offlineFileExists)
{
// create the app_offline.htm file in the root script directory
string content = FileUtility.ReadResourceString($"{ScriptConstants.ResourcePath}.{ScriptConstants.AppOfflineFileName}");
await FileUtility.WriteAsync(path, content);
}
else if (!offline && offlineFileExists)
{
// delete the app_offline.htm file
await Utility.InvokeWithRetriesAsync(() =>
{
if (File.Exists(path))
{
File.Delete(path);
}
}, maxRetries: 3, retryInterval: TimeSpan.FromSeconds(1));
}
}
private class SuspendRestartRequest : IDisposable
{
private FileMonitoringService _fileMonitoringService;
private bool _autoResume;
private bool _disposed = false;
public SuspendRestartRequest(FileMonitoringService fileMonitoringService, bool autoResume)
{
_fileMonitoringService = fileMonitoringService;
_autoResume = autoResume;
Interlocked.Increment(ref _fileMonitoringService._suspensionRequestsCount);
_fileMonitoringService._typedLogger.LogDebug($"Entering restart suspension scope. ({_fileMonitoringService._suspensionRequestsCount} requests).");
}
public void Dispose()
{
if (!_disposed)
{
Interlocked.Decrement(ref _fileMonitoringService._suspensionRequestsCount);
_fileMonitoringService._typedLogger.LogDebug($"Exiting restart suspension scope. ({_fileMonitoringService._suspensionRequestsCount} requests).");
if (_autoResume)
{
_fileMonitoringService.ResumeRestartIfScheduled();
}
_disposed = true;
}
}
}
}
}