Add project files.
This commit is contained in:
53
Inspectron.Epson/PrintServer/PrintLoop.cs
Normal file
53
Inspectron.Epson/PrintServer/PrintLoop.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Inspectron.Epson.PrintServer.PrintServices;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Inspectron.Epson.PrintServer;
|
||||
|
||||
public class PrintLoop
|
||||
{
|
||||
private readonly global::PrintServer _printServer;
|
||||
private readonly IPrintService _printService;
|
||||
private readonly IPrintJobSource _jobSource;
|
||||
private readonly IAssignedPrinterRepository _assignedPrinterRepository;
|
||||
private readonly ILogger _logger;
|
||||
private CancellationTokenSource? _cancellationSource;
|
||||
private CancellationToken _cancellationToken;
|
||||
|
||||
public PrintLoop(global::PrintServer printServer,IPrintService printService, IPrintJobSource jobSource, IAssignedPrinterRepository assignedPrinterRepository, ILogger logger)
|
||||
{
|
||||
_printServer = printServer;
|
||||
_printService = printService;
|
||||
_jobSource = jobSource;
|
||||
_assignedPrinterRepository = assignedPrinterRepository;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task StartAsync()
|
||||
{
|
||||
_cancellationSource = new CancellationTokenSource();
|
||||
_cancellationToken = _cancellationSource.Token;
|
||||
_= Task.Run(Loop);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
public async Task Loop()
|
||||
{
|
||||
while (!_cancellationSource!.Token.IsCancellationRequested)
|
||||
{
|
||||
var job = await _jobSource.GetNextJobAsync(_cancellationToken);
|
||||
var assigned = _assignedPrinterRepository.GetAssignedPrinter(job.AreaId);
|
||||
if (assigned == null)
|
||||
{
|
||||
_logger.LogWarning("Received print job for unassigned area {AreaId}, skipping", job.AreaId);
|
||||
continue;
|
||||
}
|
||||
|
||||
_printServer.SubmitJob(assigned, job);
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync()
|
||||
{
|
||||
_cancellationSource!.Cancel();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user