mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 12:36:31 +00:00
big refactoring. get rid of static properties and coupled architecture. prepare system for integration tests
This commit is contained in:
@@ -10,27 +10,34 @@ using Exception = System.Exception;
|
||||
|
||||
namespace Azaion.Common.Services;
|
||||
|
||||
public class LoaderClient(LoaderClientConfig config, ILogger logger, CancellationToken ct = default) : IDisposable
|
||||
public class LoaderClient : IDisposable
|
||||
{
|
||||
private readonly LoaderClientConfig _config;
|
||||
private readonly ILogger _logger;
|
||||
private readonly CancellationToken _ct;
|
||||
private readonly IProcessLauncher _processLauncher;
|
||||
private readonly DealerSocket _dealer = new();
|
||||
private readonly Guid _clientId = Guid.NewGuid();
|
||||
|
||||
public LoaderClient(LoaderClientConfig config, ILogger logger, IProcessLauncher processLauncher, CancellationToken ct = default)
|
||||
{
|
||||
_config = config;
|
||||
_logger = logger;
|
||||
_processLauncher = processLauncher;
|
||||
_ct = ct;
|
||||
}
|
||||
|
||||
public void StartClient()
|
||||
{
|
||||
try
|
||||
{
|
||||
using var process = new Process();
|
||||
process.StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = Constants.EXTERNAL_LOADER_PATH,
|
||||
Arguments = $"--port {config.ZeroMqPort} --api {config.ApiUrl}",
|
||||
CreateNoWindow = true
|
||||
};
|
||||
process.Start();
|
||||
_processLauncher.Launch(
|
||||
Constants.EXTERNAL_LOADER_PATH,
|
||||
$"--port {_config.ZeroMqPort} --api {_config.ApiUrl}");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.Error(e, e.Message);
|
||||
_logger.Error(e, e.Message);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -38,7 +45,7 @@ public class LoaderClient(LoaderClientConfig config, ILogger logger, Cancellatio
|
||||
public void Connect()
|
||||
{
|
||||
_dealer.Options.Identity = Encoding.UTF8.GetBytes(_clientId.ToString("N"));
|
||||
_dealer.Connect($"tcp://{config.ZeroMqHost}:{config.ZeroMqPort}");
|
||||
_dealer.Connect($"tcp://{_config.ZeroMqHost}:{_config.ZeroMqPort}");
|
||||
}
|
||||
|
||||
public void Login(ApiCredentials credentials)
|
||||
@@ -63,11 +70,11 @@ public class LoaderClient(LoaderClientConfig config, ILogger logger, Cancellatio
|
||||
_dealer.SendFrame(MessagePackSerializer.Serialize(command));
|
||||
|
||||
var tryNum = 0;
|
||||
while (!ct.IsCancellationRequested && tryNum++ < retryCount)
|
||||
while (!_ct.IsCancellationRequested && tryNum++ < retryCount)
|
||||
{
|
||||
if (!_dealer.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(retryDelayMs), out var bytes))
|
||||
continue;
|
||||
var res = MessagePackSerializer.Deserialize<RemoteCommand>(bytes, cancellationToken: ct);
|
||||
var res = MessagePackSerializer.Deserialize<RemoteCommand>(bytes, cancellationToken: _ct);
|
||||
if (res.CommandType == CommandType.Error)
|
||||
throw new Exception(res.Message);
|
||||
return res;
|
||||
@@ -77,7 +84,7 @@ public class LoaderClient(LoaderClientConfig config, ILogger logger, Cancellatio
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.Error(e, e.Message);
|
||||
_logger.Error(e, e.Message);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user