Skip to content

Commit c4625e1

Browse files
authored
Enhance tracing on file change path. Fixes #1898
1 parent 8ff078d commit c4625e1

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/WebJobs.Script/Host/ScriptHost.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -620,9 +620,9 @@ internal static void ConfigureLoggerFactory(ScriptHostConfiguration scriptConfig
620620
scriptConfig.LoggerFactoryBuilder.AddLoggerProviders(scriptConfig.HostConfig.LoggerFactory, scriptConfig, settingsManager);
621621
}
622622

623-
private void TraceFileChangeRestart(string changeType, string path, bool isShutdown)
623+
private void TraceFileChangeRestart(string changeDescription, string changeType, string path, bool isShutdown)
624624
{
625-
string fileChangeMsg = string.Format(CultureInfo.InvariantCulture, "File change of type '{0}' detected for '{1}'", changeType, path);
625+
string fileChangeMsg = string.Format(CultureInfo.InvariantCulture, "{0} change of type '{1}' detected for '{2}'", changeDescription, changeType, path);
626626
TraceWriter.Info(fileChangeMsg);
627627
Logger?.LogInformation(fileChangeMsg);
628628

@@ -1619,40 +1619,45 @@ private void NotifyInvoker(string functionName, Exception ex)
16191619

16201620
private void OnFileChanged(FileSystemEventArgs e)
16211621
{
1622-
string directory = GetRelativeDirectory(e.FullPath, ScriptConfig.RootScriptPath);
1623-
bool isWatchedDirectory = ScriptConfig.WatchDirectories.Contains(directory);
1624-
16251622
// We will perform a host restart in the following cases:
16261623
// - the file change was under one of the configured watched directories (e.g. node_modules, shared code directories, etc.)
16271624
// - the host.json file was changed
16281625
// - a function.json file was changed
1626+
// - a proxies.json file was changed
16291627
// - a function directory was added/removed/renamed
16301628
// A full host shutdown is performed when an assembly (.dll, .exe) in a watched directory is modified
1629+
1630+
string changeDescription = string.Empty;
1631+
string directory = GetRelativeDirectory(e.FullPath, ScriptConfig.RootScriptPath);
16311632
string fileName = Path.GetFileName(e.Name);
1632-
if (isWatchedDirectory ||
1633-
(string.Compare(fileName, ScriptConstants.HostMetadataFileName, StringComparison.OrdinalIgnoreCase) == 0 ||
1634-
string.Compare(fileName, ScriptConstants.FunctionMetadataFileName, StringComparison.OrdinalIgnoreCase) == 0 ||
1635-
string.Compare(fileName, ScriptConstants.ProxyMetadataFileName, StringComparison.OrdinalIgnoreCase) == 0) ||
1636-
!_directorySnapshot.SequenceEqual(Directory.EnumerateDirectories(ScriptConfig.RootScriptPath)))
1637-
{
1638-
// CRI ICM: 46695121
1639-
// Do not allow the host to restart if this notification is for the hostingstart.html file in the root
1640-
if (string.Compare(directory, "hostingstart.html", StringComparison.OrdinalIgnoreCase) == 0)
1641-
{
1642-
string hostingWarning = string.Format(CultureInfo.InvariantCulture, "Unexpected file change event detected for '{0}' which evaluated to a host restart. Suppressing restart.", e.FullPath);
1643-
TraceWriter.Warning(hostingWarning);
1644-
Logger?.LogWarning(hostingWarning);
1645-
return;
1646-
}
16471633

1634+
if (ScriptConfig.WatchDirectories.Contains(directory))
1635+
{
1636+
changeDescription = "Watched directory";
1637+
}
1638+
else if (string.Compare(fileName, ScriptConstants.HostMetadataFileName, StringComparison.OrdinalIgnoreCase) == 0 ||
1639+
string.Compare(fileName, ScriptConstants.FunctionMetadataFileName, StringComparison.OrdinalIgnoreCase) == 0 ||
1640+
string.Compare(fileName, ScriptConstants.ProxyMetadataFileName, StringComparison.OrdinalIgnoreCase) == 0)
1641+
{
1642+
changeDescription = "File";
1643+
}
1644+
else if ((e.ChangeType == WatcherChangeTypes.Deleted || Directory.Exists(e.FullPath))
1645+
&& !_directorySnapshot.SequenceEqual(Directory.EnumerateDirectories(ScriptConfig.RootScriptPath)))
1646+
{
1647+
// Check directory spashot only if "Deleted" change or if directory changed
1648+
changeDescription = "Directory";
1649+
}
1650+
1651+
if (!string.IsNullOrEmpty(changeDescription))
1652+
{
16481653
bool shutdown = false;
16491654
string fileExtension = Path.GetExtension(fileName);
16501655
if (!string.IsNullOrEmpty(fileExtension) && ScriptConstants.AssemblyFileTypes.Contains(fileExtension, StringComparer.OrdinalIgnoreCase))
16511656
{
16521657
shutdown = true;
16531658
}
16541659

1655-
TraceFileChangeRestart(e.ChangeType.ToString(), e.FullPath, shutdown);
1660+
TraceFileChangeRestart(changeDescription, e.ChangeType.ToString(), e.FullPath, shutdown);
16561661
ScheduleRestartAsync(shutdown).ContinueWith(t => TraceWriter.Error($"Error restarting host (full shutdown: {shutdown})", t.Exception),
16571662
TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted);
16581663
}

0 commit comments

Comments
 (0)