Skip to content

Commit

Permalink
Merge pull request #96 from EasyAbp/fix-updater
Browse files Browse the repository at this point in the history
Fix statistic data updater and support local event bus
  • Loading branch information
gdlcf88 committed Jun 14, 2023
2 parents 66eda70 + 2ea5c91 commit a52e576
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Volo.Abp.AutoMapper" Version="$(AbpVersion)" />
<PackageReference Include="Volo.Abp.BackgroundJobs.Abstractions" Version="$(AbpVersion)" />
<PackageReference Include="Volo.Abp.BlobStoring" Version="$(AbpVersion)" />
<PackageReference Include="Volo.Abp.Caching" Version="$(AbpVersion)" />
<PackageReference Include="Volo.Abp.Ddd.Domain" Version="$(AbpVersion)" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using EasyAbp.FileManagement.Files;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AutoMapper;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.BlobStoring;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Entities.Events.Distributed;
Expand All @@ -11,6 +12,7 @@ namespace EasyAbp.FileManagement
[DependsOn(
typeof(FileManagementDomainSharedModule),
typeof(AbpAutoMapperModule),
typeof(AbpBackgroundJobsAbstractionsModule),
typeof(AbpBlobStoringModule),
typeof(AbpCachingModule)
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class FileManager : DomainService, IFileManager

if (parent is not null)
{
await AddOrReplaceSubFilesChangedEventAsync(parent.Id);
await HandleStatisticDataUpdateAsync(parent.Id);
}

return file;
Expand Down Expand Up @@ -131,12 +131,12 @@ public virtual async Task<File> ChangeAsync(File file, string newFileName, File

if (oldParent is not null)
{
await AddOrReplaceSubFilesChangedEventAsync(oldParent.Id);
await HandleStatisticDataUpdateAsync(oldParent.Id);
}

if (newParent is not null)
{
await AddOrReplaceSubFilesChangedEventAsync(newParent.Id);
await HandleStatisticDataUpdateAsync(newParent.Id);
}

return file;
Expand Down Expand Up @@ -190,12 +190,12 @@ public virtual async Task<File> ChangeAsync(File file, string newFileName, File

if (oldParent is not null)
{
await AddOrReplaceSubFilesChangedEventAsync(oldParent.Id);
await HandleStatisticDataUpdateAsync(oldParent.Id);
}

if (newParent is not null)
{
await AddOrReplaceSubFilesChangedEventAsync(newParent.Id);
await HandleStatisticDataUpdateAsync(newParent.Id);
}

return file;
Expand Down Expand Up @@ -231,7 +231,7 @@ public virtual async Task DeleteAsync([NotNull] File file, CancellationToken can

if (parent is not null)
{
await AddOrReplaceSubFilesChangedEventAsync(parent.Id);
await HandleStatisticDataUpdateAsync(parent.Id);
}

await _fileRepository.DeleteAsync(file, true, cancellationToken);
Expand Down Expand Up @@ -373,10 +373,15 @@ protected virtual IFileDownloadProvider GetFileDownloadProvider(File file)
}

[UnitOfWork(true)]
protected virtual Task AddOrReplaceSubFilesChangedEventAsync(Guid directoryId)
protected virtual Task HandleStatisticDataUpdateAsync(Guid directoryId)
{
_unitOfWorkManager.Current.AddOrReplaceDistributedEvent(new UnitOfWorkEventRecord(
typeof(SubFilesChangedEto), new SubFilesChangedEto(directoryId), default));
var useBackgroundJob = _distributedEventBus is LocalDistributedEventBus;

_unitOfWorkManager.Current.AddOrReplaceDistributedEvent(
new UnitOfWorkEventRecord(
typeof(SubFilesChangedEto),
new SubFilesChangedEto(CurrentTenant.Id, directoryId, useBackgroundJob),
default));

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Volo.Abp.EventBus.Distributed;

namespace EasyAbp.FileManagement.Files;

public interface IRecursiveDirectoryStatisticDataUpdater : IDistributedEventHandler<SubFilesChangedEto>
{
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.DistributedLocking;
using Volo.Abp.EventBus;
using Volo.Abp.Uow;

namespace EasyAbp.FileManagement.Files;

public class RecursiveDirectoryStatisticDataUpdater : ILocalEventHandler<SubFilesChangedEto>, ITransientDependency
public class RecursiveDirectoryStatisticDataUpdater : IRecursiveDirectoryStatisticDataUpdater, ITransientDependency
{
protected virtual TimeSpan Timeout => TimeSpan.FromSeconds(3);

private readonly IBackgroundJobManager _backgroundJobManager;
private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly IAbpDistributedLock _abpDistributedLock;
private readonly IFileRepository _fileRepository;

public RecursiveDirectoryStatisticDataUpdater(
IBackgroundJobManager backgroundJobManager,
IUnitOfWorkManager unitOfWorkManager,
IAbpDistributedLock abpDistributedLock,
IFileRepository fileRepository)
{
_backgroundJobManager = backgroundJobManager;
_unitOfWorkManager = unitOfWorkManager;
_abpDistributedLock = abpDistributedLock;
_fileRepository = fileRepository;
}

[UnitOfWork(true)]
public virtual async Task HandleEventAsync(SubFilesChangedEto eventData)
{
await UpdateStatisticDataAsync(eventData.DirectoryId);
if (eventData.UseBackgroundJob)
{
await _backgroundJobManager.EnqueueAsync(
new SubFilesUpdateHandlingJobArgs(eventData.TenantId, eventData.DirectoryId));
}
else
{
await UpdateStatisticDataAsync(eventData.DirectoryId);
}
}

[UnitOfWork(true)]
protected virtual async Task UpdateStatisticDataAsync(Guid? directoryId)
{
while (directoryId != null)
Expand All @@ -38,6 +51,13 @@ protected virtual async Task UpdateStatisticDataAsync(Guid? directoryId)
await using var lockHandle =
await _abpDistributedLock.TryAcquireAsync(await GetDistributedLockKeyAsync(directoryId.Value), Timeout);

if (lockHandle is null)
{
throw new AbpDbConcurrencyException();
}

var uow = _unitOfWorkManager.Begin(true);

var directory = await _fileRepository.FindAsync(directoryId.Value);

if (!directory.TryUpdateStatisticData(statisticData))
Expand All @@ -48,6 +68,8 @@ protected virtual async Task UpdateStatisticDataAsync(Guid? directoryId)
await _fileRepository.UpdateAsync(directory, true);

directoryId = directory.ParentId;

await uow.CompleteAsync();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
using System;
using Volo.Abp.MultiTenancy;

namespace EasyAbp.FileManagement.Files;

public class SubFilesChangedEto
public class SubFilesChangedEto : IMultiTenant
{
public Guid? TenantId { get; set; }

public Guid DirectoryId { get; set; }

public bool UseBackgroundJob { get; set; }

public SubFilesChangedEto()
{
}

public SubFilesChangedEto(Guid directoryId)
public SubFilesChangedEto(Guid? tenantId, Guid directoryId, bool useBackgroundJob)
{
TenantId = tenantId;
DirectoryId = directoryId;
UseBackgroundJob = useBackgroundJob;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Threading.Tasks;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;

namespace EasyAbp.FileManagement.Files;

public class SubFilesUpdateHandlingJob : AsyncBackgroundJob<SubFilesUpdateHandlingJobArgs>, ITransientDependency
{
private readonly IRecursiveDirectoryStatisticDataUpdater _updater;

public SubFilesUpdateHandlingJob(IRecursiveDirectoryStatisticDataUpdater updater)
{
_updater = updater;
}

public override async Task ExecuteAsync(SubFilesUpdateHandlingJobArgs args)
{
await _updater.HandleEventAsync(args.SubFilesChangedEto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Volo.Abp.MultiTenancy;

namespace EasyAbp.FileManagement.Files;

[Serializable]
public class SubFilesUpdateHandlingJobArgs : IMultiTenant
{
public Guid? TenantId { get; set; }

public SubFilesChangedEto SubFilesChangedEto { get; set; }

public SubFilesUpdateHandlingJobArgs()
{
}

public SubFilesUpdateHandlingJobArgs(Guid? tenantId, Guid directoryId)
{
TenantId = tenantId;
SubFilesChangedEto = new SubFilesChangedEto(tenantId, directoryId, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;

namespace EasyAbp.FileManagement;

public class RealtimeBackgroundJobManager : IBackgroundJobManager, ITransientDependency
{
private readonly AbpBackgroundJobOptions _options;
private readonly IServiceProvider _serviceProvider;

public RealtimeBackgroundJobManager(
IOptions<AbpBackgroundJobOptions> options,
IServiceProvider serviceProvider)
{
_options = options.Value;
_serviceProvider = serviceProvider;
}

public async Task<string> EnqueueAsync<TArgs>(TArgs args,
BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan? delay = null)
{
var job = _serviceProvider.GetService(_options.GetJob<TArgs>().JobType);

switch (job)
{
case IAsyncBackgroundJob<TArgs> asyncBackgroundJob:
await asyncBackgroundJob.ExecuteAsync(args);
break;
case IBackgroundJob<TArgs> backgroundJob:
backgroundJob.Execute(args);
break;
}

return null;
}
}

0 comments on commit a52e576

Please sign in to comment.