Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broadcast logs #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Queil.Ring.Client.FSharp/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,36 @@
"resolved": "8.0.101",
"contentHash": "sOLz3O4BOxnTKfd5OChdRmDUy4Id0GfoEClRG4nzIod8LY1LJZcNyygKAV0A78XOLh8yvhA5hsDYKZXGCR9blw=="
},
"MessagePack": {
"type": "Transitive",
"resolved": "2.5.140",
"contentHash": "nkIsgy8BkIfv40bSz9XZb4q//scI1PF3AYeB5X66nSlIhBIqbdpLz8Qk3gHvnjV3RZglQLO/ityK3eNfLii2NA==",
"dependencies": {
"MessagePack.Annotations": "2.5.140",
"Microsoft.NET.StringTools": "17.6.3",
"System.Runtime.CompilerServices.Unsafe": "6.0.0"
}
},
"MessagePack.Annotations": {
"type": "Transitive",
"resolved": "2.5.140",
"contentHash": "JE3vwluOrsJ4t3hnfXzIxJUh6lhx6M/KR8Sark/HOUw1DJ5UKu5JsAnnuaQngg6poFkRx1lzHSLTkxHNJO7+uQ=="
},
"Microsoft.Bcl.AsyncInterfaces": {
"type": "Transitive",
"resolved": "5.0.0",
"contentHash": "W8DPQjkMScOMTtJbPwmPyj9c3zYSFGawDW3jwlBOOsnY+EzZFLgNQ/UMkK35JmkNOVPdCyPr2Tw7Vv9N+KA3ZQ=="
},
"Microsoft.NET.StringTools": {
"type": "Transitive",
"resolved": "17.6.3",
"contentHash": "N0ZIanl1QCgvUumEL1laasU0a7sOE5ZwLZVTn0pAePnfhq8P7SvTjF8Axq+CnavuQkmdQpGNXQ1efZtu5kDFbA=="
},
"System.Runtime.CompilerServices.Unsafe": {
"type": "Transitive",
"resolved": "6.0.0",
"contentHash": "/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg=="
},
"System.Text.Encodings.Web": {
"type": "Transitive",
"resolved": "8.0.0",
Expand All @@ -44,6 +69,7 @@
"queil.ring.protocol": {
"type": "Project",
"dependencies": {
"MessagePack": "[2.*, )",
"System.Text.Json": "[8.*, )",
"System.Threading.Channels": "[8.*, )"
}
Expand Down
12 changes: 9 additions & 3 deletions src/Queil.Ring.DotNet.Cli/Abstractions/Runnable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ namespace Queil.Ring.DotNet.Cli.Abstractions;
using Logging;
using Microsoft.Extensions.Logging;
using Protocol;
using Protocol.Events;
using Stateless;

[DebuggerDisplay("{UniqueId}")]
public abstract class Runnable<TContext, TConfig> : IRunnable
public abstract class App<TContext, TConfig> : IRunnable
where TConfig : IRunnableConfig
{
private readonly Dictionary<string, object> _details = new();
private readonly Fsm _fsm = new();
private readonly ILogger<Runnable<TContext, TConfig>> _logger;
private readonly ILogger<App<TContext, TConfig>> _logger;
protected readonly ISender Sender;
private TContext? _context;
private Task _destroyTask = Task.CompletedTask;
private Task _stopTask = Task.CompletedTask;

protected Runnable(TConfig config, ILogger<Runnable<TContext, TConfig>> logger, ISender sender)
protected App(TConfig config, ILogger<App<TContext, TConfig>> logger, ISender sender)
{
Config = config;
if (Config.FriendlyName != null) _details.Add(DetailsKeys.FriendlyName, Config.FriendlyName);
Expand Down Expand Up @@ -343,6 +344,11 @@ private async Task DestroyCoreAsync(TContext ctx, CancellationToken token)
}
}

protected virtual void PublishLogs(string line)
{
Sender.Enqueue(Message.RunnableLogs(new RunnableLogLine { RunnableId = UniqueId, Line = line }));
}

private class Fsm() : StateMachine<State, Trigger>(State.Zero);
}

Expand Down
24 changes: 24 additions & 0 deletions src/Queil.Ring.DotNet.Cli/Logging/BroadcastSink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Queil.Ring.DotNet.Cli.Logging;

using Infrastructure;
using Protocol;
using Protocol.Events;
using Serilog.Core;
using Serilog.Events;

public class BroadcastSink(ISender sender) : ILogEventSink
{
public void Emit(Serilog.Events.LogEvent logEvent)
{
if (logEvent.Level < LogEventLevel.Information || !logEvent.Properties.TryGetValue(Scope.Broadcast, out _)) return;
var message = logEvent.RenderMessage();
sender.Enqueue(
Message.RunnableLogs(new RunnableLogLine
{
Line = message,
RunnableId = logEvent.Properties.TryGetValue(Scope.UniqueIdKey, out var uniqueId)
? uniqueId.ToString()
: null
}));
}
}
2 changes: 1 addition & 1 deletion src/Queil.Ring.DotNet.Cli/Logging/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public class Scope : Dictionary<string, object>
{
public const string LogEventKey = "LogEvent";
public const string UniqueIdKey = "UniqueId";

public const string Broadcast = "Broadcast";
public static Scope Event(LogEvent logEvent) => new() { { LogEventKey, logEvent } };
}
10 changes: 8 additions & 2 deletions src/Queil.Ring.DotNet.Cli/Logging/ScopeLoggingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ public static void LogContextDebug<T>(this ILogger<T> logger, object context)

public static IDisposable? WithLogErrorScope<T>(this ILogger<T> logger) =>
logger.BeginScope(new Scope
{ [Scope.LogEventKey] = $"{Red}ERROR" });
{
[Scope.LogEventKey] = $"{Red}ERROR",
[Scope.Broadcast] = true
});

