job status reporting
This commit is contained in:
@@ -44,6 +44,7 @@ public class PrintServerHostedService : IHostedService
|
|||||||
_kernel.Bind<IAssignedPrinterRepository>().ToConstant(config);
|
_kernel.Bind<IAssignedPrinterRepository>().ToConstant(config);
|
||||||
_kernel.Bind<IWrapperPrinterFactory>().To<PrinterWrapperFactory>().InSingletonScope();
|
_kernel.Bind<IWrapperPrinterFactory>().To<PrinterWrapperFactory>().InSingletonScope();
|
||||||
_kernel.Bind<IPrinterConfigurationSource>().ToConstant(config);
|
_kernel.Bind<IPrinterConfigurationSource>().ToConstant(config);
|
||||||
|
_kernel.Bind<IJobStatusReporter>().To<NullJobStatusReporter>();
|
||||||
_kernel.Bind<PrintServer>().ToSelf().InSingletonScope();
|
_kernel.Bind<PrintServer>().ToSelf().InSingletonScope();
|
||||||
|
|
||||||
// Get instances
|
// Get instances
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ kernel.Bind<ILogger>().ToMethod(ctx =>
|
|||||||
kernel.Bind<IWrapperPrinterFactory>().To<PrinterWrapperFactory>().InSingletonScope();
|
kernel.Bind<IWrapperPrinterFactory>().To<PrinterWrapperFactory>().InSingletonScope();
|
||||||
kernel.Bind<IPrinterConfigurationSource>().To<FixedConfigurationSource>();
|
kernel.Bind<IPrinterConfigurationSource>().To<FixedConfigurationSource>();
|
||||||
kernel.Bind<IReceiptConverterFactory>().To<ReceiptConverterFactory>().InSingletonScope();
|
kernel.Bind<IReceiptConverterFactory>().To<ReceiptConverterFactory>().InSingletonScope();
|
||||||
|
kernel.Bind<IJobStatusReporter>().To<NullJobStatusReporter>();
|
||||||
kernel.Bind<PrintServer>().ToSelf().InSingletonScope();
|
kernel.Bind<PrintServer>().ToSelf().InSingletonScope();
|
||||||
kernel.Bind<IDiscoveredPrintersReceiver>().To<JamesDiscoveredPrintersReceiver>().InSingletonScope();
|
kernel.Bind<IDiscoveredPrintersReceiver>().To<JamesDiscoveredPrintersReceiver>().InSingletonScope();
|
||||||
kernel.Bind<PrinterDiscoveryBackgroundTask>().ToSelf().InSingletonScope();
|
kernel.Bind<PrinterDiscoveryBackgroundTask>().ToSelf().InSingletonScope();
|
||||||
|
|||||||
6
Inspectron.Epson/Queue/IJobStatusReporter.cs
Normal file
6
Inspectron.Epson/Queue/IJobStatusReporter.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
public interface IJobStatusReporter
|
||||||
|
{
|
||||||
|
Task ReportStatusAsync(PrintJob job, PrintJobStatus status);
|
||||||
|
}
|
||||||
6
Inspectron.Epson/Queue/NullJobStatusReporter.cs
Normal file
6
Inspectron.Epson/Queue/NullJobStatusReporter.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
public class NullJobStatusReporter : IJobStatusReporter
|
||||||
|
{
|
||||||
|
public Task ReportStatusAsync(PrintJob job, PrintJobStatus status) => Task.CompletedTask;
|
||||||
|
}
|
||||||
6
Inspectron.Epson/Queue/PrintJobStatus.cs
Normal file
6
Inspectron.Epson/Queue/PrintJobStatus.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
public enum PrintJobStatus
|
||||||
|
{
|
||||||
|
Received,
|
||||||
|
Completed,
|
||||||
|
Failed
|
||||||
|
}
|
||||||
@@ -5,15 +5,17 @@ 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 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)
|
public PrintServer(IPrintService printService, ILogger logger, IJobStatusReporter statusReporter)
|
||||||
{
|
{
|
||||||
_printService = printService;
|
_printService = printService;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_statusReporter = statusReporter;
|
||||||
_printerQueues = new ConcurrentDictionary<string, PrinterQueue>();
|
_printerQueues = new ConcurrentDictionary<string, PrinterQueue>();
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -76,7 +78,7 @@ public class PrintServer
|
|||||||
|
|
||||||
public void RegisterPrinter(string printerIp)
|
public void RegisterPrinter(string printerIp)
|
||||||
{
|
{
|
||||||
var queue = new PrinterQueue(printerIp, _printService, _logger, this);
|
var queue = new PrinterQueue(printerIp, _printService, _logger, this, _statusReporter);
|
||||||
if (_printerQueues.TryAdd(printerIp, queue))
|
if (_printerQueues.TryAdd(printerIp, queue))
|
||||||
{
|
{
|
||||||
queue.Start();
|
queue.Start();
|
||||||
|
|||||||
@@ -16,11 +16,12 @@ public class PrinterQueue
|
|||||||
private readonly IPrintService _printService;
|
private readonly IPrintService _printService;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly PrintServer _printServer;
|
private readonly PrintServer _printServer;
|
||||||
|
private readonly IJobStatusReporter _statusReporter;
|
||||||
|
|
||||||
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)
|
public PrinterQueue(string printerIp, IPrintService printService, ILogger logger, PrintServer printServer, IJobStatusReporter statusReporter)
|
||||||
{
|
{
|
||||||
PrinterIp = printerIp;
|
PrinterIp = printerIp;
|
||||||
_queue = new ConcurrentQueue<PrintJob>();
|
_queue = new ConcurrentQueue<PrintJob>();
|
||||||
@@ -30,12 +31,14 @@ public class PrinterQueue
|
|||||||
_printService = printService;
|
_printService = printService;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_printServer = printServer;
|
_printServer = printServer;
|
||||||
|
_statusReporter = statusReporter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Enqueue(PrintJob job)
|
public void Enqueue(PrintJob job)
|
||||||
{
|
{
|
||||||
_queue.Enqueue(job);
|
_queue.Enqueue(job);
|
||||||
_signal.Release(); // Signal that there's work to do
|
_signal.Release(); // Signal that there's work to do
|
||||||
|
_statusReporter.ReportStatusAsync(job, PrintJobStatus.Received).GetAwaiter().GetResult();
|
||||||
_logger.LogInformation("Job queued for printer {PrinterId}. Queue length: {QueueLength}", PrinterIp, QueueLength);
|
_logger.LogInformation("Job queued for printer {PrinterId}. Queue length: {QueueLength}", PrinterIp, QueueLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,6 +86,7 @@ public class PrinterQueue
|
|||||||
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");
|
||||||
|
await _statusReporter.ReportStatusAsync(job, PrintJobStatus.Failed);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -96,12 +100,13 @@ public class PrinterQueue
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Job completed successfully on printer {PrinterId}", PrinterIp);
|
_logger.LogInformation("Job completed successfully on printer {PrinterId}", PrinterIp);
|
||||||
|
await _statusReporter.ReportStatusAsync(job, PrintJobStatus.Completed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error processing job");
|
_logger.LogError(ex, "Error processing job");
|
||||||
// Handle exception (retry, log, etc.)
|
await _statusReporter.ReportStatusAsync(job, PrintJobStatus.Failed);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user