Skip to content

Commit

Permalink
feat: wait for seq to be available
Browse files Browse the repository at this point in the history
  • Loading branch information
CumpsD committed Jun 30, 2020
1 parent 1e7b780 commit c2f2888
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 3 deletions.
72 changes: 72 additions & 0 deletions src/FunctionalLiving.Api/Infrastructure/WaitFor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace FunctionalLiving.Api.Infrastructure
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

internal class WriteTo
{
public string Name { get; set; }

public Dictionary<string, string> Args { get; set; }

public WriteTo()
{
Args = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
}
}

internal static class WaitFor
{
public static async Task SeqToBecomeAvailable(
IConfiguration configuration,
CancellationToken token = default)
{
var writeTos = configuration
.GetSection("Serilog:WriteTo")
.Get<WriteTo[]>();

var seq = writeTos.SingleOrDefault(to => "Seq".Equals(to.Name, StringComparison.InvariantCultureIgnoreCase));
if (seq == null)
return;

using (var client = new HttpClient { BaseAddress = new Uri(seq.Args["ServerUrl"]) })
{
var exit = false;
while (!exit)
{
Console.WriteLine("Waiting for Seq to become available...");

try
{
using (var response = await client.GetAsync("/api", token))
{
if (!response.IsSuccessStatusCode)
{
await Task.Delay(TimeSpan.FromSeconds(1), token);
}
else
{
exit = true;
}
}
}
catch (Exception exception)
{
Console.WriteLine(
"Observed an exception while waiting for Seq to become available. Exception: {0}",
exception);

await Task.Delay(TimeSpan.FromSeconds(1), token);
}
}
}

Console.WriteLine("Seq became available.");
}
}
}
72 changes: 72 additions & 0 deletions src/FunctionalLiving.Knx.Listener/Infrastructure/WaitFor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace FunctionalLiving.Knx.Listener.Infrastructure
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

internal class WriteTo
{
public string Name { get; set; }

public Dictionary<string, string> Args { get; set; }

public WriteTo()
{
Args = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
}
}

internal static class WaitFor
{
public static async Task SeqToBecomeAvailable(
IConfiguration configuration,
CancellationToken token = default)
{
var writeTos = configuration
.GetSection("Serilog:WriteTo")
.Get<WriteTo[]>();

var seq = writeTos.SingleOrDefault(to => "Seq".Equals(to.Name, StringComparison.InvariantCultureIgnoreCase));
if (seq == null)
return;

using (var client = new HttpClient { BaseAddress = new Uri(seq.Args["ServerUrl"]) })
{
var exit = false;
while (!exit)
{
Console.WriteLine("Waiting for Seq to become available...");

try
{
using (var response = await client.GetAsync("/api", token))
{
if (!response.IsSuccessStatusCode)
{
await Task.Delay(TimeSpan.FromSeconds(1), token);
}
else
{
exit = true;
}
}
}
catch (Exception exception)
{
Console.WriteLine(
"Observed an exception while waiting for Seq to become available. Exception: {0}",
exception);

await Task.Delay(TimeSpan.FromSeconds(1), token);
}
}
}

Console.WriteLine("Seq became available.");
}
}
}
8 changes: 5 additions & 3 deletions src/FunctionalLiving.Knx.Listener/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace FunctionalLiving.Knx.Listener
{
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using FunctionalLiving.Knx.Listener.Infrastructure;
using Infrastructure.Modules;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -51,10 +51,12 @@ public static async Task Main(string[] args)
var openTelemetry = container.GetService<TracerFactoryBase>();
var logger = container.GetRequiredService<ILogger<Program>>();

logger.LogInformation("Starting FunctionalLiving.Knx.Listener");

try
{
await WaitFor.SeqToBecomeAvailable(configuration).ConfigureAwait(false);

logger.LogInformation("Starting FunctionalLiving.Knx.Listener");

var runner = container.GetRequiredService<KnxListener>();

runner.Start();
Expand Down
72 changes: 72 additions & 0 deletions src/FunctionalLiving.Knx.Sender/Infrastructure/WaitFor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace FunctionalLiving.Knx.Sender.Infrastructure
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

internal class WriteTo
{
public string Name { get; set; }

public Dictionary<string, string> Args { get; set; }

public WriteTo()
{
Args = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
}
}

internal static class WaitFor
{
public static async Task SeqToBecomeAvailable(
IConfiguration configuration,
CancellationToken token = default)
{
var writeTos = configuration
.GetSection("Serilog:WriteTo")
.Get<WriteTo[]>();

var seq = writeTos.SingleOrDefault(to => "Seq".Equals(to.Name, StringComparison.InvariantCultureIgnoreCase));
if (seq == null)
return;

using (var client = new HttpClient { BaseAddress = new Uri(seq.Args["ServerUrl"]) })
{
var exit = false;
while (!exit)
{
Console.WriteLine("Waiting for Seq to become available...");

try
{
using (var response = await client.GetAsync("/api", token))
{
if (!response.IsSuccessStatusCode)
{
await Task.Delay(TimeSpan.FromSeconds(1), token);
}
else
{
exit = true;
}
}
}
catch (Exception exception)
{
Console.WriteLine(
"Observed an exception while waiting for Seq to become available. Exception: {0}",
exception);

await Task.Delay(TimeSpan.FromSeconds(1), token);
}
}
}

Console.WriteLine("Seq became available.");
}
}
}

0 comments on commit c2f2888

Please sign in to comment.