emit insin telemetry for job outcomes in PrinterQueue
This commit is contained in:
@@ -8,18 +8,19 @@ public class PrintServer
|
|||||||
private readonly IPrintService _printService;
|
private readonly IPrintService _printService;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly IJobStatusReporter _statusReporter;
|
private readonly IJobStatusReporter _statusReporter;
|
||||||
|
private readonly Inspectron.Epson.PrintServer.Telemetry.IInsinTelemetry _telemetry;
|
||||||
private readonly ConcurrentDictionary<string, PrinterQueue> _printerQueues;
|
private readonly ConcurrentDictionary<string, PrinterQueue> _printerQueues;
|
||||||
private readonly SemaphoreSlim _printDiscoveryLock = new(1, 1);
|
private readonly SemaphoreSlim _printDiscoveryLock = new(1, 1);
|
||||||
private int _readerCount = 0;
|
private int _readerCount = 0;
|
||||||
private readonly object _readerCountLock = new();
|
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;
|
_printService = printService;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_statusReporter = statusReporter;
|
_statusReporter = statusReporter;
|
||||||
|
_telemetry = telemetry;
|
||||||
_printerQueues = new ConcurrentDictionary<string, PrinterQueue>();
|
_printerQueues = new ConcurrentDictionary<string, PrinterQueue>();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -80,7 +81,7 @@ public class PrintServer
|
|||||||
|
|
||||||
public void RegisterPrinter(string printerIp)
|
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))
|
if (_printerQueues.TryAdd(printerIp, queue))
|
||||||
{
|
{
|
||||||
queue.Start();
|
queue.Start();
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
using Inspectron.Epson.PrintServer.Telemetry;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace Inspectron.Epson.Queue;
|
namespace Inspectron.Epson.Queue;
|
||||||
@@ -15,11 +16,12 @@ public class PrinterQueue
|
|||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly PrintServer _printServer;
|
private readonly PrintServer _printServer;
|
||||||
private readonly IJobStatusReporter _statusReporter;
|
private readonly IJobStatusReporter _statusReporter;
|
||||||
|
private readonly IInsinTelemetry _telemetry;
|
||||||
|
|
||||||
public bool IsProcessing { get; private set; }
|
public bool IsProcessing { get; private set; }
|
||||||
public int QueueLength => _queue.Count + _priorityQueue.Count;
|
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;
|
PrinterIp = printerIp;
|
||||||
_queue = new ConcurrentQueue<PrintJob>();
|
_queue = new ConcurrentQueue<PrintJob>();
|
||||||
@@ -30,6 +32,7 @@ public class PrinterQueue
|
|||||||
_logger = logger;
|
_logger = logger;
|
||||||
_printServer = printServer;
|
_printServer = printServer;
|
||||||
_statusReporter = statusReporter;
|
_statusReporter = statusReporter;
|
||||||
|
_telemetry = telemetry;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Enqueue(PrintJob job)
|
public void Enqueue(PrintJob job)
|
||||||
@@ -74,13 +77,22 @@ public class PrinterQueue
|
|||||||
_logger.LogInformation("Processing job on printer {PrinterId}", PrinterIp);
|
_logger.LogInformation("Processing job on printer {PrinterId}", PrinterIp);
|
||||||
|
|
||||||
_printServer.EnterPrintLock();
|
_printServer.EnterPrintLock();
|
||||||
|
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Call the actual print function
|
// Call the actual print function
|
||||||
var result = await _printService.PrintAsync(PrinterIp, job);
|
var result = await _printService.PrintAsync(PrinterIp, job);
|
||||||
|
stopwatch.Stop();
|
||||||
|
|
||||||
if (!result.Success)
|
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)
|
if (result.ErrorType == PrintErrorType.ConversionError)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Job failed due to conversion error, not re-queuing");
|
_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);
|
_logger.LogInformation("Job completed successfully on printer {PrinterId}", PrinterIp);
|
||||||
await _statusReporter.ReportStatusAsync(job, PrintJobStatus.Completed);
|
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)
|
catch (Exception ex)
|
||||||
@@ -146,4 +162,10 @@ public class PrinterQueue
|
|||||||
{
|
{
|
||||||
return new List<PrintJob>(_queue);
|
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"); }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user