w4c-workflows-api/Services/RuntimeSelfInfo.cs
2026-09-09 16:09:48 +03:00

73 lines
2.3 KiB
C#

using System.Diagnostics;
using System.Reflection;
namespace w4c_workflows.Services;
/// <summary>
/// Static self-description of this workflows-api instance, served at
/// <c>GET /api/about</c>. Used by the platform to show a runtime's name/version in
/// the runtime selector and by self-hosted runtimes to report themselves when they
/// connect over the outbound channel.
/// </summary>
public sealed record RuntimeSelfInfo(
string Name,
string Version,
DateTimeOffset StartedAt,
bool MultiTenant);
/// <summary>
/// Produces <see cref="RuntimeSelfInfo"/> from configuration + process state.
/// <c>name</c> ← <c>Runtime:Name</c> (default <c>APP_NAME</c> / app name),
/// <c>version</c> ← assembly informational version, <c>startedAt</c> ← process
/// start, <c>multiTenant</c> ← <c>!UseLiteMode</c> (overridable via
/// <c>Runtime:MultiTenant</c>).
/// </summary>
public sealed class RuntimeSelfInfoProvider
{
private readonly RuntimeSelfInfo _info;
public RuntimeSelfInfoProvider(IConfiguration config)
{
var name = FirstNonEmpty(
config["Runtime:Name"],
config["APP_NAME"],
"w4c-workflows-api")!;
var version =
Assembly.GetExecutingAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
?? Assembly.GetExecutingAssembly().GetName().Version?.ToString()
?? "unknown";
DateTimeOffset startedAt;
try
{
startedAt = Process.GetCurrentProcess().StartTime.ToUniversalTime();
}
catch
{
startedAt = DateTimeOffset.UtcNow;
}
var liteMode = config.GetValue<bool>("UseLiteMode");
var multiTenant = !liteMode;
if (config["Runtime:MultiTenant"] is { Length: > 0 } raw &&
bool.TryParse(raw, out var parsed))
{
multiTenant = parsed;
}
_info = new RuntimeSelfInfo(name, version, startedAt, multiTenant);
}
public RuntimeSelfInfo Get() => _info;
private static string? FirstNonEmpty(params string?[] values)
{
foreach (var value in values)
if (!string.IsNullOrWhiteSpace(value))
return value.Trim();
return null;
}
}