make python app load a bit eariler, making startup a bit faster

This commit is contained in:
Alex Bezdieniezhnykh
2025-02-13 18:13:15 +02:00
parent e329e5bb67
commit cfd5483a18
31 changed files with 183 additions and 124 deletions
+10 -1
View File
@@ -1,7 +1,16 @@
namespace Azaion.CommonSecurity.DTO;
using MessagePack;
namespace Azaion.CommonSecurity.DTO;
[MessagePackObject]
public class ApiCredentials(string email, string password) : EventArgs
{
[Key(nameof(Email))]
public string Email { get; set; } = email;
[Key(nameof(Password))]
public string Password { get; set; } = password;
[Key(nameof(Folder))]
public string Folder { get; set; } = null!;
}
@@ -3,22 +3,36 @@
namespace Azaion.CommonSecurity.DTO.Commands;
[MessagePackObject]
public class RemoteCommand(CommandType commandType, string? filename = null, byte[]? data = null)
public class RemoteCommand(CommandType commandType, byte[]? data = null)
{
[Key("CommandType")]
public CommandType CommandType { get; set; } = commandType;
[Key("Filename")]
public string? Filename { get; set; } = filename;
[Key("Data")]
public byte[]? Data { get; set; } = data;
public static byte[] Serialize<T>(CommandType commandType, T data) where T : class
{
var dataBytes = MessagePackSerializer.Serialize(data);
return MessagePackSerializer.Serialize(new RemoteCommand(commandType, dataBytes ));
}
}
[MessagePackObject]
public class LoadFileData(string filename, string? folder = null )
{
[Key(nameof(Folder))]
public string? Folder { get; set; } = folder;
[Key(nameof(Filename))]
public string Filename { get; set; } = filename;
}
public enum CommandType
{
None = 0,
GetUser = 10,
Login = 10,
Load = 20,
Inference = 30,
StopInference = 40,
+2 -2
View File
@@ -6,8 +6,8 @@ namespace Azaion.CommonSecurity.DTO;
[MessagePackObject]
public class User
{
[Key("i")]public string Id { get; set; }
[Key("e")]public string Email { get; set; }
[Key("i")] public string Id { get; set; } = "";
[Key("e")] public string Email { get; set; } = "";
[Key("r")]public RoleEnum Role { get; set; }
//For deserializing
@@ -10,7 +10,7 @@ namespace Azaion.CommonSecurity.Services;
public interface IResourceLoader
{
MemoryStream LoadFileFromPython(string fileName);
MemoryStream LoadFileFromPython(string fileName, string? folder = null);
void StopPython();
}
@@ -25,29 +25,22 @@ public class PythonResourceLoader : IResourceLoader, IAuthProvider
private readonly DealerSocket _dealer = new();
private readonly Guid _clientId = Guid.NewGuid();
public User CurrentUser { get; }
public User CurrentUser { get; set; }
public PythonResourceLoader(ApiConfig apiConfig, ApiCredentials credentials, AzaionApiClient api)
public PythonResourceLoader()
{
StartPython(apiConfig, credentials);
StartPython();
_dealer.Options.Identity = Encoding.UTF8.GetBytes(_clientId.ToString("N"));
_dealer.Connect($"tcp://{SecurityConstants.ZMQ_HOST}:{SecurityConstants.ZMQ_PORT}");
_dealer.SendFrame(MessagePackSerializer.Serialize(new RemoteCommand(CommandType.GetUser)));
var user = _dealer.Get<User>();
if (user == null)
throw new Exception("Can't get user from Auth provider");
CurrentUser = user;
}
private void StartPython( ApiConfig apiConfig, ApiCredentials credentials)
private void StartPython()
{
using var process = new Process();
process.StartInfo = new ProcessStartInfo
{
FileName = SecurityConstants.AZAION_INFERENCE_PATH,
Arguments = $"-e {credentials.Email} -p {credentials.Password} -f {apiConfig.ResourcesFolder}",
//Arguments = $"-e {credentials.Email} -p {credentials.Password} -f {apiConfig.ResourcesFolder}",
//UseShellExecute = false,
//RedirectStandardOutput = true,
// RedirectStandardError = true,
@@ -59,17 +52,27 @@ public class PythonResourceLoader : IResourceLoader, IAuthProvider
process.Start();
}
public void Login(ApiCredentials credentials)
{
_dealer.SendFrame(RemoteCommand.Serialize(CommandType.Login, credentials));
var user = _dealer.Get<User>();
if (user == null)
throw new Exception("Can't get user from Auth provider");
CurrentUser = user;
}
public void StopPython()
{
_dealer.SendFrame(MessagePackSerializer.Serialize(new RemoteCommand(CommandType.Exit)));
_dealer?.Close();
_dealer.Close();
}
public MemoryStream LoadFileFromPython(string fileName)
public MemoryStream LoadFileFromPython(string fileName, string? folder = null)
{
try
{
_dealer.SendFrame(MessagePackSerializer.Serialize(new RemoteCommand(CommandType.Load, fileName)));
_dealer.SendFrame(RemoteCommand.Serialize(CommandType.Load, new LoadFileData(fileName, folder)));
if (!_dealer.TryReceiveFrameBytes(TimeSpan.FromSeconds(3), out var bytes))
throw new Exception($"Unable to receive {fileName}");