This commit is contained in:
2020-05-25 21:24:23 +00:00
parent bbd64e2618
commit 9fee1f2dbd

View File

@@ -10,11 +10,9 @@ 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
@@ -24,49 +22,27 @@ namespace AyaNova.Generator
public class GeneratorService : BackgroundService
{
private readonly ILogger<GeneratorService> log;
// private readonly AyContext ct;
// private readonly ApiServerState serverState;
private readonly IServiceProvider provider;
private const int MAXIMUM_TIME_ALLOWED_FOR_PROCESSING_ALL_JOBS = 1 * 60 * 1000;//1 minutes TEST TEST TEST #####
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<GeneratorService> logger, AyContext dbcontext, ApiServerState apiServerState)
// {
// ct = dbcontext;
// log = logger;
// serverState = apiServerState;
// }
public GeneratorService(ILogger<GeneratorService> logger, IServiceProvider serviceProvider)
public GeneratorService(ILogger<GeneratorService> logger)
{
// ct = dbcontext;
provider = serviceProvider;
log = logger;
// serverState = apiServerState;
}
/*
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)
{
//don't immediately run the generator stuff on boot
bool justStarted = true;
log.LogDebug($"GeneratorService is starting.");
stoppingToken.Register(() =>
@@ -74,11 +50,9 @@ namespace AyaNova.Generator
while (!stoppingToken.IsCancellationRequested)
{
if (!justStarted)
{
log.LogDebug($"GeneratorService task doing background work.");
ApiServerState serverState = ServiceProviderProvider.ServerState;
//=================================================================
try
@@ -90,23 +64,16 @@ namespace AyaNova.Generator
else
{
System.Diagnostics.Debug.WriteLine($"### GENERATE calling JobsBiz.ProcessJobs");
//Before anything capture metrics
// await CoreJobMetricsSnapshot.DoJobAsync(ct);
//Task.Run(() => CoreJobMetricsSnapshot.DoJob());
//deliberately calling this non-async
//it needs to be fast and efficient
//Capture metrics
CoreJobMetricsSnapshot.DoJob();
//TODO: this should be big timeout and then inside the process jobs each job has it's own timeout
await TaskUtil.WithTimeoutAfterStart(ctoken => JobsBiz.ProcessJobsAsync(ctoken), TimeSpan.FromMilliseconds(MAXIMUM_TIME_ALLOWED_FOR_PROCESSING_ALL_JOBS));
// await JobsBiz.ProcessJobsAsync(ct, serverState);
await TaskUtil.WithTimeoutAfterStart(ctoken => JobsBiz.ProcessJobsAsync(ctoken), TimeSpan.FromMilliseconds(MAXIMUM_MS_ALLOWED_FOR_PROCESSING_ALL_JOBS));
System.Diagnostics.Debug.WriteLine($"### GENERATE BACK FROM calling JobsBiz.ProcessJobs");
}
}
catch (Exception ex)
{
log.LogError(ex, "Generate::ProcessJobs result in exception error ");
}
//=================================================================
@@ -119,21 +86,12 @@ namespace AyaNova.Generator
}
//originally but kept getting compiler error
// public override async Task StopAsync(CancellationToken stoppingToken)
// {
// log.LogDebug($"GeneratorService StopAsync triggered.");
// // Run your graceful clean-up actions
// }
public override Task StopAsync(CancellationToken stoppingToken)
{
log.LogDebug($"GeneratorService StopAsync triggered.");
return Task.FromResult(0);
// Run your graceful clean-up actions
}
}
}//eoc
}
}//eons