add azaion loader

This commit is contained in:
Alex Bezdieniezhnykh
2025-06-01 19:16:49 +03:00
parent 2584b4f125
commit 500db31142
54 changed files with 629 additions and 291 deletions
+49 -24
View File
@@ -1,30 +1,30 @@
using System.Diagnostics;
using System.Text;
using Azaion.CommonSecurity.DTO;
using MediatR;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Azaion.CommonSecurity.DTO.Commands;
using MessagePack;
using NetMQ;
using NetMQ.Sockets;
using Serilog;
using Exception = System.Exception;
namespace Azaion.CommonSecurity.Services;
public class LoaderClient
public class LoaderClient(LoaderClientConfig config, ILogger logger, CancellationToken ct = default)
{
private readonly IMediator _mediator;
private readonly ILogger<LoaderClient> _logger;
private readonly DealerSocket _dealer = new();
private readonly Guid _clientId = Guid.NewGuid();
public LoaderClient(IMediator mediator, IOptions<LoaderClientConfig> config, ILogger<LoaderClient> logger)
public void StartClient()
{
_mediator = mediator;
_logger = logger;
try
{
using var process = new Process();
process.StartInfo = new ProcessStartInfo
{
FileName = SecurityConstants.LoaderPath,
Arguments = $"--port {config.Value.ZeroMqPort} --api {config.Value.ApiUrl}",
//RedirectStandardOutput = true,
//RedirectStandardError = true,
//CreateNoWindow = true
FileName = SecurityConstants.EXTERNAL_LOADER_PATH,
Arguments = $"--port {config.ZeroMqPort} --api {config.ApiUrl}",
CreateNoWindow = true
};
process.OutputDataReceived += (_, e) => { if (e.Data != null) Console.WriteLine(e.Data); };
@@ -33,19 +33,44 @@ public class LoaderClient
}
catch (Exception e)
{
Console.WriteLine(e);
//throw;
logger.Error(e.Message);
throw;
}
}
_requestAddress = $"tcp://{gpsConfig.Value.ZeroMqHost}:{gpsConfig.Value.ZeroMqPort}";
_requestSocket.Connect(_requestAddress);
public void Connect()
{
_dealer.Options.Identity = Encoding.UTF8.GetBytes(_clientId.ToString("N"));
_dealer.Connect($"tcp://{config.ZeroMqHost}:{config.ZeroMqPort}");
}
_subscriberAddress = $"tcp://{gpsConfig.Value.ZeroMqHost}:{gpsConfig.Value.ZeroMqReceiverPort}";
_subscriberSocket.Connect(_subscriberAddress);
_subscriberSocket.Subscribe("");
_subscriberSocket.ReceiveReady += async (sender, e) => await ProcessClientCommand(sender, e);
public void Send(RemoteCommand command) =>
_dealer.SendFrame(MessagePackSerializer.Serialize(command));
_poller.Add(_subscriberSocket);
_poller.RunAsync();
public MemoryStream LoadFile(string filename, string folder)
{
try
{
Send(RemoteCommand.Create(CommandType.Load, new LoadFileData(filename, folder)));
var retries = 10;
var tryNum = 0;
while (!ct.IsCancellationRequested && tryNum++ < retries)
{
if (!_dealer.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(150), out var bytes))
continue;
var resultCommand = MessagePackSerializer.Deserialize<RemoteCommand>(bytes, cancellationToken: ct);
if (resultCommand.Data?.Length == 0)
throw new Exception($"Can't load {filename}. Returns 0 bytes");
return new MemoryStream(resultCommand.Data!);
}
throw new Exception($"Can't load file {filename} after {retries} retries");
}
catch (Exception e)
{
logger.Error(e, e.Message);
throw;
}
}
}