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,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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user