emit insin telemetry for job outcomes in PrinterQueue

This commit is contained in:
EugeneTes
2026-07-23 08:03:32 +00:00
parent 050616b595
commit e70126be2b
2 changed files with 27 additions and 4 deletions

View File

@@ -8,18 +8,19 @@ public class PrintServer
private readonly IPrintService _printService;
private readonly ILogger _logger;
private readonly IJobStatusReporter _statusReporter;
private readonly Inspectron.Epson.PrintServer.Telemetry.IInsinTelemetry _telemetry;
private readonly ConcurrentDictionary<string, PrinterQueue> _printerQueues;
private readonly SemaphoreSlim _printDiscoveryLock = new(1, 1);
private int _readerCount = 0;
private readonly object _readerCountLock = new();
public PrintServer(IPrintService printService, ILogger logger, IJobStatusReporter statusReporter)
public PrintServer(IPrintService printService, ILogger logger, IJobStatusReporter statusReporter, Inspectron.Epson.PrintServer.Telemetry.IInsinTelemetry telemetry)
{
_printService = printService;
_logger = logger;
_statusReporter = statusReporter;
_telemetry = telemetry;
_printerQueues = new ConcurrentDictionary<string, PrinterQueue>();
}
/// <summary>
@@ -80,7 +81,7 @@ public class PrintServer
public void RegisterPrinter(string printerIp)
{
var queue = new PrinterQueue(printerIp, _printService, _logger, this, _statusReporter);
var queue = new PrinterQueue(printerIp, _printService, _logger, this, _statusReporter, _telemetry);
if (_printerQueues.TryAdd(printerIp, queue))
{
queue.Start();

View File

@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using Inspectron.Epson.PrintServer.Telemetry;
using Microsoft.Extensions.Logging;
namespace Inspectron.Epson.Queue;
@@ -15,11 +16,12 @@ public class PrinterQueue
private readonly ILogger _logger;
private readonly PrintServer _printServer;
private readonly IJobStatusReporter _statusReporter;
private readonly IInsinTelemetry _telemetry;
public bool IsProcessing { get; private set; }
public int QueueLength => _queue.Count + _priorityQueue.Count;
public PrinterQueue(string printerIp, IPrintService printService, ILogger logger, PrintServer printServer, IJobStatusReporter statusReporter)
public PrinterQueue(string printerIp, IPrintService printService, ILogger logger, PrintServer printServer, IJobStatusReporter statusReporter, IInsinTelemetry telemetry)
{
PrinterIp = printerIp;
_queue = new ConcurrentQueue<PrintJob>();
@@ -30,6 +32,7 @@ public class PrinterQueue
_logger = logger;
_printServer = printServer;
_statusReporter = statusReporter;
_telemetry = telemetry;
}
public void Enqueue(PrintJob job)
@@ -74,13 +77,22 @@ public class PrinterQueue
_logger.LogInformation("Processing job on printer {PrinterId}", PrinterIp);
_printServer.EnterPrintLock();
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
try
{
// Call the actual print function
var result = await _printService.PrintAsync(PrinterIp, job);
stopwatch.Stop();
if (!result.Success)
{
var failKind = InsinEventKinds.MapErrorTypeToKind(result.ErrorType);
SafeEmit(failKind, InsinMessageFormatter.Format(
("printer", PrinterIp),
("receiptType", job.Document?.ReceiptType.ToString()),
("retry", job.RetryCount.ToString()),
("error", result.ErrorType.ToString())));
if (result.ErrorType == PrintErrorType.ConversionError)
{
_logger.LogWarning("Job failed due to conversion error, not re-queuing");
@@ -108,6 +120,10 @@ public class PrinterQueue
{
_logger.LogInformation("Job completed successfully on printer {PrinterId}", PrinterIp);
await _statusReporter.ReportStatusAsync(job, PrintJobStatus.Completed);
SafeEmit(InsinEventKinds.JobPrinted, InsinMessageFormatter.Format(
("printer", PrinterIp),
("receiptType", job.Document?.ReceiptType.ToString()),
("durationMs", stopwatch.ElapsedMilliseconds.ToString())));
}
}
catch (Exception ex)
@@ -146,4 +162,10 @@ public class PrinterQueue
{
return new List<PrintJob>(_queue);
}
private void SafeEmit(string kind, string message)
{
try { _telemetry.Emit(kind, message); }
catch (Exception ex) { _logger.LogWarning(ex, "insin telemetry emit failed"); }
}
}