mirror of
https://github.com/9ParsonsB/Pulsar.git
synced 2025-07-02 17:03:41 -04:00
Rework JournalHandler Again
Reduce Code Dupe Finished ModulesInfo Started ShipLocker
This commit is contained in:
@ -3,58 +3,35 @@ namespace Pulsar.Features.Status;
|
||||
using Observatory.Framework.Files;
|
||||
|
||||
public interface IStatusService : IJournalHandler<Status>;
|
||||
public class StatusService(ILogger<StatusService> logger, IOptions<PulsarConfiguration> options, IEventHubContext hub) : IStatusService
|
||||
|
||||
public class StatusService(ILogger<StatusService> logger, IOptions<PulsarConfiguration> options, IEventHubContext hub)
|
||||
: JournalHandlerBase<Status>(logger), IStatusService
|
||||
{
|
||||
public string FileName => FileHandlerService.StatusFileName;
|
||||
|
||||
public async Task HandleFile(string filePath)
|
||||
public override string FileName => FileHandlerService.StatusFileName;
|
||||
|
||||
public override async Task HandleFile(string filePath)
|
||||
{
|
||||
if (!ValidateFile(filePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var file = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
var status = await JsonSerializer.DeserializeAsync<Status>(file);
|
||||
|
||||
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 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;
|
||||
}
|
||||
|
||||
if (fileInfo.Length == 0)
|
||||
{
|
||||
logger.LogWarning("Status file {StatusFile} is empty", filePath);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<Status> Get()
|
||||
public override async Task<Status> Get()
|
||||
{
|
||||
var statusFile = Path.Combine(options.Value.JournalDirectory, FileName);
|
||||
|
||||
|
||||
if (!ValidateFile(statusFile))
|
||||
{
|
||||
return new Status();
|
||||
@ -63,9 +40,8 @@ public class StatusService(ILogger<StatusService> logger, IOptions<PulsarConfigu
|
||||
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