Skip to content

Commit

Permalink
asyncapi#196 GetStreamFor try recreate directory?
Browse files Browse the repository at this point in the history
  • Loading branch information
Senn Geerts authored and Senn Geerts committed Jul 13, 2024
1 parent e846dd9 commit fc7cccd
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/AsyncAPI.Saunter.Generator.Cli/ToFile/StreamProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using Microsoft.Extensions.Logging;

namespace AsyncAPI.Saunter.Generator.Cli.ToFile;

Expand All @@ -15,7 +16,21 @@ public Stream GetStreamFor(string path)

if (!string.IsNullOrEmpty(path))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
var directory = Path.GetDirectoryName(path);
var sw = Stopwatch.StartNew();
do
{
try
{
Directory.CreateDirectory(directory);
}
catch (Exception e) when (sw.Elapsed < TimeSpan.FromMilliseconds(250))
{
logger.LogDebug(e, "Retry...");
Thread.Sleep(100);
}
}
while (!Directory.Exists(directory));
}

return path != null ? File.Create(path) : Console.OpenStandardOutput();
Expand Down

0 comments on commit fc7cccd

Please sign in to comment.