zcbot/windows-node/Zcbot.WindowsNode/Runtime/SoftwareProbeService.cs

43 lines
1.0 KiB
C#

namespace Zcbot.WindowsNode;
internal sealed class SoftwareProbeService
{
private readonly Func<AdapterRuntimeStatus> probe;
private readonly object sync = new();
private AdapterRuntimeStatus? cachedRuntime;
private DateTimeOffset runtimeCheckedAt;
internal SoftwareProbeService(AdapterExecutionService execution)
: this(execution.Probe)
{
}
internal SoftwareProbeService(Func<AdapterRuntimeStatus> probe)
{
this.probe = probe;
}
internal AdapterRuntimeStatus Detect()
{
lock (sync)
{
if (cachedRuntime is null
|| DateTimeOffset.UtcNow - runtimeCheckedAt > TimeSpan.FromSeconds(30))
{
cachedRuntime = probe();
runtimeCheckedAt = DateTimeOffset.UtcNow;
}
return cachedRuntime;
}
}
internal void Invalidate()
{
lock (sync)
{
cachedRuntime = null;
runtimeCheckedAt = default;
}
}
}