75 lines
1.9 KiB
C#
75 lines
1.9 KiB
C#
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using PaperlessAI.Models;
|
|
|
|
namespace PaperlessAI.Services;
|
|
|
|
public class DocumentProcessingService(ILogger<DocumentProcessingService> logger)
|
|
{
|
|
private Queue<PaperlessDocument> paperlessDocumentsQueue = new Queue<PaperlessDocument>();
|
|
private bool isWorking = false;
|
|
|
|
public void Enqueue(List<PaperlessDocument> paperlessDocuments)
|
|
{
|
|
if(paperlessDocumentsQueue.Count != 0)
|
|
return;
|
|
|
|
foreach(var doc in paperlessDocuments)
|
|
{
|
|
Enqueue(doc);
|
|
}
|
|
}
|
|
|
|
public void Enqueue(PaperlessDocument paperlessDocument)
|
|
{
|
|
if(paperlessDocumentsQueue.Contains(paperlessDocument, new PaperlessDocumentSameDocument()))
|
|
return;
|
|
|
|
paperlessDocumentsQueue.Enqueue(paperlessDocument);
|
|
|
|
logger.LogInformation($"Enqueued document: {paperlessDocument}");
|
|
}
|
|
|
|
public void Process()
|
|
{
|
|
if(isWorking)
|
|
return;
|
|
|
|
if(paperlessDocumentsQueue.Count == 0)
|
|
return;
|
|
|
|
if(paperlessDocumentsQueue.TryDequeue(out PaperlessDocument? docToProcess))
|
|
{
|
|
logger.LogInformation($"Dequeued: {docToProcess}");
|
|
ProcessDocument(docToProcess);
|
|
}
|
|
}
|
|
|
|
private void ProcessDocument(PaperlessDocument docToProcess)
|
|
{
|
|
logger.LogInformation($"Processing: {docToProcess}");
|
|
isWorking = true;
|
|
|
|
isWorking = false;
|
|
}
|
|
}
|
|
|
|
class PaperlessDocumentSameDocument : EqualityComparer<PaperlessDocument>
|
|
{
|
|
public override bool Equals(PaperlessDocument? x, PaperlessDocument? y)
|
|
{
|
|
if (x == null && y == null)
|
|
return true;
|
|
else if (x == null || y == null)
|
|
return false;
|
|
|
|
return x.Id == y.Id || x.DocumentId == y.DocumentId;
|
|
}
|
|
|
|
public override int GetHashCode([DisallowNull] PaperlessDocument obj)
|
|
{
|
|
var hcode = obj.Id + obj.DocumentId;
|
|
return hcode.GetHashCode();
|
|
}
|
|
}
|