Skip to content

Commit

Permalink
Fix FileSystemWatcher (#733)
Browse files Browse the repository at this point in the history
* Define EnhancedFileSystemWatcher in class

* d
  • Loading branch information
StefH authored Mar 1, 2022
1 parent 344f5c8 commit 6c68033
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 36 deletions.
3 changes: 3 additions & 0 deletions examples/WireMock.Net.Console.Net452.Classic/MainApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@ public static void Run()

System.Console.WriteLine("Press any key to quit");
System.Console.ReadKey();

server.Stop();
server.Dispose();
}
}
}
93 changes: 57 additions & 36 deletions src/WireMock.Net/Server/WireMockServer.Admin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace WireMock.Server
/// </summary>
public partial class WireMockServer
{
private const int EnhancedFileSystemWatcherTimeoutMs = 10000; // Changed from 1000 to 10000 (#726)
private const int EnhancedFileSystemWatcherTimeoutMs = 1000;
private const int AdminPriority = int.MinValue;
private const int ProxyPriority = 1000;
private const string ContentTypeJson = "application/json";
Expand All @@ -49,6 +49,8 @@ public partial class WireMockServer
private readonly RegexMatcher _adminMappingsGuidPathMatcher = new RegexMatcher(@"^\/__admin\/mappings\/([0-9A-Fa-f]{8}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{12})$");
private readonly RegexMatcher _adminRequestsGuidPathMatcher = new RegexMatcher(@"^\/__admin\/requests\/([0-9A-Fa-f]{8}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{12})$");

private EnhancedFileSystemWatcher _enhancedFileSystemWatcher;

#region InitAdmin
private void InitAdmin()
{
Expand Down Expand Up @@ -163,45 +165,17 @@ public void WatchStaticMappings([CanBeNull] string folder = null)

_settings.Logger.Info($"Watching folder '{folder}'{includeSubdirectoriesText} for new, updated and deleted MappingFiles.");

var watcher = new EnhancedFileSystemWatcher(folder, "*.json", EnhancedFileSystemWatcherTimeoutMs)
DisposeEnhancedFileSystemWatcher();
_enhancedFileSystemWatcher = new EnhancedFileSystemWatcher(folder, "*.json", EnhancedFileSystemWatcherTimeoutMs)
{
IncludeSubdirectories = includeSubdirectories
};

watcher.Created += (sender, args) =>
{
_settings.Logger.Info("MappingFile created : '{0}', reading file.", args.FullPath);
if (!ReadStaticMappingAndAddOrUpdate(args.FullPath))
{
_settings.Logger.Error("Unable to read MappingFile '{0}'.", args.FullPath);
}
};
watcher.Changed += (sender, args) =>
{
_settings.Logger.Info("MappingFile updated : '{0}', reading file.", args.FullPath);
if (!ReadStaticMappingAndAddOrUpdate(args.FullPath))
{
_settings.Logger.Error("Unable to read MappingFile '{0}'.", args.FullPath);
}
};
watcher.Deleted += (sender, args) =>
{
_settings.Logger.Info("MappingFile deleted : '{0}'", args.FullPath);
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(args.FullPath);

if (Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
{
DeleteMapping(guidFromFilename);
}
else
{
DeleteMapping(args.FullPath);
}
};

watcher.EnableRaisingEvents = true;
_enhancedFileSystemWatcher.Created += EnhancedFileSystemWatcherCreated;
_enhancedFileSystemWatcher.Changed += EnhancedFileSystemWatcherChanged;
_enhancedFileSystemWatcher.Deleted += EnhancedFileSystemWatcherDeleted;
_enhancedFileSystemWatcher.EnableRaisingEvents = true;
}

/// <inheritdoc cref="IWireMockServer.WatchStaticMappings" />
[PublicAPI]
public bool ReadStaticMappingAndAddOrUpdate([NotNull] string path)
Expand Down Expand Up @@ -940,5 +914,52 @@ private T[] DeserializeJsonToArray<T>(string value)
{
return DeserializeObjectToArray<T>(JsonUtils.DeserializeObject(value));
}

private void DisposeEnhancedFileSystemWatcher()
{
if (_enhancedFileSystemWatcher != null)
{
_enhancedFileSystemWatcher.EnableRaisingEvents = false;

_enhancedFileSystemWatcher.Created -= EnhancedFileSystemWatcherCreated;
_enhancedFileSystemWatcher.Changed -= EnhancedFileSystemWatcherChanged;
_enhancedFileSystemWatcher.Deleted -= EnhancedFileSystemWatcherDeleted;

_enhancedFileSystemWatcher.Dispose();
}
}

private void EnhancedFileSystemWatcherCreated(object sender, FileSystemEventArgs args)
{
_settings.Logger.Info("MappingFile created : '{0}', reading file.", args.FullPath);
if (!ReadStaticMappingAndAddOrUpdate(args.FullPath))
{
_settings.Logger.Error("Unable to read MappingFile '{0}'.", args.FullPath);
}
}

private void EnhancedFileSystemWatcherChanged(object sender, FileSystemEventArgs args)
{
_settings.Logger.Info("MappingFile updated : '{0}', reading file.", args.FullPath);
if (!ReadStaticMappingAndAddOrUpdate(args.FullPath))
{
_settings.Logger.Error("Unable to read MappingFile '{0}'.", args.FullPath);
}
}

private void EnhancedFileSystemWatcherDeleted(object sender, FileSystemEventArgs args)
{
_settings.Logger.Info("MappingFile deleted : '{0}'", args.FullPath);
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(args.FullPath);

if (Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
{
DeleteMapping(guidFromFilename);
}
else
{
DeleteMapping(args.FullPath);
}
}
}
}
1 change: 1 addition & 0 deletions src/WireMock.Net/Server/WireMockServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public void Dispose()
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
DisposeEnhancedFileSystemWatcher();
_httpServer?.StopAsync();
}
#endregion
Expand Down

0 comments on commit 6c68033

Please sign in to comment.