job status reporting
This commit is contained in:
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 ILogger _logger;
|
||||
private readonly IJobStatusReporter _statusReporter;
|
||||
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)
|
||||
public PrintServer(IPrintService printService, ILogger logger, IJobStatusReporter statusReporter)
|
||||
{
|
||||
_printService = printService;
|
||||
_logger = logger;
|
||||
_statusReporter = statusReporter;
|
||||
_printerQueues = new ConcurrentDictionary<string, PrinterQueue>();
|
||||
|
||||
}
|
||||
@@ -76,7 +78,7 @@ public class PrintServer
|
||||
|
||||
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))
|
||||
{
|
||||
queue.Start();
|
||||
|
||||
@@ -16,11 +16,12 @@ public class PrinterQueue
|
||||
private readonly IPrintService _printService;
|
||||
private readonly ILogger _logger;
|
||||
private readonly PrintServer _printServer;
|
||||
private readonly IJobStatusReporter _statusReporter;
|
||||
|
||||
public bool IsProcessing { get; private set; }
|
||||
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;
|
||||
_queue = new ConcurrentQueue<PrintJob>();
|
||||
@@ -30,12 +31,14 @@ public class PrinterQueue
|
||||
_printService = printService;
|
||||
_logger = logger;
|
||||
_printServer = printServer;
|
||||
_statusReporter = statusReporter;
|
||||
}
|
||||
|
||||
public void Enqueue(PrintJob job)
|
||||
{
|
||||
_queue.Enqueue(job);
|
||||
_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);
|
||||
}
|
||||
|
||||
@@ -83,6 +86,7 @@ public class PrinterQueue
|
||||
if (result.ErrorType == PrintErrorType.ConversionError)
|
||||
{
|
||||
_logger.LogWarning("Job failed due to conversion error, not re-queuing");
|
||||
await _statusReporter.ReportStatusAsync(job, PrintJobStatus.Failed);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -96,12 +100,13 @@ public class PrinterQueue
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("Job completed successfully on printer {PrinterId}", PrinterIp);
|
||||
await _statusReporter.ReportStatusAsync(job, PrintJobStatus.Completed);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error processing job");
|
||||
// Handle exception (retry, log, etc.)
|
||||
await _statusReporter.ReportStatusAsync(job, PrintJobStatus.Failed);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user