Skip to content

Commit 0f46245

Browse files
cosminpopescujoelverhagen
authored andcommitted
Normalize directories before comparing paths in file changed event in order to avoid erroneous cache invalidation in a multi-node deploymeny (#54)
* Normalize nuget server path before comparing root paths. * Normalize nuget server path before comparing root paths. PR review changes.
1 parent 4aad11b commit 0f46245

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

src/NuGet.Server.Core/Infrastructure/ServerPackageRepository.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class ServerPackageRepository
3232

3333
private readonly bool _runBackgroundTasks;
3434
private FileSystemWatcher _fileSystemWatcher;
35+
private string _watchDirectory;
3536
private bool _isFileSystemWatcherSuppressed;
3637
private bool _needsRebuild;
3738

@@ -548,6 +549,9 @@ private void RegisterFileSystemWatcher()
548549
IncludeSubdirectories = true,
549550
};
550551

552+
//Keep the normalized watch path.
553+
_watchDirectory = Path.GetFullPath(_fileSystemWatcher.Path);
554+
551555
_fileSystemWatcher.Changed += FileSystemChangedAsync;
552556
_fileSystemWatcher.Created += FileSystemChangedAsync;
553557
_fileSystemWatcher.Deleted += FileSystemChangedAsync;
@@ -576,6 +580,8 @@ private void UnregisterFileSystemWatcher()
576580

577581
_logger.Log(LogLevel.Verbose, "Destroyed FileSystemWatcher - no longer monitoring {0}.", Source);
578582
}
583+
584+
_watchDirectory = null;
579585
}
580586

581587

@@ -593,16 +599,24 @@ private async void FileSystemChangedAsync(object sender, FileSystemEventArgs e)
593599

594600
_logger.Log(LogLevel.Verbose, "File system changed. File: {0} - Change: {1}", e.Name, e.ChangeType);
595601

602+
var changedDirectory = Path.GetDirectoryName(e.FullPath);
603+
if (changedDirectory == null || _watchDirectory == null)
604+
{
605+
return;
606+
}
607+
608+
changedDirectory = Path.GetFullPath(changedDirectory);
609+
596610
// 1) If a .nupkg is dropped in the root, add it as a package
597-
if (string.Equals(Path.GetDirectoryName(e.FullPath), _fileSystemWatcher.Path, StringComparison.OrdinalIgnoreCase)
611+
if (string.Equals(changedDirectory, _watchDirectory, StringComparison.OrdinalIgnoreCase)
598612
&& string.Equals(Path.GetExtension(e.Name), ".nupkg", StringComparison.OrdinalIgnoreCase))
599613
{
600614
// When a package is dropped into the server packages root folder, add it to the repository.
601615
await AddPackagesFromDropFolderAsync(CancellationToken.None);
602616
}
603617

604618
// 2) If a file is updated in a subdirectory, *or* a folder is deleted, invalidate the cache
605-
if ((!string.Equals(Path.GetDirectoryName(e.FullPath), _fileSystemWatcher.Path, StringComparison.OrdinalIgnoreCase) && File.Exists(e.FullPath))
619+
if ((!string.Equals(changedDirectory, _watchDirectory, StringComparison.OrdinalIgnoreCase) && File.Exists(e.FullPath))
606620
|| e.ChangeType == WatcherChangeTypes.Deleted)
607621
{
608622
// TODO: invalidating *all* packages for every nupkg change under this folder seems more expensive than it should.

0 commit comments

Comments
 (0)