public static IDisposable? WithLogInfoScope<T>(this ILogger<T> logger) =>
logger.BeginScope(new Scope
{ [Scope.LogEventKey] = $"{Gray}LOG" });
{
[Scope.LogEventKey] = $"{Gray}LOG",
[Scope.Broadcast] = true
});
}
17 changes: 9 additions & 8 deletions src/Queil.Ring.DotNet.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ static string Ring(string ver) =>
services.AddOptions();
services.Configure<RingConfiguration>(builder.Configuration);
services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));
services.AddSingleton<BroadcastSink>();
services.AddSingleton<IServer, Server>();
services.AddSingleton<WebsocketsHandler>();
services.AddSingleton<IConfigurationTreeReader, ConfigurationTreeReader>();
Expand Down Expand Up @@ -104,15 +105,20 @@ static string Ring(string ver) =>
services.AddTransient<DotnetCliBundle>();
services.AddTransient<DockerCompose>();

builder.Host.UseSerilog();
builder.Host.UseSerilog((c, f, l) =>
{
l.ReadFrom.Configuration(c.Configuration)
.WriteTo.Sink(f.GetRequiredService<BroadcastSink>());
if (options.IsDebug) l.MinimumLevel.Debug();
});

builder.Host.ConfigureContainer<IServiceContainer>((ctx, container) =>
{
var runnableTypes = Assembly.GetEntryAssembly().GetExportedTypes()
.Where(t => typeof(IRunnable).IsAssignableFrom(t)).ToList();

var configMap = (from r in runnableTypes
let cfg = r.GetProperty(nameof(Runnable<object, IRunnableConfig>.Config))
let cfg = r.GetProperty(nameof(App<object, IRunnableConfig>.Config))
where cfg != null
select (RunnableType: r, ConfigType: cfg.PropertyType))
.ToDictionary(x => x.ConfigType, x => x.RunnableType);
Expand Down Expand Up @@ -150,12 +156,7 @@ static string Ring(string ver) =>

var app = builder.Build();
app.Urls.Add($"http://0.0.0.0:{app.Configuration.GetValue<int>("ring:port")}");

var loggingConfig = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration);

if (options.IsDebug) loggingConfig.MinimumLevel.Debug();

Log.Logger = loggingConfig.CreateLogger();

var logger = app.Services.GetRequiredService<ILogger<Program>>();

using (logger.WithHostScope(LogEvent.INIT))
Expand Down
6 changes: 3 additions & 3 deletions src/Queil.Ring.DotNet.Cli/Runnables/CsProjRunnable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace Queil.Ring.DotNet.Cli.Runnables;
using Infrastructure;
using Microsoft.Extensions.Logging;

public abstract class CsProjRunnable<TContext, TConfig>(
public abstract class CsProjApp<TContext, TConfig>(
TConfig config,
ILogger<CsProjRunnable<TContext, TConfig>> logger,
ILogger<CsProjApp<TContext, TConfig>> logger,
ISender sender)
: ProcessRunnable<TContext, TConfig>(config, logger, sender)
: ProcessApp<TContext, TConfig>(config, logger, sender)
where TContext : ITrackProcessId, ICsProjContext, ITrackRetries
where TConfig : IRunnableConfig, IUseCsProjFile
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace Queil.Ring.DotNet.Cli.Runnables.DockerCompose;
using Tools;
using DockerComposeConfig = Configuration.Runnables.DockerCompose;

public class DockerComposeRunnable(
public class DockerComposeApp(
DockerComposeConfig config,
ILogger<Runnable<DockerComposeContext, DockerComposeConfig>> logger,
ILogger<App<DockerComposeContext, DockerComposeConfig>> logger,
ISender sender,
DockerCompose dockerCompose)
: Runnable<DockerComposeContext, DockerComposeConfig>(config, logger, sender)
: App<DockerComposeContext, DockerComposeConfig>(config, logger, sender)
{
public override string UniqueId => Config.Path;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace Queil.Ring.DotNet.Cli.Runnables.Dotnet;
using Tools;
using static Dtos.DetailsKeys;

public class AspNetCoreRunnable(
public class AspNetCoreApp(
AspNetCore config,
DotnetCliBundle dotnet,
ILogger<AspNetCoreRunnable> logger,
ILogger<AspNetCoreApp> logger,
ISender sender,
GitClone gitClone)
: DotnetRunnableBase<AspNetCoreContext, AspNetCore>(config, dotnet, logger, sender, gitClone)
: DotnetAppBase<AspNetCoreContext, AspNetCore>(config, dotnet, logger, sender, gitClone)
{
protected override async Task<AspNetCoreContext> InitAsync(CancellationToken token)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Queil.Ring.DotNet.Cli/Runnables/Dotnet/DotnetRunnable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace Queil.Ring.DotNet.Cli.Runnables.Dotnet;
using Microsoft.Extensions.Logging;
using Tools;

public abstract class DotnetRunnableBase<TContext, TConfig>(
public abstract class DotnetAppBase<TContext, TConfig>(
TConfig config,
DotnetCliBundle dotnet,
ILogger<DotnetRunnableBase<TContext, TConfig>> logger,
ILogger<DotnetAppBase<TContext, TConfig>> logger,
ISender sender,
GitClone gitClone)
: ProcessRunnable<TContext, TConfig>(config, logger, sender)
: ProcessApp<TContext, TConfig>(config, logger, sender)
where TContext : DotnetContext
where TConfig : IUseCsProjFile, IRunnableConfig
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ namespace Queil.Ring.DotNet.Cli.Runnables.Kustomize;
using Microsoft.Extensions.Options;
using Tools;

public class KustomizeRunnable(
public class KustomizeApp(
KustomizeConfig config,
IOptions<RingConfiguration> ringCfg,
ILogger<Runnable<KustomizeContext, KustomizeConfig>> logger,
ILogger<App<KustomizeContext, KustomizeConfig>> logger,
ISender sender,
KubectlBundle bundle)
: Runnable<KustomizeContext, KustomizeConfig>(config, logger, sender)
: App<KustomizeContext, KustomizeConfig>(config, logger, sender)
{
private const string NamespacesPath = "{range .items[?(@.kind=='Namespace')]}{.metadata.name}{'\\n'}{end}";

private readonly string _cacheDir = ringCfg?.Value?.Kustomize.CachePath ??
throw new ArgumentNullException(nameof(RingConfiguration.Kustomize.CachePath));

private readonly ILogger<Runnable<KustomizeContext, KustomizeConfig>> _logger = logger;
private readonly ILogger<App<KustomizeContext, KustomizeConfig>> _logger = logger;

public override string UniqueId => Config.Path;
protected override TimeSpan HealthCheckPeriod => TimeSpan.FromSeconds(10);
Expand Down
6 changes: 3 additions & 3 deletions src/Queil.Ring.DotNet.Cli/Runnables/Proc/ProcRunnable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public class ProcContext : ITrackProcessId
public int ProcessId { get; set; }
}

public class ProcRunnable : ProcessRunnable<ProcContext, Proc>
public class ProcApp : ProcessApp<ProcContext, Proc>
{
private readonly ProcessRunner _runner;

public ProcRunnable(Proc config,
ILogger<ProcessRunnable<ProcContext, Proc>> logger,
public ProcApp(Proc config,
ILogger<ProcessApp<ProcContext, Proc>> logger,
ISender sender,
ProcessRunner runner) : base(config, logger, sender)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Queil.Ring.DotNet.Cli/Runnables/ProcessRunnable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
using Microsoft.Extensions.Logging;
using Tools;

public abstract class ProcessRunnable<TContext, TConfig>(
public abstract class ProcessApp<TContext, TConfig>(
TConfig config,
ILogger<ProcessRunnable<TContext, TConfig>> logger,
ILogger<ProcessApp<TContext, TConfig>> logger,
ISender sender)
: Runnable<TContext, TConfig>(config, logger, sender)
: App<TContext, TConfig>(config, logger, sender)
where TContext : ITrackProcessId
where TConfig : IRunnableConfig
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
using Tools.Windows;
using IISExpressConfig = Configuration.Runnables.IISExpress;

public class IISExpressRunnable(
public class IisExpressApp(
IISExpressConfig config,
IISExpressExe iisExpress,
ILogger<IISExpressRunnable> logger,
ILogger<IisExpressApp> logger,
ISender sender,
Func<Uri, HttpClient> clientFactory)
: CsProjRunnable<IISExpressContext, IISExpressConfig>(config, logger, sender)
: CsProjApp<IISExpressContext, IISExpressConfig>(config, logger, sender)
{
private readonly List<string> _wcfServices = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
using Tools.Windows;
using IISXCoreConfig = Configuration.Runnables.IISXCore;

public class IISXCoreRunnable(
public class IisxCoreApp(
IISXCoreConfig config,
IISExpressExe iisExpress,
ILogger<IISXCoreRunnable> logger,
ILogger<IisxCoreApp> logger,
ISender sender,
GitClone gitClone)
: CsProjRunnable<IISXCoreContext, IISXCoreConfig>(config, logger, sender)
: CsProjApp<IISXCoreContext, IISXCoreConfig>(config, logger, sender)
{
protected override IISXCoreContext CreateContext()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace Queil.Ring.DotNet.Cli.Runnables.Windows.NetExe;
using Microsoft.Extensions.Logging;
using Tools;

public class NetExeRunnable(
public class NetExeApp(
NetExeConfig config,
ProcessRunner processRunner,
ILogger<NetExeRunnable> logger,
ILogger<NetExeApp> logger,
ISender sender)
: CsProjRunnable<NetExeContext, NetExeConfig>(config, logger, sender)
: CsProjApp<NetExeContext, NetExeConfig>(config, logger, sender)
{
protected override NetExeContext CreateContext()
{
Expand Down
3 changes: 1 addition & 2 deletions src/Queil.Ring.DotNet.Cli/Tools/DockerCompose.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Queil.Ring.DotNet.Cli.Tools;

using System;
using System.Threading;
using System.Threading.Tasks;
using Abstractions.Tools;
Expand All @@ -9,7 +8,7 @@ namespace Queil.Ring.DotNet.Cli.Tools;
public class DockerCompose(ILogger<ITool> logger) : ITool
{
public string Command { get; set; } = "docker-compose";
public string[] DefaultArgs { get; set; } = Array.Empty<string>();
public string[] DefaultArgs { get; set; } = [];
public ILogger<ITool> Logger { get; } = logger;

public async Task<ExecutionInfo> RmAsync(string composeFilePath, CancellationToken token) =>
Expand Down
2 changes: 1 addition & 1 deletion src/Queil.Ring.DotNet.Cli/Tools/DotnetCliBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class DotnetCliBundle(ProcessRunner processRunner, ILogger<DotnetCliBundl
private readonly Dictionary<string, string> DefaultEnvVars = new() { ["ASPNETCORE_ENVIRONMENT"] = "Development" };
public ILogger<ITool> Logger { get; } = logger;
public string Command { get; set; } = "dotnet";
public string[] DefaultArgs { get; set; } = Array.Empty<string>();
public string[] DefaultArgs { get; set; } = [];

public async Task<ExecutionInfo> RunAsync(DotnetContext ctx, CancellationToken token, string[]? urls = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Queil.Ring.DotNet.Cli/Tools/GitClone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public partial class GitClone(ILogger<GitClone> logger, IOptions<RingConfigurati
ringCfg?.Value ?? throw new NullReferenceException(nameof(ringCfg.Value));

public string Command { get; set; } = "git";
public string[] DefaultArgs { get; set; } = Array.Empty<string>();
public string[] DefaultArgs { get; set; } = [];
public ILogger<ITool> Logger { get; } = logger;

public string ResolveFullClonePath(IFromGit gitCfg, string? rootPathOverride = null)
Expand Down
2 changes: 1 addition & 1 deletion src/Queil.Ring.DotNet.Cli/Tools/Kubectl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class KubectlBundle(
private readonly string[] _allowedContexts = config.Value.Kubernetes.AllowedContexts ?? Array.Empty<string>();

public string Command { get; set; } = "kubectl";
public string[] DefaultArgs { get; set; } = Array.Empty<string>();
public string[] DefaultArgs { get; set; } = [];
public ILogger<ITool> Logger { get; } = logger;

public async Task EnsureContextIsAllowed(CancellationToken token)
Expand Down
Loading
Loading