add resource check

incorrect pass / hw handling in a loader
This commit is contained in:
Alex Bezdieniezhnykh
2025-06-15 15:01:55 +03:00
parent c0f8dd792d
commit def7aad833
14 changed files with 193 additions and 16 deletions
+9 -3
View File
@@ -1,9 +1,15 @@
using MessagePack;
namespace Azaion.LoaderUI;
public class ApiCredentials(string email, string pw)
[MessagePackObject]
public class ApiCredentials
{
public string Email { get; set; } = email;
public string Password { get; set; } = pw;
[Key(nameof(Email))]
public string Email { get; set; } = null!;
[Key(nameof(Password))]
public string Password { get; set; } = null!;
public bool IsValid() =>
!string.IsNullOrWhiteSpace(Email) && !string.IsNullOrWhiteSpace(Password);
+2
View File
@@ -10,9 +10,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MessagePack" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.5" />
<PackageReference Include="NetMQ" Version="4.0.1.13" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="9.0.1" />
+4
View File
@@ -6,4 +6,8 @@ public static class Constants
public const string API_URL = "https://api.azaion.com";
public const string AZAION_SUITE_EXE = "Azaion.Suite.exe";
public const string SUITE_FOLDER = "suite";
public const string INFERENCE_EXE = "azaion-inference";
public const string EXTERNAL_LOADER_PATH = "azaion-loader.exe";
public const int EXTERNAL_LOADER_PORT = 5020;
public const string EXTERNAL_LOADER_HOST = "127.0.0.1";
}
+104 -6
View File
@@ -1,10 +1,15 @@
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using MessagePack;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NetMQ;
using NetMQ.Sockets;
namespace Azaion.LoaderUI;
@@ -23,17 +28,40 @@ public partial class Login
InitializeComponent();
}
private void SetControlsStatus(bool isLoading)
{
LoginBtn.IsEnabled = !isLoading;
TbEmail.IsEnabled = !isLoading;
TbPassword.IsEnabled = !isLoading;
LoginBtn.Cursor = isLoading ? Cursors.Wait : Cursors.Arrow;
Cursor = isLoading ? Cursors.Wait : Cursors.Arrow;
}
private async void LoginClick(object sender, RoutedEventArgs e)
{
var creds = new ApiCredentials(TbEmail.Text, TbPassword.Password);
var creds = new ApiCredentials
{
Email = TbEmail.Text,
Password = TbPassword.Password
};
if (!creds.IsValid())
return;
LoginBtn.Cursor = Cursors.Wait;
Cursor = Cursors.Wait;
SetControlsStatus(isLoading: true);
_azaionApi.Login(creds);
try
{
Validate(creds);
}
catch (Exception exception)
{
_logger.LogError(exception, exception.Message);
TbStatus.Foreground = Brushes.Red;
TbStatus.Text = exception.Message;
SetControlsStatus(isLoading: false);
return;
}
TbStatus.Foreground = Brushes.Black;
var installerVersion = await GetInstallerVer();
var localVersion = GetLocalVer();
@@ -44,12 +72,81 @@ public partial class Login
TbStatus.Text = $"Installed {installerVersion}!";
}
else
TbStatus.Text = $"Your version is up to date!";
TbStatus.Text = "Your version is up to date!";
Process.Start(Constants.AZAION_SUITE_EXE, $"-e {creds.Email} -p {creds.Password}");
await Task.Delay(800);
TbStatus.Text = "Loading...";
while (!Process.GetProcessesByName(Constants.INFERENCE_EXE).Any())
await Task.Delay(500);
await Task.Delay(500);
Close();
}
private void Validate(ApiCredentials creds)
{
var dealer = new DealerSocket();
try
{
using var process = new Process();
process.StartInfo = new ProcessStartInfo
{
FileName = Constants.EXTERNAL_LOADER_PATH,
Arguments = $"--port {Constants.EXTERNAL_LOADER_PORT} --api {Constants.API_URL}",
CreateNoWindow = true
};
process.Start();
dealer.Options.Identity = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString("N"));
dealer.Connect($"tcp://{Constants.EXTERNAL_LOADER_HOST}:{Constants.EXTERNAL_LOADER_PORT}");
var result = SendCommand(dealer, RemoteCommand.Create(CommandType.Login, creds));
if (result.CommandType != CommandType.Ok)
throw new Exception(result.Message);
result = SendCommand(dealer, RemoteCommand.Create(CommandType.CheckResource));
if (result.CommandType != CommandType.Ok)
throw new Exception(result.Message);
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
throw;
}
finally
{
SendCommand(dealer, RemoteCommand.Create(CommandType.Exit));
dealer.Close();
dealer.Dispose();
}
}
private RemoteCommand SendCommand(DealerSocket dealer, RemoteCommand command, int retryCount = 30, int retryDelayMs = 800)
{
try
{
dealer.SendFrame(MessagePackSerializer.Serialize(command));
var tryNum = 0;
while (tryNum++ < retryCount)
{
if (!dealer.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(retryDelayMs), out var bytes))
continue;
var res = MessagePackSerializer.Deserialize<RemoteCommand>(bytes);
if (res.CommandType == CommandType.Error)
throw new Exception(res.Message);
return res;
}
throw new Exception($"Sent {command} {retryCount} times, with wait time {retryDelayMs}ms for each call. No response from client.");
}
catch (Exception e)
{
_logger.LogError(e, e.Message);
throw;
}
}
private async Task DownloadAndRunInstaller()
{
var (installerName, stream) = await _azaionApi.DownloadInstaller(_dirConfig?.SuiteInstallerDirectory ?? "");
@@ -65,6 +162,7 @@ public partial class Login
var process = Process.Start(processInfo);
await process!.WaitForExitAsync();
File.Delete(installerName);
}
private async Task<Version> GetInstallerVer()
+56
View File
@@ -0,0 +1,56 @@
using MessagePack;
namespace Azaion.LoaderUI;
[MessagePackObject]
public class RemoteCommand(CommandType commandType, byte[]? data = null, string? message = null)
{
[Key("CommandType")]
public CommandType CommandType { get; set; } = commandType;
[Key("Data")]
public byte[]? Data { get; set; } = data;
[Key("Message")]
public string? Message { get; set; } = message;
public static RemoteCommand Create(CommandType commandType) =>
new(commandType);
public static RemoteCommand Create<T>(CommandType commandType, T data, string? message = null) where T : class =>
new(commandType, MessagePackSerializer.Serialize(data), message);
public override string ToString() => $"({CommandType.ToString().ToUpper()}: Data: {Data?.Length ?? 0} bytes. Message: {Message})";
}
[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,
Ok = 3,
Login = 10,
CheckResource = 12,
ListRequest = 15,
ListFiles = 18,
Load = 20,
LoadBigSmall = 22,
UploadBigSmall = 24,
DataBytes = 25,
Inference = 30,
InferenceData = 35,
StopInference = 40,
AIAvailabilityCheck = 80,
AIAvailabilityResult = 85,
Error = 90,
Exit = 100,
}