feat: add phase five operations foundation
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using Tiku.Application.Jobs;
|
||||
using Tiku.Application.Security;
|
||||
using Tiku.Application.Tenancy;
|
||||
|
||||
@@ -9,21 +10,22 @@ public class Worker(
|
||||
IOptions<DomainLifecycleOptions> options,
|
||||
ILogger<Worker> logger) : BackgroundService
|
||||
{
|
||||
private readonly string workerId = $"{Environment.MachineName}:{Guid.NewGuid():N}";
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var scope = scopeFactory.CreateAsyncScope();
|
||||
scope.ServiceProvider.GetRequiredService<ITenantContextInitializer>()
|
||||
.InitializeSystem(null, "Tenant domain DNS and TLS lifecycle worker");
|
||||
var processed = await scope.ServiceProvider
|
||||
.GetRequiredService<ITenantDomainLifecycleService>()
|
||||
.ProcessPendingAsync(stoppingToken);
|
||||
if (processed > 0)
|
||||
var domainCount = await ProcessTenantDomainsAsync(stoppingToken);
|
||||
var jobCount = await ProcessBackgroundJobsAsync(stoppingToken);
|
||||
if (domainCount > 0 || jobCount > 0)
|
||||
{
|
||||
logger.LogInformation("Processed {Count} pending tenant domains.", processed);
|
||||
logger.LogInformation(
|
||||
"Worker processed {DomainCount} pending tenant domains and {JobCount} background jobs.",
|
||||
domainCount,
|
||||
jobCount);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
||||
@@ -40,4 +42,24 @@ public class Worker(
|
||||
stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<int> ProcessTenantDomainsAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await using var scope = scopeFactory.CreateAsyncScope();
|
||||
scope.ServiceProvider.GetRequiredService<ITenantContextInitializer>()
|
||||
.InitializeSystem(null, "Tenant domain DNS and TLS lifecycle worker");
|
||||
return await scope.ServiceProvider
|
||||
.GetRequiredService<ITenantDomainLifecycleService>()
|
||||
.ProcessPendingAsync(stoppingToken);
|
||||
}
|
||||
|
||||
private async Task<int> ProcessBackgroundJobsAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await using var scope = scopeFactory.CreateAsyncScope();
|
||||
scope.ServiceProvider.GetRequiredService<ITenantContextInitializer>()
|
||||
.InitializeSystem(null, "Background job lease worker");
|
||||
return await scope.ServiceProvider
|
||||
.GetRequiredService<IBackgroundJobService>()
|
||||
.ProcessPendingAsync(workerId, 20, stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user