mirror of
https://github.com/9ParsonsB/Pulsar.git
synced 2025-07-01 16:33:43 -04:00
Rework Journal Handling
Stub ModulesInfo
This commit is contained in:
@ -1,30 +1,12 @@
|
||||
namespace Pulsar.Features.Status;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/status")]
|
||||
public class StatusController(IOptions<PulsarConfiguration> pulsarOptions, IHubContext<EventsHub, IEventsHub> hub) : ControllerBase
|
||||
[Route("api/[controller]")]
|
||||
public class StatusController(IStatusService status) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Get()
|
||||
{
|
||||
// TODO: put in service
|
||||
var journalDir = pulsarOptions.Value.JournalDirectory;
|
||||
var dir = new DirectoryInfo(journalDir);
|
||||
|
||||
if (!dir.Exists)
|
||||
return Problem("Journal directory does not exist.");
|
||||
|
||||
var files = dir.GetFiles();
|
||||
|
||||
var statusFile = files.FirstOrDefault(f =>
|
||||
string.Equals(f.Name, "status.json", StringComparison.InvariantCultureIgnoreCase));
|
||||
|
||||
if (statusFile == null)
|
||||
return Problem("Status file not found.");
|
||||
|
||||
await using var file = System.IO.File.Open(statusFile.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
var status = await JsonSerializer.DeserializeAsync<Observatory.Framework.Files.Status>(file);
|
||||
await hub.Clients.All.StatusUpdated(status);
|
||||
return Ok(status);
|
||||
return Ok(status.Get());
|
||||
}
|
||||
}
|
@ -1,18 +1,71 @@
|
||||
namespace Pulsar.Features.Status;
|
||||
|
||||
public class StatusService : IStatusService
|
||||
using Observatory.Framework.Files;
|
||||
|
||||
public interface IStatusService : IJournalHandler<Status>;
|
||||
public class StatusService(ILogger<StatusService> logger, IOptions<PulsarConfiguration> options, IEventHubContext hub) : IStatusService
|
||||
{
|
||||
public void HandleFile(string fileInfo)
|
||||
public string FileName => FileHandlerService.StatusFileName;
|
||||
|
||||
public async Task HandleFile(string filePath)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (!ValidateFile(filePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var file = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
var status = await JsonSerializer.DeserializeAsync<Status>(file);
|
||||
|
||||
if (status == null)
|
||||
{
|
||||
logger.LogWarning("Failed to deserialize status file {FilePath}", filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
await hub.Clients.All.StatusUpdated(status);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IStatusService : IJournalHandler
|
||||
{
|
||||
}
|
||||
public bool ValidateFile(string filePath)
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
logger.LogWarning("Status file {StatusFile} does not exist", filePath);
|
||||
return false;
|
||||
}
|
||||
|
||||
var fileInfo = new FileInfo(filePath);
|
||||
|
||||
if (!string.Equals(fileInfo.Name, FileName, StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
logger.LogWarning("File {StatusFile} is not a status file", filePath);
|
||||
return false;
|
||||
}
|
||||
|
||||
public interface IJournalHandler
|
||||
{
|
||||
void HandleFile(string fileInfo);
|
||||
}
|
||||
if (fileInfo.Length == 0)
|
||||
{
|
||||
logger.LogWarning("Status file {StatusFile} is empty", filePath);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<Status> Get()
|
||||
{
|
||||
var statusFile = Path.Combine(options.Value.JournalDirectory, FileName);
|
||||
|
||||
if (!ValidateFile(statusFile))
|
||||
{
|
||||
return new Status();
|
||||
}
|
||||
|
||||
await using var file = File.Open(statusFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
var status = await JsonSerializer.DeserializeAsync<Status>(file);
|
||||
if (status != null) return status;
|
||||
|
||||
logger.LogWarning("Failed to deserialize status file {StatusFile}", statusFile);
|
||||
return new Status();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user