using System.Threading; using System; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using AyaNova.Biz; using AyaNova.Util; namespace AyaNova.Generator { //Implemented from a example here //https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/multi-container-microservice-net-applications/background-tasks-with-ihostedservice /* LOOKAT: Generator tasks that should happen: - Periodically erase any temp files written to userfiles root (attachments temp files) that are older than a day - These files should be normally erased within seconds after uploading and processing into their permanent folder but shit will go wrong */ public class GeneratorService : BackgroundService { private readonly ILogger log; // private const int MAXIMUM_MS_ALLOWED_FOR_PROCESSING_ALL_JOBS = 1 * 60 * 1000;//1 minutes TEST TEST TEST ##### // #if (DEBUG) // private const int GENERATE_SECONDS = 1; // #else // private const int GENERATE_SECONDS = 20; // #endif public GeneratorService(ILogger logger) { log = logger; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { log.LogInformation($"GeneratorService is starting."); stoppingToken.Register(() => log.LogDebug($" GeneratorService background task is stopping.")); while (!stoppingToken.IsCancellationRequested) { if (!ServerGlobalOpsSettingsCache.BOOTING) { // log.LogTrace($"GeneratorService running jobs"); //================================================================= try { await JobsBiz.ProcessJobsAsync(); } catch (Exception ex) { log.LogError(ex, "Generate::ProcessJobs result in exception error "); } //================================================================= } //There *MUST* be a delay here or the server will come to a standstill //ideally need to move on from this method of generation to something external or more reasonable but //this does work, we need to get to release, for our projected use load this should be fine and it's //a documented by MS method to do asp.net core job processing //https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-6.0&tabs=visual-studio await Task.Delay(1000); } log.LogInformation($"GeneratorService is stopping"); } public override Task StopAsync(CancellationToken stoppingToken) { log.LogDebug($"GeneratorService StopAsync triggered"); return Task.FromResult(0); // Run any needed clean-up actions } }//eoc }//eons