Added the file structure for the Puppeteer Worker and Process
This commit is contained in:
parent
aa1c63f42d
commit
6ff4afd4a6
@ -0,0 +1,9 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using AAIntegration.SimmonsBank.API.Services;
|
||||||
|
|
||||||
|
namespace AAIntegration.SimmonsBank.API.Processes;
|
||||||
|
|
||||||
|
public interface IPuppeteerProcess
|
||||||
|
{
|
||||||
|
Task FundEnvelopes();
|
||||||
|
}
|
53
AAIntegration.SimmonsBank.API/Processes/PuppeteerProcess.cs
Normal file
53
AAIntegration.SimmonsBank.API/Processes/PuppeteerProcess.cs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AAIntegration.SimmonsBank.API.Configs;
|
||||||
|
using AAIntegration.SimmonsBank.API.Entities;
|
||||||
|
using AAIntegration.SimmonsBank.API.Models.Transactions;
|
||||||
|
using AAIntegration.SimmonsBank.API.Services;
|
||||||
|
using Internal;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
namespace AAIntegration.SimmonsBank.API.Processes;
|
||||||
|
|
||||||
|
public class PuppeteerProcess : IPuppeteerProcess
|
||||||
|
{
|
||||||
|
private readonly EnvelopeFundConfig _config;
|
||||||
|
private readonly ILogger<PuppeteerProcess> _logger;
|
||||||
|
|
||||||
|
public PuppeteerProcess(
|
||||||
|
IOptions<EnvelopeFundConfig> config,
|
||||||
|
ILogger<PuppeteerProcess> logger)
|
||||||
|
{
|
||||||
|
_config = config.Value;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task FundEnvelopes()
|
||||||
|
{
|
||||||
|
_logger.LogInformation($"Starting envelope funding...");
|
||||||
|
|
||||||
|
// Grab envelopes
|
||||||
|
/*List<Envelope> envelopes = envelopeService.GetAll().ToList();
|
||||||
|
|
||||||
|
bool anyTriggered = false;
|
||||||
|
|
||||||
|
foreach (Envelope env in envelopes)
|
||||||
|
{
|
||||||
|
// Check if needs triggering
|
||||||
|
if (env.Enabled && !envelopeService.HasBeenFundedThisMonth(env.Id))
|
||||||
|
{
|
||||||
|
_logger.LogInformation($"Envelope '{env.Name}' (id={env.Id}) needs funding - Has now been triggered.");
|
||||||
|
anyTriggered = true;
|
||||||
|
|
||||||
|
// Fund Envelope
|
||||||
|
envelopeService.FundEnvelopeForThisMonth(env.Id);
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
if (false)
|
||||||
|
_logger.LogInformation($"No action taken.");
|
||||||
|
else
|
||||||
|
_logger.LogInformation($"Envelope funding complete.");
|
||||||
|
}
|
||||||
|
}
|
@ -11,6 +11,8 @@ using Microsoft.IdentityModel.Tokens;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
//using Microsoft.Build.Framework;
|
//using Microsoft.Build.Framework;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using AAIntegration.SimmonsBank.API.Processes;
|
||||||
|
using AAIntegration.SimmonsBank.API.Workers;
|
||||||
|
|
||||||
internal class Program
|
internal class Program
|
||||||
{
|
{
|
||||||
@ -85,6 +87,10 @@ internal class Program
|
|||||||
|
|
||||||
builder.Services.AddScoped<ApiKeyAuthenticationHandler>();
|
builder.Services.AddScoped<ApiKeyAuthenticationHandler>();
|
||||||
|
|
||||||
|
builder.Services.AddSingleton<IPuppeteerProcess, PuppeteerProcess>();
|
||||||
|
|
||||||
|
builder.Services.AddHostedService<PuppeteerWorker>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Apply Database Migrations - This is NOT recommended for multi-node deployment!!!
|
// Apply Database Migrations - This is NOT recommended for multi-node deployment!!!
|
||||||
|
53
AAIntegration.SimmonsBank.API/Workers/PuppeteerWorker.cs
Normal file
53
AAIntegration.SimmonsBank.API/Workers/PuppeteerWorker.cs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user