Jobs and services
This commit is contained in:
1 parent
4fb8768807
commit
c06f06c6ce
7 files changed
+142
-5
No files matched your search
@@ -1,11 +1,9 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace PaperlessAI.Bodies;
|
||||
|
||||
public class NewPaperlessDocumentBody
|
||||
{
|
||||
[JsonProperty("document_id")]
|
||||
public long DocumentId { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Frozen;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PaperlessAI.Context;
|
||||
using PaperlessAI.Repositories;
|
||||
using PaperlessAI.Services;
|
||||
|
||||
namespace PaperlessAI.Jobs;
|
||||
|
||||
public class CheckDocumentsToProcessJob(ILogger<CheckDocumentsToProcessJob> logger, IServiceScopeFactory scopeFactory) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(5));
|
||||
|
||||
do
|
||||
{
|
||||
await DoWork(stoppingToken);
|
||||
}
|
||||
while(await timer.WaitForNextTickAsync(stoppingToken));
|
||||
}
|
||||
|
||||
private async Task DoWork(CancellationToken stoppingToken)
|
||||
{
|
||||
logger.LogInformation("Working");
|
||||
|
||||
await using var scope = scopeFactory.CreateAsyncScope();
|
||||
var context = scope.ServiceProvider.GetRequiredService<MyDbContext>();
|
||||
var documentProcessingService = scope.ServiceProvider.GetRequiredService<DocumentProcessingService>();
|
||||
|
||||
var docsToProcess = await context
|
||||
.PaperlessDocuments
|
||||
.Where(x => x.DocumentState == Enums.EOperationState.Created)
|
||||
.OrderBy(x => x.CreatedAt)
|
||||
.ToListAsync();
|
||||
|
||||
documentProcessingService.Enqueue(docsToProcess);
|
||||
documentProcessingService.Process();
|
||||
}
|
||||
}
|
||||
@@ -21,4 +21,9 @@ public class PaperlessDocument
|
||||
return paperlessDocument;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Id: {Id}, DocumentId: {DocumentId}";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PaperlessAI.Context;
|
||||
using PaperlessAI.Jobs;
|
||||
using PaperlessAI.Repositories;
|
||||
using PaperlessAI.Services;
|
||||
using Serilog;
|
||||
@@ -8,6 +9,7 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Services
|
||||
builder.Services.AddScoped<PaperlessDocumentService>();
|
||||
builder.Services.AddSingleton<DocumentProcessingService>();
|
||||
|
||||
// Repositories
|
||||
builder.Services.AddScoped<PaperlessDocumentRepository>();
|
||||
@@ -39,6 +41,9 @@ Log.Logger = new LoggerConfiguration()
|
||||
|
||||
builder.Host.UseSerilog();
|
||||
|
||||
// Jobs
|
||||
builder.Services.AddHostedService<CheckDocumentsToProcessJob>();
|
||||
|
||||
// App
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace PaperlessAI.Services;
|
||||
|
||||
public class LLMAgentService
|
||||
{
|
||||
|
||||
}
|
||||
@@ -8,10 +8,17 @@
|
||||
}
|
||||
},
|
||||
"Application": {
|
||||
"Version": "<VERSION>"
|
||||
"Version": "<Version>"
|
||||
},
|
||||
"Settings": {
|
||||
"MaxMediaDuration": 7200
|
||||
"LlmAgent": {
|
||||
"Url": "<URL>",
|
||||
"Token": "<Token>",
|
||||
"Model": "<Model>",
|
||||
"McpServer": "<McpServer>",
|
||||
"ContextLength": "<ContextLength>",
|
||||
"MaxOutputTokens": "<MaxOutputTokens>",
|
||||
"Temperature": "<Temperature>",
|
||||
"Reasoning": "<Reasoning>"
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"MariaDbConnectionString": "server=<IP>;user id=<USER>;password=<PASSWORD>;database=<DATABASE>"
|
||||
|
||||
Reference in new issue
Block a user