From 50e9334db639a24de5ae6c616e79c30fa4a00752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sedat=20=C3=96zt=C3=BCrk?= Date: Sun, 14 Jun 2026 00:26:19 +0300 Subject: [PATCH] =?UTF-8?q?Hangfire=20otomatik=20y=C3=BCklenecek?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BackgroundWorkerInitializer.cs | 20 ++++++- .../FileManagementAppService.cs | 14 +---- .../BackgroundWorkerStartupService.cs | 58 +++++++++++++++++++ .../PlatformHttpApiHostModule.cs | 23 +------- 4 files changed, 78 insertions(+), 37 deletions(-) create mode 100644 api/src/Sozsoft.Platform.HttpApi.Host/BackgroundWorkerStartupService.cs diff --git a/api/src/Sozsoft.Platform.Application/BackgroundWorker/BackgroundWorkerInitializer.cs b/api/src/Sozsoft.Platform.Application/BackgroundWorker/BackgroundWorkerInitializer.cs index 77abc83..fdd092e 100644 --- a/api/src/Sozsoft.Platform.Application/BackgroundWorker/BackgroundWorkerInitializer.cs +++ b/api/src/Sozsoft.Platform.Application/BackgroundWorker/BackgroundWorkerInitializer.cs @@ -11,6 +11,7 @@ using Sozsoft.Platform.Enums; using Microsoft.Extensions.Logging; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Repositories; +using Volo.Abp.Uow; namespace Sozsoft.Platform; @@ -19,19 +20,24 @@ public class BackgroundWorkerInitializer : ITransientDependency private readonly IRepository _repo; private readonly PlatformTemplateManager _templateManager; private readonly ILogger _logger; + private readonly IUnitOfWorkManager _unitOfWorkManager; public BackgroundWorkerInitializer( IRepository repo, PlatformTemplateManager templateManager, - ILogger logger) + ILogger logger, + IUnitOfWorkManager unitOfWorkManager) { _repo = repo; _templateManager = templateManager; _logger = logger; + _unitOfWorkManager = unitOfWorkManager; } public async Task RunAsync() { + using var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: false); + var turkeyTimeZone = GetTurkeyTimeZone(); _logger.LogInformation( @@ -41,6 +47,10 @@ public class BackgroundWorkerInitializer : ITransientDependency var jobs = JobStorage.Current.GetConnection().GetRecurringJobs(); var workers = await _repo.GetListAsync(a => a.IsActive); + _logger.LogInformation( + "Loading {WorkerCount} active background workers into Hangfire.", + workers.Count); + foreach (var job in jobs) { 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() @@ -98,4 +114,4 @@ public class BackgroundWorkerInitializer : ITransientDependency throw new TimeZoneNotFoundException( "Türkiye saat dilimi bulunamadı. Linux/Docker için 'Europe/Istanbul', Windows için 'Turkey Standard Time' gerekir."); } -} \ No newline at end of file +} diff --git a/api/src/Sozsoft.Platform.Application/FileManagement/FileManagementAppService.cs b/api/src/Sozsoft.Platform.Application/FileManagement/FileManagementAppService.cs index e327b24..8ca0365 100644 --- a/api/src/Sozsoft.Platform.Application/FileManagement/FileManagementAppService.cs +++ b/api/src/Sozsoft.Platform.Application/FileManagement/FileManagementAppService.cs @@ -110,18 +110,6 @@ public class FileManagementAppService : ApplicationService, IFileManagementAppSe 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) { var pathParts = path.Split('/', StringSplitOptions.RemoveEmptyEntries); @@ -159,7 +147,7 @@ public class FileManagementAppService : ApplicationService, IFileManagementAppSe { var decodedPath = DecodeIdAsPath(id); - if (IsProtectedFolder(decodedPath)) + if (IsProtectedRootFolder(decodedPath)) { var folderName = decodedPath.Split('/')[0]; Logger.LogWarning($"Blocked {operation} operation on protected folder: {folderName}"); diff --git a/api/src/Sozsoft.Platform.HttpApi.Host/BackgroundWorkerStartupService.cs b/api/src/Sozsoft.Platform.HttpApi.Host/BackgroundWorkerStartupService.cs new file mode 100644 index 0000000..f45c6d4 --- /dev/null +++ b/api/src/Sozsoft.Platform.HttpApi.Host/BackgroundWorkerStartupService.cs @@ -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 _logger; + + public BackgroundWorkerStartupService( + IServiceProvider serviceProvider, + IHostApplicationLifetime applicationLifetime, + ILogger 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(); + await initializer.RunAsync(); + + _logger.LogInformation("Background worker startup initialization completed."); + } + catch (Exception ex) + { + _logger.LogError(ex, "Background worker initialization failed."); + } + } +} diff --git a/api/src/Sozsoft.Platform.HttpApi.Host/PlatformHttpApiHostModule.cs b/api/src/Sozsoft.Platform.HttpApi.Host/PlatformHttpApiHostModule.cs index 50bc78f..4c2fb53 100644 --- a/api/src/Sozsoft.Platform.HttpApi.Host/PlatformHttpApiHostModule.cs +++ b/api/src/Sozsoft.Platform.HttpApi.Host/PlatformHttpApiHostModule.cs @@ -134,6 +134,7 @@ public class PlatformHttpApiHostModule : AbpModule ConfigureIdentity(configuration); ConfigureCache(); ConfigureHangfire(context, configuration); + context.Services.AddHostedService(); ConfigureBlobStoring(configuration); 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>(); - - try - { - var initializer = scope.ServiceProvider.GetRequiredService(); - await initializer.RunAsync(); - } - catch (Exception ex) - { - logger.LogError(ex, "Background worker initialization failed."); - } - }); - - return Task.CompletedTask; - } }