Files
PaperlessAI/Jobs/CheckDocumentsToProcessJob.cs
T
2026-10-10 01:35:56 +02:00

41 lines
1.3 KiB
C#

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();
}
}