BackgroundWorker açılışta run edilmesi

This commit is contained in:
Sedat ÖZTÜRK 2026-05-07 09:57:46 +03:00
parent d81ca4cd2f
commit c97e7c4afa
7 changed files with 93 additions and 57 deletions

View file

@ -1,19 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Hangfire;
using Hangfire.Storage;
using Sozsoft.MailQueue.Domain.Shared;
using Sozsoft.Platform.BackgroundWorkers;
using Sozsoft.Platform.Entities;
using Sozsoft.Platform.Enums;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.TextTemplating;
namespace Sozsoft.Platform;
@ -21,18 +11,15 @@ namespace Sozsoft.Platform;
[Authorize]
public class BackgroundWorkerAppService : PlatformAppService
{
private readonly IRepository<BackgroundWorker, Guid> repo;
private readonly PlatformTemplateManager templateManager;
private readonly BackgroundWorkerInitializer initializer;
private readonly ITemplateDefinitionManager platformTemplateDefinitionManager;
public BackgroundWorkerAppService(
IRepository<BackgroundWorker, Guid> repo,
PlatformTemplateManager templateManager,
BackgroundWorkerInitializer initializer,
ITemplateDefinitionManager platformTemplateDefinitionManager
)
{
this.repo = repo;
this.templateManager = templateManager;
this.initializer = initializer;
this.platformTemplateDefinitionManager = platformTemplateDefinitionManager;
}
@ -46,42 +33,7 @@ public class BackgroundWorkerAppService : PlatformAppService
{
try
{
var jobs = JobStorage.Current.GetConnection().GetRecurringJobs();
var workers = await repo.GetListAsync(a => a.IsActive);
foreach (var job in jobs)
{
if (job.Queue == "platform" && !workers.Any(a => $"{a.WorkerType}:{a.Name}" == job.Id))
{
RecurringJob.RemoveIfExists(job.Id);
}
}
foreach (var worker in workers)
{
var workerId = worker.Id;
RecurringJob.AddOrUpdate<IPlatformBackgroundWorker>(
$"{worker.WorkerType}:{worker.Name}",
"platform",
(w) => w.StartAsync(workerId, default),
worker.Cron,
new RecurringJobOptions
{
TimeZone = TimeZoneInfo.Local,
});
if (worker.WorkerType == WorkerTypeEnum.MailQueueWorker && !worker.Options.IsNullOrWhiteSpace())
{
var Options = JsonSerializer.Deserialize<MailQueueWorkerOptions>(worker.Options);
// Add template
if (!Options.MailTemplate.IsNullOrEmpty())
{
await templateManager.AddOrUpdateTemplateAsync(worker.Name, Options.MailTemplate);
}
}
}
await initializer.RunAsync();
}
catch (Exception ex)
{

View file

@ -0,0 +1,70 @@
using System;
using System.Text.Json;
using System.Threading.Tasks;
using Hangfire;
using Hangfire.Storage;
using Sozsoft.MailQueue.Domain.Shared;
using Sozsoft.Platform.BackgroundWorkers;
using Sozsoft.Platform.Entities;
using Sozsoft.Platform.Enums;
using Microsoft.Extensions.Logging;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
namespace Sozsoft.Platform;
public class BackgroundWorkerInitializer : ITransientDependency
{
private readonly IRepository<BackgroundWorker, Guid> _repo;
private readonly PlatformTemplateManager _templateManager;
private readonly ILogger<BackgroundWorkerInitializer> _logger;
public BackgroundWorkerInitializer(
IRepository<BackgroundWorker, Guid> repo,
PlatformTemplateManager templateManager,
ILogger<BackgroundWorkerInitializer> logger)
{
_repo = repo;
_templateManager = templateManager;
_logger = logger;
}
public async Task RunAsync()
{
var jobs = JobStorage.Current.GetConnection().GetRecurringJobs();
var workers = await _repo.GetListAsync(a => a.IsActive);
foreach (var job in jobs)
{
if (job.Queue == "platform" && !workers.Exists(a => $"{a.WorkerType}:{a.Name}" == job.Id))
{
RecurringJob.RemoveIfExists(job.Id);
}
}
foreach (var worker in workers)
{
var workerId = worker.Id;
RecurringJob.AddOrUpdate<IPlatformBackgroundWorker>(
$"{worker.WorkerType}:{worker.Name}",
"platform",
(w) => w.StartAsync(workerId, default),
worker.Cron,
new RecurringJobOptions
{
TimeZone = TimeZoneInfo.Local,
});
if (worker.WorkerType == WorkerTypeEnum.MailQueueWorker && !worker.Options.IsNullOrWhiteSpace())
{
var options = JsonSerializer.Deserialize<MailQueueWorkerOptions>(worker.Options);
if (options != null && !options.MailTemplate.IsNullOrEmpty())
{
await _templateManager.AddOrUpdateTemplateAsync(worker.Name, options.MailTemplate);
}
}
}
}
}

View file

@ -2745,8 +2745,8 @@
{
"resourceName": "Platform",
"key": "Abp.Identity.User.UserInformation",
"en": "User Information",
"tr": "Kullanıcı Bilgileri"
"en": "General",
"tr": "Genel"
},
{
"resourceName": "Platform",

View file

@ -1060,6 +1060,7 @@ public class PlatformDbContext :
b.Property(x => x.PublishDate).IsRequired();
b.Property(x => x.Attachments).HasMaxLength(2048);
b.Property(x => x.ViewCount).HasDefaultValue(0);
b.Property(x => x.ImageUrl).HasColumnType("text");
});
builder.Entity<Survey>(b =>

View file

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Hangfire;
using Hangfire.PostgreSql;
using Sozsoft.Languages;
@ -522,6 +523,11 @@ public class PlatformHttpApiHostModule : AbpModule
app.UseConfiguredEndpoints();
}
public override async Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context)
{
using var scope = context.ServiceProvider.CreateScope();
var initializer = scope.ServiceProvider.GetRequiredService<BackgroundWorkerInitializer>();
await initializer.RunAsync();
}
}

View file

@ -49,6 +49,7 @@ export interface DocumentDto {
parentId: string
isReadOnly: boolean
childCount: number
tenantId?: string
}
// Duyuru

View file

@ -4,6 +4,7 @@ import dayjs from 'dayjs'
import { DocumentDto } from '@/proxy/intranet/models'
import { useLocalization } from '@/utils/hooks/useLocalization'
import { getFileIcon, getFileType } from '@/proxy/intranet/utils'
import { FILE_URL } from '@/constants/app.constant'
const formatFileSize = (bytes: number): string => {
if (bytes === 0) return '0 B'
@ -58,10 +59,15 @@ const RecentDocuments: React.FC<{ documents: DocumentDto[] }> = ({ documents })
<button
onClick={(e) => {
e.stopPropagation()
const url = FILE_URL(doc.path, doc.tenantId)
const link = document.createElement('a')
link.href = `/cdn/${doc.path}`
link.href = url
link.download = doc.name
link.target = '_blank'
link.rel = 'noopener noreferrer'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}}
className="p-2 hover:bg-blue-100 dark:hover:bg-blue-900/30 rounded-lg transition-colors group"
title={translate('::App.Platform.Intranet.Widgets.RecentDocuments.Download')}