mirror of https://github.com/abpframework/abp.git
19 changed files with 178 additions and 135 deletions
@ -1,27 +1,46 @@ |
|||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
using Microsoft.Extensions.DependencyInjection; |
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Logging; |
||||
using Microsoft.Extensions.Options; |
using Microsoft.Extensions.Options; |
||||
using Volo.Abp.BackgroundWorkers; |
using Volo.Abp.BackgroundWorkers; |
||||
|
using Volo.Abp.DistributedLocking; |
||||
using Volo.Abp.Threading; |
using Volo.Abp.Threading; |
||||
|
|
||||
namespace Volo.Abp.OpenIddict.Tokens; |
namespace Volo.Abp.OpenIddict.Tokens; |
||||
|
|
||||
public class TokenCleanupBackgroundWorker : AsyncPeriodicBackgroundWorkerBase |
public class TokenCleanupBackgroundWorker : AsyncPeriodicBackgroundWorkerBase |
||||
{ |
{ |
||||
|
protected IAbpDistributedLock DistributedLock { get; } |
||||
|
|
||||
public TokenCleanupBackgroundWorker( |
public TokenCleanupBackgroundWorker( |
||||
AbpAsyncTimer timer, |
AbpAsyncTimer timer, |
||||
IServiceScopeFactory serviceScopeFactory, |
IServiceScopeFactory serviceScopeFactory, |
||||
IOptionsMonitor<TokenCleanupOptions> cleanupOptions) |
IOptionsMonitor<TokenCleanupOptions> cleanupOptions, |
||||
|
IAbpDistributedLock distributedLock) |
||||
: base(timer, serviceScopeFactory) |
: base(timer, serviceScopeFactory) |
||||
{ |
{ |
||||
|
DistributedLock = distributedLock; |
||||
timer.Period = cleanupOptions.CurrentValue.CleanupPeriod; |
timer.Period = cleanupOptions.CurrentValue.CleanupPeriod; |
||||
} |
} |
||||
|
|
||||
protected async override Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) |
protected async override Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) |
||||
{ |
{ |
||||
await workerContext |
await using (var handle = await DistributedLock.TryAcquireAsync(nameof(TokenCleanupBackgroundWorker))) |
||||
.ServiceProvider |
{ |
||||
.GetRequiredService<TokenCleanupService>() |
Logger.LogInformation($"Lock is acquired for {nameof(TokenCleanupBackgroundWorker)}"); |
||||
.CleanAsync(); |
|
||||
|
if (handle != null) |
||||
|
{ |
||||
|
await workerContext |
||||
|
.ServiceProvider |
||||
|
.GetRequiredService<TokenCleanupService>() |
||||
|
.CleanAsync(); |
||||
|
|
||||
|
Logger.LogInformation($"Lock is released for {nameof(TokenCleanupBackgroundWorker)}"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
Logger.LogInformation($"Handle is null because of the locking for : {nameof(TokenCleanupBackgroundWorker)}"); |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue