Skip to content

Commit

Permalink
Avoid crashing when file is being used by another process
Browse files Browse the repository at this point in the history
Small refactor
  • Loading branch information
natenho committed Aug 1, 2019
1 parent 30db9cb commit 820fbc6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Mockaco/Mockaco.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.7.11" />
<PackageReference Include="Polly" Version="7.1.0" />
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
Expand Down
32 changes: 27 additions & 5 deletions src/Mockaco/Templating/Providers/TemplateFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Polly;
using Polly.Retry;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -14,6 +16,18 @@ public sealed class TemplateFileProvider : ITemplateProvider, IDisposable
{
public event EventHandler OnChange;

private const string DefaultTemplateFolder = "Mocks";
private const string DefaultTemplateSearchPattern = "*.json";

private static readonly RetryPolicy _retryPolicy = Policy
.Handle<IOException>()
.WaitAndRetry(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2),
TimeSpan.FromSeconds(3)
});

private readonly ILogger<TemplateFileProvider> _logger;
private readonly PhysicalFileProvider _fileProvider;
private readonly IMemoryCache _memoryCache;
Expand All @@ -26,12 +40,12 @@ public TemplateFileProvider(IMemoryCache memoryCache, ILogger<TemplateFileProvid
_fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
_logger = logger;

KeepWatchingForFileChanges();
KeepWatchingForFileChanges();
}

private void KeepWatchingForFileChanges()
{
_fileChangeToken = _fileProvider.Watch("Mocks/**/*.json");
_fileChangeToken = _fileProvider.Watch($"{DefaultTemplateFolder}/**/{DefaultTemplateSearchPattern}");
_fileChangeToken.RegisterChangeCallback(TemplateFileModified, default);
}

Expand Down Expand Up @@ -67,14 +81,22 @@ private void PostEvictionCallback(object key, object value, EvictionReason reaso

private static IEnumerable<IRawTemplate> LoadTemplatesFromDirectory()
{
var directory = new DirectoryInfo("Mocks");
var directory = new DirectoryInfo(DefaultTemplateFolder);

foreach (var file in directory.GetFiles("*.json", SearchOption.AllDirectories)
foreach (var file in directory.GetFiles(DefaultTemplateSearchPattern, SearchOption.AllDirectories)
.OrderBy(f => f.FullName))
{
var name = Path.GetRelativePath(directory.FullName, file.FullName);
var rawContent = string.Empty;

var rawContent = File.ReadAllText(file.FullName);
_retryPolicy.Execute(() =>
{
using (var stream = File.Open(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var streamReader = new StreamReader(stream))
{
rawContent = streamReader.ReadToEnd();
}
});

yield return new RawTemplate(name, rawContent);
}
Expand Down

0 comments on commit 820fbc6

Please sign in to comment.