Jobs and services
This commit is contained in:
1 parent
4fb8768807
commit
c06f06c6ce
7 files changed
+142
-5
No files matched your search
@@ -0,0 +1,74 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user