Browse Source

PErmission management: complete UOW inside the common lock to prevent conflicts.

pull/16276/head
Halil İbrahim Kalkan 4 years ago
parent
commit
c61d97f09d
  1. 54
      modules/feature-management/src/Volo.Abp.FeatureManagement.Domain/Volo/Abp/FeatureManagement/StaticFeatureSaver.cs

54
modules/feature-management/src/Volo.Abp.FeatureManagement.Domain/Volo/Abp/FeatureManagement/StaticFeatureSaver.cs

@ -28,6 +28,8 @@ public class StaticFeatureSaver : IStaticFeatureSaver, ITransientDependency
protected ICancellationTokenProvider CancellationTokenProvider { get; } protected ICancellationTokenProvider CancellationTokenProvider { get; }
protected AbpDistributedCacheOptions CacheOptions { get; } protected AbpDistributedCacheOptions CacheOptions { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; }
public StaticFeatureSaver( public StaticFeatureSaver(
IStaticFeatureDefinitionStore staticStore, IStaticFeatureDefinitionStore staticStore,
IFeatureGroupDefinitionRecordRepository featureGroupRepository, IFeatureGroupDefinitionRecordRepository featureGroupRepository,
@ -38,7 +40,8 @@ public class StaticFeatureSaver : IStaticFeatureSaver, ITransientDependency
IApplicationInfoAccessor applicationInfoAccessor, IApplicationInfoAccessor applicationInfoAccessor,
IAbpDistributedLock distributedLock, IAbpDistributedLock distributedLock,
IOptions<AbpFeatureOptions> featureManagementOptions, IOptions<AbpFeatureOptions> featureManagementOptions,
ICancellationTokenProvider cancellationTokenProvider) ICancellationTokenProvider cancellationTokenProvider,
IUnitOfWorkManager unitOfWorkManager)
{ {
StaticStore = staticStore; StaticStore = staticStore;
FeatureGroupRepository = featureGroupRepository; FeatureGroupRepository = featureGroupRepository;
@ -48,12 +51,12 @@ public class StaticFeatureSaver : IStaticFeatureSaver, ITransientDependency
ApplicationInfoAccessor = applicationInfoAccessor; ApplicationInfoAccessor = applicationInfoAccessor;
DistributedLock = distributedLock; DistributedLock = distributedLock;
CancellationTokenProvider = cancellationTokenProvider; CancellationTokenProvider = cancellationTokenProvider;
UnitOfWorkManager = unitOfWorkManager;
FeatureOptions = featureManagementOptions.Value; FeatureOptions = featureManagementOptions.Value;
CacheOptions = cacheOptions.Value; CacheOptions = cacheOptions.Value;
} }
[UnitOfWork] public async Task SaveAsync()
public virtual async Task SaveAsync()
{ {
await using var applicationLockHandle = await DistributedLock.TryAcquireAsync( await using var applicationLockHandle = await DistributedLock.TryAcquireAsync(
GetApplicationDistributedLockKey() GetApplicationDistributedLockKey()
@ -99,19 +102,40 @@ public class StaticFeatureSaver : IStaticFeatureSaver, ITransientDependency
throw new AbpException("Could not acquire distributed lock for saving static features!"); throw new AbpException("Could not acquire distributed lock for saving static features!");
} }
var hasChangesInGroups = await UpdateChangedFeatureGroupsAsync(featureGroupRecords); using (var unitOfWork = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: true))
var hasChangesInFeatures = await UpdateChangedFeaturesAsync(featureRecords);
if (hasChangesInGroups ||hasChangesInFeatures)
{ {
await Cache.SetStringAsync( try
GetCommonStampCacheKey(), {
Guid.NewGuid().ToString(), var hasChangesInGroups = await UpdateChangedFeatureGroupsAsync(featureGroupRecords);
new DistributedCacheEntryOptions { var hasChangesInFeatures = await UpdateChangedFeaturesAsync(featureRecords);
SlidingExpiration = TimeSpan.FromDays(30) //TODO: Make it configurable?
}, if (hasChangesInGroups ||hasChangesInFeatures)
CancellationTokenProvider.Token {
); await Cache.SetStringAsync(
GetCommonStampCacheKey(),
Guid.NewGuid().ToString(),
new DistributedCacheEntryOptions {
SlidingExpiration = TimeSpan.FromDays(30) //TODO: Make it configurable?
},
CancellationTokenProvider.Token
);
}
}
catch
{
try
{
await unitOfWork.RollbackAsync();
}
catch
{
/* ignored */
}
throw;
}
await unitOfWork.CompleteAsync();
} }
} }

Loading…
Cancel
Save