2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-04-05 17:39:39 -04:00
pulsar/Pulsar/Features/ModulesInfo/ModulesInfoService.cs
Ben Parsons efd0b3e0c0 Journals Now processed in own thread
Some invalid journal data is now handled
Journals now use polymorphic deserialization
Added Event names to all journal events
Remove unused controllers
2024-05-24 19:30:12 +10:00

50 lines
1.6 KiB
C#

namespace Pulsar.Features.ModulesInfo;
using Observatory.Framework.Files;
public interface IModulesInfoService : IJournalHandler<ModuleInfoFile>;
public class ModulesInfoService(
ILogger<ModulesInfoService> logger,
IOptions<PulsarConfiguration> options,
IEventHubContext hub) : IModulesInfoService
{
public string FileName => FileHandlerService.ModulesInfoFileName;
public async Task HandleFile(string filePath, CancellationToken token = new())
{
if (!FileHelper.ValidateFile(filePath))
{
return;
}
var file = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var moduleInfo = await JsonSerializer.DeserializeAsync<ModuleInfoFile>(file, cancellationToken: token);
if (moduleInfo == null)
{
logger.LogWarning("Failed to deserialize status file {FilePath}", filePath);
return;
}
await hub.Clients.All.ModuleInfoUpdated(moduleInfo);
}
public async Task<ModuleInfoFile> Get()
{
var moduleInfoFile = Path.Combine(options.Value.JournalDirectory, FileName);
if (!FileHelper.ValidateFile(moduleInfoFile))
{
return new ModuleInfoFile();
}
await using var file = File.Open(moduleInfoFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var moduleInfo = await JsonSerializer.DeserializeAsync<ModuleInfoFile>(file);
if (moduleInfo != null) return moduleInfo;
logger.LogWarning("Failed to deserialize module info file {ModuleInfoFile}", moduleInfoFile);
return new ModuleInfoFile();
}
}