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 namespace AyaNova.Generator
{ {
//Implemented from a example here //Implemented from a example here
//https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/multi-container-microservice-net-applications/background-tasks-with-ihostedservice //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: LOOKAT: Generator tasks that should happen:
- Periodically erase any temp files written to userfiles root (attachments temp files) that are older than a day - 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 public class GeneratorService : BackgroundService
{ {
private readonly ILogger<GeneratorService> log; private readonly ILogger<GeneratorService> log;
// private readonly AyContext ct; private const int MAXIMUM_MS_ALLOWED_FOR_PROCESSING_ALL_JOBS = 1 * 60 * 1000;//1 minutes TEST TEST TEST #####
// 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 #####
#if(DEBUG) #if(DEBUG)
private const int GENERATE_SECONDS = 5; private const int GENERATE_SECONDS = 5;
#else #else
private const int GENERATE_SECONDS = 20; private const int GENERATE_SECONDS = 20;
#endif #endif
public GeneratorService(ILogger<GeneratorService> logger)
// public GeneratorService(ILogger<GeneratorService> logger, AyContext dbcontext, ApiServerState apiServerState)
// {
// ct = dbcontext;
// log = logger;
// serverState = apiServerState;
// }
public GeneratorService(ILogger<GeneratorService> logger, IServiceProvider serviceProvider)
{ {
// ct = dbcontext;
provider = serviceProvider;
log = logger; log = logger;
// serverState = apiServerState;
} }
/* /*
todo: improve this todo: improve this
it should timeout: https://stackoverflow.com/questions/23476576/cancellationtoken-timeout-vs-task-delay-and-timeout 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 it should never stop running no matter what unless teh server shuts down
*/ */
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
//don't immediately run the generator stuff on boot //don't immediately run the generator stuff on boot
bool justStarted = true; bool justStarted = true;
log.LogDebug($"GeneratorService is starting."); log.LogDebug($"GeneratorService is starting.");
stoppingToken.Register(() => stoppingToken.Register(() =>
@@ -74,11 +50,9 @@ namespace AyaNova.Generator
while (!stoppingToken.IsCancellationRequested) while (!stoppingToken.IsCancellationRequested)
{ {
if (!justStarted) if (!justStarted)
{ {
log.LogDebug($"GeneratorService task doing background work."); log.LogDebug($"GeneratorService task doing background work.");
ApiServerState serverState = ServiceProviderProvider.ServerState; ApiServerState serverState = ServiceProviderProvider.ServerState;
//================================================================= //=================================================================
try try
@@ -90,23 +64,16 @@ namespace AyaNova.Generator
else else
{ {
System.Diagnostics.Debug.WriteLine($"### GENERATE calling JobsBiz.ProcessJobs"); System.Diagnostics.Debug.WriteLine($"### GENERATE calling JobsBiz.ProcessJobs");
//Capture metrics
//Before anything capture metrics
// await CoreJobMetricsSnapshot.DoJobAsync(ct);
//Task.Run(() => CoreJobMetricsSnapshot.DoJob());
//deliberately calling this non-async
//it needs to be fast and efficient
CoreJobMetricsSnapshot.DoJob(); CoreJobMetricsSnapshot.DoJob();
//TODO: this should be big timeout and then inside the process jobs each job has it's own timeout //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 TaskUtil.WithTimeoutAfterStart(ctoken => JobsBiz.ProcessJobsAsync(ctoken), TimeSpan.FromMilliseconds(MAXIMUM_MS_ALLOWED_FOR_PROCESSING_ALL_JOBS));
// await JobsBiz.ProcessJobsAsync(ct, serverState);
System.Diagnostics.Debug.WriteLine($"### GENERATE BACK FROM calling JobsBiz.ProcessJobs"); System.Diagnostics.Debug.WriteLine($"### GENERATE BACK FROM calling JobsBiz.ProcessJobs");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
log.LogError(ex, "Generate::ProcessJobs result in exception error "); 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) public override Task StopAsync(CancellationToken stoppingToken)
{ {
log.LogDebug($"GeneratorService StopAsync triggered."); log.LogDebug($"GeneratorService StopAsync triggered.");
return Task.FromResult(0); return Task.FromResult(0);
// Run your graceful clean-up actions // Run your graceful clean-up actions
} }
} }//eoc
} }//eons