mirror of
https://gitlab.com/Hukuta47/cartrix.git
synced 2026-12-06 13:00:55 +00:00
srfedgdfgh
This commit is contained in:
1 parent
2883956264
commit
4a0ac97c24
9 files changed
+225
-5
No files matched your search
@@ -1,16 +1,39 @@
|
||||
using CartriX.Share.Grpc;
|
||||
using Grpc.Net.Client;
|
||||
|
||||
namespace CartriX.Service;
|
||||
|
||||
public class Worker(ILogger<Worker> logger) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var serverAddress = "http://localhost:5054";
|
||||
var random = new Random();
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
try
|
||||
{
|
||||
logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
using var channel = GrpcChannel.ForAddress(serverAddress);
|
||||
var client = new PrinterMonitor.PrinterMonitorClient(channel);
|
||||
|
||||
var response = await client.SendStatsAsync(new PrinterStatsRequest
|
||||
{
|
||||
PrinterId = 1,
|
||||
PrinterName = "USB-Lab-01",
|
||||
PageCount = random.Next(1000, 2000),
|
||||
TonerPercent = random.Next(10, 100),
|
||||
Source = "agent-usb"
|
||||
}, cancellationToken: stoppingToken);
|
||||
|
||||
logger.LogInformation("Agent sent stats: {Message}", response.Message);
|
||||
}
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Agent cannot send stats to server yet.");
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user