This commit is contained in:
2024-06-25 14:47:28 +03:00
parent ce48d72209
commit 66d94ced8a
18 changed files with 1402 additions and 3 deletions

View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Esiur" Version="2.4.1" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
<PackageReference Include="System.Management" Version="8.0.0" />
</ItemGroup>
</Project>

28
AZ.Compute.Agent/App.cs Normal file
View File

@@ -0,0 +1,28 @@

using AZ.Compute.Agent;
using Esiur.Net.IIP;
using Esiur.Resource;
using Esiur.Stores;
internal class App
{
private static async Task Main(string[] args)
{
// Create a store to keep objects.
var system = await Warehouse.Put("sys", new MemoryStore());
// Create a distibuted server
var esiurServer = await Warehouse.Put("sys/server", new DistributedServer() { Port = 10001});
// Add your object to the store
var service = await Warehouse.Put("sys/agent", new Node());
// Start your server
await Warehouse.Open();
Console.WriteLine("AZ Compute Agent is running.");
}
}

53
AZ.Compute.Agent/Job.cs Normal file
View File

@@ -0,0 +1,53 @@
using Esiur.Resource;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AZ.Compute.Agent
{
[Resource]
public partial class Job
{
[Export] int id;
[Export] string name;
[Export] string command;
[Export] DateTime start = DateTime.Now;
[Export] DateTime finish;
[Export] bool finished;
[Export] object results;
[Export] float cpu;
[Export] float ram;
Process process;
public Process Process
{
get => process;
set
{
process = value;
Name = process.ProcessName;
Command = process.StartInfo.Arguments;
Id = process.Id;
process.Exited += Process_Exited;
}
}
private void Process_Exited(object? sender, EventArgs e)
{
finished = true;
finish = DateTime.Now;
}
[Export]
public void Kill()
{
process?.Kill();
}
}
}

122
AZ.Compute.Agent/Node.cs Normal file
View File

@@ -0,0 +1,122 @@
using Esiur.Core;
using Esiur.Data;
using Esiur.Resource;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Timers;
namespace AZ.Compute.Agent
{
[Resource]
public partial class Node
{
System.Timers.Timer timer;
PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;
NetworkInterface nic;
ulong totalNetworkBytes;
[Export] string id;
[Export] string ip;
[Export] float cpu;
[Export] float ram;
[Export] float network;
[Export] uint tasks;
[Export] uint cpuClock;
[Export] uint cpuMaxClock;
[Export] float networkSpeed;
[Export] Job[] jobs=new Job[0];
[Export]
public async AsyncReply<Job> Compute(string fileName, string arguments)
{
var psi = new ProcessStartInfo()
{
FileName = fileName,
Arguments = arguments
};
var p = Process.Start(psi);
var job = await Warehouse.New<Job>(fileName, null, this);
job.Process = p;
Jobs = jobs.Append(job).ToArray();
return job;
}
public Node()
{
Id = System.Environment.MachineName;
nic = NetworkInterface.GetAllNetworkInterfaces().Where(x => x.OperationalStatus == OperationalStatus.Up).First();
networkSpeed = (float)(nic.Speed / 1048576.0);
totalNetworkBytes = (ulong)nic.GetIPv4Statistics().BytesReceived + (ulong)nic.GetIPv4Statistics().BytesSent;
Ip = nic.GetIPProperties().UnicastAddresses.First(x=> x.Address.AddressFamily == AddressFamily.InterNetwork).Address.ToString();
using (ManagementObject Mo = new ManagementObject("Win32_Processor.DeviceID='CPU0'"))
{
cpuClock = (uint)(Mo["CurrentClockSpeed"]);
cpuMaxClock = (uint)(Mo["MaxClockSpeed"]);
}
//foreach (ManagementObject obj in new ManagementObjectSearcher("SELECT *, Name FROM Win32_Processor").Get())
//{
// double maxSpeed = Convert.ToDouble(obj["MaxClockSpeed"]) / 1000;
// double turboSpeed = maxSpeed * cpuValue / 100;
// return string.Format("{0} Running at {1:0.00}Ghz, Turbo Speed: {2:0.00}Ghz", obj["Name"], maxSpeed, turboSpeed);
//}
cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
timer = new System.Timers.Timer();
timer.Elapsed += Timer_Elapsed;
timer.Interval = 5000;
timer.Start();
}
private void Timer_Elapsed(object? sender, ElapsedEventArgs e)
{
try
{
var ntb = (ulong)nic.GetIPv4Statistics().BytesReceived + (ulong)nic.GetIPv4Statistics().BytesSent;
network = (float)(((ntb - totalNetworkBytes) / 5.0) / nic.Speed);
Cpu = cpuCounter.NextValue();
Ram = ramCounter.NextValue();
foreach (Job job in jobs)
{
job.Ram = (float)(job.Process.PeakWorkingSet64 / 1048576.0);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}