feat: scaffolding for background tasks complete

This commit is contained in:
Michael Green
2023-03-17 23:08:46 +11:00
parent 55735599f8
commit 5260738a5b
9 changed files with 397 additions and 35 deletions

View File

@@ -1,4 +1,5 @@
using System;
using gaseous_tools;
namespace gaseous_server
{
@@ -6,17 +7,18 @@ namespace gaseous_server
public class TimedHostedService : IHostedService, IDisposable
{
private int executionCount = 0;
private readonly ILogger<TimedHostedService> _logger;
//private readonly ILogger<TimedHostedService> _logger;
private Timer _timer;
public TimedHostedService(ILogger<TimedHostedService> logger)
{
_logger = logger;
}
//public TimedHostedService(ILogger<TimedHostedService> logger)
//{
// _logger = logger;
//}
public Task StartAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Timed Hosted Service running.");
//_logger.LogInformation("Timed Hosted Service running.");
Logging.Log(Logging.LogType.Debug, "Background", "Starting background task monitor");
_timer = new Timer(DoWork, null, TimeSpan.Zero,
TimeSpan.FromSeconds(5));
@@ -28,13 +30,20 @@ namespace gaseous_server
{
var count = Interlocked.Increment(ref executionCount);
_logger.LogInformation(
"Timed Hosted Service is working. Count: {Count}", count);
//_logger.LogInformation(
// "Timed Hosted Service is working. Count: {Count}", count);
foreach (ProcessQueue.QueueItem qi in ProcessQueue.QueueItems) {
if (DateTime.UtcNow > qi.NextRunTime || qi.Force == true) {
qi.Execute();
}
}
}
public Task StopAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Timed Hosted Service is stopping.");
//_logger.LogInformation("Timed Hosted Service is stopping.");
Logging.Log(Logging.LogType.Debug, "Background", "Stopping background task monitor");
_timer?.Change(Timeout.Infinite, 0);