Hangfire otomatik yüklenecek
This commit is contained in:
parent
fc30539916
commit
50e9334db6
4 changed files with 78 additions and 37 deletions
|
|
@ -11,6 +11,7 @@ using Sozsoft.Platform.Enums;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Volo.Abp.DependencyInjection;
|
using Volo.Abp.DependencyInjection;
|
||||||
using Volo.Abp.Domain.Repositories;
|
using Volo.Abp.Domain.Repositories;
|
||||||
|
using Volo.Abp.Uow;
|
||||||
|
|
||||||
namespace Sozsoft.Platform;
|
namespace Sozsoft.Platform;
|
||||||
|
|
||||||
|
|
@ -19,19 +20,24 @@ public class BackgroundWorkerInitializer : ITransientDependency
|
||||||
private readonly IRepository<BackgroundWorker, Guid> _repo;
|
private readonly IRepository<BackgroundWorker, Guid> _repo;
|
||||||
private readonly PlatformTemplateManager _templateManager;
|
private readonly PlatformTemplateManager _templateManager;
|
||||||
private readonly ILogger<BackgroundWorkerInitializer> _logger;
|
private readonly ILogger<BackgroundWorkerInitializer> _logger;
|
||||||
|
private readonly IUnitOfWorkManager _unitOfWorkManager;
|
||||||
|
|
||||||
public BackgroundWorkerInitializer(
|
public BackgroundWorkerInitializer(
|
||||||
IRepository<BackgroundWorker, Guid> repo,
|
IRepository<BackgroundWorker, Guid> repo,
|
||||||
PlatformTemplateManager templateManager,
|
PlatformTemplateManager templateManager,
|
||||||
ILogger<BackgroundWorkerInitializer> logger)
|
ILogger<BackgroundWorkerInitializer> logger,
|
||||||
|
IUnitOfWorkManager unitOfWorkManager)
|
||||||
{
|
{
|
||||||
_repo = repo;
|
_repo = repo;
|
||||||
_templateManager = templateManager;
|
_templateManager = templateManager;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_unitOfWorkManager = unitOfWorkManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task RunAsync()
|
public async Task RunAsync()
|
||||||
{
|
{
|
||||||
|
using var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: false);
|
||||||
|
|
||||||
var turkeyTimeZone = GetTurkeyTimeZone();
|
var turkeyTimeZone = GetTurkeyTimeZone();
|
||||||
|
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
|
|
@ -41,6 +47,10 @@ public class BackgroundWorkerInitializer : ITransientDependency
|
||||||
var jobs = JobStorage.Current.GetConnection().GetRecurringJobs();
|
var jobs = JobStorage.Current.GetConnection().GetRecurringJobs();
|
||||||
var workers = await _repo.GetListAsync(a => a.IsActive);
|
var workers = await _repo.GetListAsync(a => a.IsActive);
|
||||||
|
|
||||||
|
_logger.LogInformation(
|
||||||
|
"Loading {WorkerCount} active background workers into Hangfire.",
|
||||||
|
workers.Count);
|
||||||
|
|
||||||
foreach (var job in jobs)
|
foreach (var job in jobs)
|
||||||
{
|
{
|
||||||
if (job.Queue == "platform" && !workers.Exists(a => $"{a.WorkerType}:{a.Name}" == job.Id))
|
if (job.Queue == "platform" && !workers.Exists(a => $"{a.WorkerType}:{a.Name}" == job.Id))
|
||||||
|
|
@ -73,6 +83,12 @@ public class BackgroundWorkerInitializer : ITransientDependency
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation(
|
||||||
|
"{WorkerCount} active background workers loaded into Hangfire.",
|
||||||
|
workers.Count);
|
||||||
|
|
||||||
|
await uow.CompleteAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TimeZoneInfo GetTurkeyTimeZone()
|
private static TimeZoneInfo GetTurkeyTimeZone()
|
||||||
|
|
|
||||||
|
|
@ -110,18 +110,6 @@ public class FileManagementAppService : ApplicationService, IFileManagementAppSe
|
||||||
return id.Replace("|", "/");
|
return id.Replace("|", "/");
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsProtectedFolder(string path)
|
|
||||||
{
|
|
||||||
// Get the root folder name from path
|
|
||||||
var pathParts = path.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
if (pathParts.Length == 0) return false;
|
|
||||||
|
|
||||||
var rootFolder = pathParts[0];
|
|
||||||
var isProtected = ProtectedFolders.Contains(rootFolder);
|
|
||||||
|
|
||||||
return isProtected;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsProtectedRootFolder(string path)
|
private bool IsProtectedRootFolder(string path)
|
||||||
{
|
{
|
||||||
var pathParts = path.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
var pathParts = path.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
@ -159,7 +147,7 @@ public class FileManagementAppService : ApplicationService, IFileManagementAppSe
|
||||||
{
|
{
|
||||||
var decodedPath = DecodeIdAsPath(id);
|
var decodedPath = DecodeIdAsPath(id);
|
||||||
|
|
||||||
if (IsProtectedFolder(decodedPath))
|
if (IsProtectedRootFolder(decodedPath))
|
||||||
{
|
{
|
||||||
var folderName = decodedPath.Split('/')[0];
|
var folderName = decodedPath.Split('/')[0];
|
||||||
Logger.LogWarning($"Blocked {operation} operation on protected folder: {folderName}");
|
Logger.LogWarning($"Blocked {operation} operation on protected folder: {folderName}");
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,58 @@
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Background worker startup initialization started.");
|
||||||
|
|
||||||
|
using var scope = _serviceProvider.CreateScope();
|
||||||
|
var initializer = scope.ServiceProvider.GetRequiredService<BackgroundWorkerInitializer>();
|
||||||
|
await initializer.RunAsync();
|
||||||
|
|
||||||
|
_logger.LogInformation("Background worker startup initialization completed.");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Background worker initialization failed.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -134,6 +134,7 @@ public class PlatformHttpApiHostModule : AbpModule
|
||||||
ConfigureIdentity(configuration);
|
ConfigureIdentity(configuration);
|
||||||
ConfigureCache();
|
ConfigureCache();
|
||||||
ConfigureHangfire(context, configuration);
|
ConfigureHangfire(context, configuration);
|
||||||
|
context.Services.AddHostedService<BackgroundWorkerStartupService>();
|
||||||
ConfigureBlobStoring(configuration);
|
ConfigureBlobStoring(configuration);
|
||||||
ConfigureAuditing();
|
ConfigureAuditing();
|
||||||
|
|
||||||
|
|
@ -540,26 +541,4 @@ public class PlatformHttpApiHostModule : AbpModule
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context)
|
|
||||||
{
|
|
||||||
var serviceProvider = context.ServiceProvider;
|
|
||||||
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
using var scope = serviceProvider.CreateScope();
|
|
||||||
var logger = scope.ServiceProvider.GetRequiredService<ILogger<PlatformHttpApiHostModule>>();
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var initializer = scope.ServiceProvider.GetRequiredService<BackgroundWorkerInitializer>();
|
|
||||||
await initializer.RunAsync();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
logger.LogError(ex, "Background worker initialization failed.");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue