2026-06-13 21:26:19 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace Sozsoft.Platform;
|
|
|
|
|
|
|
|
|
|
public class BackgroundWorkerStartupService : IHostedService
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
private readonly IHostApplicationLifetime _applicationLifetime;
|
|
|
|
|
private readonly ILogger<BackgroundWorkerStartupService> _logger;
|
|
|
|
|
|
|
|
|
|
public BackgroundWorkerStartupService(
|
|
|
|
|
IServiceProvider serviceProvider,
|
|
|
|
|
IHostApplicationLifetime applicationLifetime,
|
|
|
|
|
ILogger<BackgroundWorkerStartupService> logger)
|
|
|
|
|
{
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
_applicationLifetime = applicationLifetime;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
_applicationLifetime.ApplicationStarted.Register(() =>
|
|
|
|
|
{
|
|
|
|
|
_ = Task.Run(InitializeBackgroundWorkersAsync);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task InitializeBackgroundWorkersAsync()
|
|
|
|
|
{
|
2026-06-29 09:37:52 +00:00
|
|
|
const int maxAttempts = 5;
|
|
|
|
|
|
|
|
|
|
for (var attempt = 1; attempt <= maxAttempts; attempt++)
|
2026-06-13 21:26:19 +00:00
|
|
|
{
|
2026-06-29 09:37:52 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation(
|
|
|
|
|
"Background worker startup initialization started. Attempt {Attempt}/{MaxAttempts}.",
|
|
|
|
|
attempt,
|
|
|
|
|
maxAttempts);
|
2026-06-13 21:26:19 +00:00
|
|
|
|
2026-06-29 09:37:52 +00:00
|
|
|
using var scope = _serviceProvider.CreateScope();
|
|
|
|
|
var abpRecurringJobInitializer = scope.ServiceProvider.GetRequiredService<AbpBackgroundWorkerRecurringJobInitializer>();
|
|
|
|
|
await abpRecurringJobInitializer.RunAsync();
|
2026-06-13 21:26:19 +00:00
|
|
|
|
2026-06-29 09:37:52 +00:00
|
|
|
var initializer = scope.ServiceProvider.GetRequiredService<BackgroundWorkerInitializer>();
|
|
|
|
|
await initializer.RunAsync();
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Background worker startup initialization completed.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (attempt == maxAttempts)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Background worker initialization failed.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogWarning(
|
|
|
|
|
ex,
|
|
|
|
|
"Background worker initialization failed. It will be retried.");
|
|
|
|
|
|
|
|
|
|
await Task.Delay(TimeSpan.FromSeconds(3));
|
|
|
|
|
}
|
2026-06-13 21:26:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|