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 = 5; #else private const int GENERATE_SECONDS = 20; #endif public GeneratorService(ILogger logger) { log = logger; } /* todo: improve this it should timeout: https://stackoverflow.com/questions/23476576/cancellationtoken-timeout-vs-task-delay-and-timeout it should never stop running no matter what unless teh server shuts down */ protected override async Task ExecuteAsync(CancellationToken stoppingToken) { log.LogDebug($"GeneratorService is starting."); stoppingToken.Register(() => log.LogDebug($" GeneratorService background task is stopping.")); while (!stoppingToken.IsCancellationRequested) { if (!ServerGlobalOpsSettingsCache.BOOTING) { log.LogDebug($"GeneratorService running jobs"); //================================================================= try { await JobsBiz.ProcessJobsAsync(); } catch (Exception ex) { log.LogError(ex, "Generate::ProcessJobs result in exception error "); } //================================================================= } await Task.Delay((GENERATE_SECONDS * 1000), stoppingToken); } log.LogDebug($"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