54 lines
1.9 KiB
C#
Raw Normal View History

using System;
using AAIntegration.SimmonsBank.API.Configs;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using AAIntegration.SimmonsBank.API.Processes;
using AAIntegration.SimmonsBank.API.Services;
namespace AAIntegration.SimmonsBank.API.Workers;
public class PuppeteerWorker : BackgroundService
{
private readonly EnvelopeFundConfig _config;
private readonly IPuppeteerProcess _puppeteerProcess;
private readonly ILogger<PuppeteerWorker> _logger;
private readonly IServiceScopeFactory _serviceScopeFactory;
public PuppeteerWorker(
IOptions<EnvelopeFundConfig> config,
IPuppeteerProcess puppeteerProcess,
ILogger<PuppeteerWorker> logger,
IServiceScopeFactory serviceScopeFactory)
{
_config = config.Value;
_puppeteerProcess = puppeteerProcess;
_logger = logger;
_serviceScopeFactory = serviceScopeFactory;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
DateTime? lastExecutedOn = null;
using (var scope = _serviceScopeFactory.CreateScope())
{
//IEnvelopeService envelopeService = scope.ServiceProvider.GetService<IEnvelopeService>();
// This is how we keep the app running (in the background)
while (!stoppingToken.IsCancellationRequested)
{
if (lastExecutedOn != null)
{
int minutesRemaining = _config.CheckIntervalInMinutes - (int)DateTime.UtcNow.Subtract(lastExecutedOn.Value).TotalMinutes;
await Task.Delay(TimeSpan.FromMinutes(minutesRemaining < 0 ? 0 : minutesRemaining), stoppingToken);
}
lastExecutedOn = DateTime.UtcNow;
_logger.LogInformation("EnvelopeFundWorker running at: {time}", DateTimeOffset.Now);
//await _puppeteerProcess.FundEnvelopes(envelopeService);
}
}
}
}