Добавил сервис для запуска процессов

This commit is contained in:
2026-07-02 14:18:17 +03:00
parent e0b13d510d
commit d5915ccf72
3 changed files with 35 additions and 0 deletions
@@ -15,7 +15,9 @@ builder.Services.AddSwaggerGen();
builder.Services.Configure<CpuMonitorSettings>(builder.Configuration.GetSection("CpuMonitorSettings"));
builder.Services.AddScoped<IProcessStarterService, ProcessStarterService>();
builder.Services.AddScoped<IMonitorService, MonitorService>();
builder.Services.AddSingleton<RawCpuMonitorService>();
builder.Services.AddSingleton<CpuMonitorBackgroundService>();
builder.Services.AddHostedService(provider => provider.GetRequiredService<CpuMonitorBackgroundService>());
@@ -0,0 +1,27 @@
using MonitorAgent.Core.Abstraction;
using System.Diagnostics;
namespace MonitorAgent.Application.Services;
public class ProcessStarterService : IProcessStarterService
{
public async Task<string> StartProcessAsync(
string fileName, string arguments, CancellationToken cancellationToken = default)
{
using var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
}
};
process.Start();
return await process.StandardOutput.ReadToEndAsync(cancellationToken);
}
}
@@ -0,0 +1,6 @@
namespace MonitorAgent.Core.Abstraction;
public interface IProcessStarterService
{
Task<string> StartProcessAsync(string fileName, string arguments, CancellationToken cancellationToken);
}