-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/worker-cluster' into feature/2.6.0-union
# Conflicts: # src/CAServer.EntityEventHandler/CAServerEntityEventHandlerModule.cs
- Loading branch information
Showing
6 changed files
with
51 additions
and
130 deletions.
There are no files selected for viewing
17 changes: 6 additions & 11 deletions
17
src/CAServer.EntityEventHandler.Core/Worker/ContactMigrateWorker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 6 additions & 10 deletions
16
src/CAServer.EntityEventHandler.Core/Worker/LoginGuardianChangeRecordReceiveWorker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 7 additions & 10 deletions
17
src/CAServer.EntityEventHandler.Core/Worker/ReferralRankWorker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 11 additions & 82 deletions
93
src/CAServer.EntityEventHandler.Core/Worker/TokenPriceBackgroundWorker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,41 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CAServer.Commons; | ||
using CAServer.Options; | ||
using CAServer.ScheduledTask; | ||
using CAServer.Tokens.TokenPrice; | ||
using Microsoft.Extensions.Caching.Distributed; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Volo.Abp.BackgroundWorkers; | ||
using Volo.Abp.Caching; | ||
using Volo.Abp.DistributedLocking; | ||
using Volo.Abp.Threading; | ||
|
||
namespace CAServer.EntityEventHandler.Core.Worker; | ||
|
||
public class TokenPriceBackgroundWorker : AsyncPeriodicBackgroundWorkerBase | ||
public class TokenPriceBackgroundWorker : ScheduledTaskBase | ||
{ | ||
private readonly ILogger<TokenPriceBackgroundWorker> _logger; | ||
private readonly IAbpDistributedLock _distributedLock; | ||
private readonly IDistributedCache<string> _distributedCache; | ||
private readonly TokenPriceWorkerOption _tokenPriceWorkerOption; | ||
private readonly ITokenPriceService _tokenPriceService; | ||
|
||
private readonly string _hostName; | ||
private readonly string _workerNameKey; | ||
private readonly string _workerLockKey; | ||
|
||
public TokenPriceBackgroundWorker(AbpAsyncTimer timer, IServiceScopeFactory serviceScopeFactory, | ||
IAbpDistributedLock distributedLock, ILogger<TokenPriceBackgroundWorker> logger, | ||
IDistributedCache<string> distributedCache, IOptionsMonitor<TokenPriceWorkerOption> tokenPriceWorkerOption, | ||
ITokenPriceService tokenPriceService) : base(timer, serviceScopeFactory) | ||
public TokenPriceBackgroundWorker( | ||
ILogger<TokenPriceBackgroundWorker> logger, | ||
IOptionsMonitor<TokenPriceWorkerOption> tokenPriceWorkerOption, | ||
ITokenPriceService tokenPriceService) | ||
{ | ||
_logger = logger; | ||
_distributedLock = distributedLock; | ||
_distributedCache = distributedCache; | ||
_tokenPriceService = tokenPriceService; | ||
_tokenPriceWorkerOption = tokenPriceWorkerOption.CurrentValue; | ||
|
||
timer.Period = _tokenPriceWorkerOption.Period * 1000; | ||
timer.RunOnStart = true; | ||
|
||
_hostName = $"{HostHelper.GetLocalHostName()}_{Guid.NewGuid()}"; | ||
_workerNameKey = $"{_tokenPriceWorkerOption.Prefix}:{_tokenPriceWorkerOption.WorkerNameKey}"; | ||
_workerLockKey = $"{_tokenPriceWorkerOption.Prefix}:{_tokenPriceWorkerOption.WorkerLockKey}"; | ||
} | ||
|
||
public override async Task StopAsync(CancellationToken cancellationToken = default) | ||
{ | ||
_logger.LogInformation("stopping token price background worker start..."); | ||
await base.StopAsync(cancellationToken); | ||
|
||
try | ||
{ | ||
var workName = await _distributedCache.GetAsync(_workerNameKey); | ||
if (!workName.IsNullOrWhiteSpace() && workName != this._hostName) | ||
{ | ||
return; | ||
} | ||
|
||
_logger.LogInformation("TokenPriceWorker:remove current worker... {0}", workName); | ||
await _distributedCache.RemoveAsync(_workerNameKey); | ||
_logger.LogInformation("TokenPriceWorker:remove current worker finished..."); | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "TokenPriceWorker: stop Workder error."); | ||
} | ||
_logger.LogInformation("stoping token price background worker finished..."); | ||
Period = _tokenPriceWorkerOption.Period * 1000; | ||
} | ||
|
||
protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) | ||
protected override async Task DoWorkAsync() | ||
{ | ||
_logger.LogInformation("token price background worker start... {0}", _hostName); | ||
try | ||
{ | ||
var workName = await _distributedCache.GetAsync(_workerNameKey); | ||
if (!workName.IsNullOrWhiteSpace() && workName != this._hostName) | ||
{ | ||
_logger.LogInformation("TokenPriceWorker: running worker: {0}", workName); | ||
return; | ||
} | ||
|
||
using (var lockHandle = _distributedLock.TryAcquireAsync(_workerLockKey)) | ||
{ | ||
if (lockHandle == null) | ||
{ | ||
_logger.LogWarning("TokenPriceWorker: lock fail."); | ||
return; | ||
} | ||
|
||
_logger.LogInformation("TokenPriceWorker: update current worker... {0}", _hostName); | ||
await _distributedCache.SetAsync(_workerNameKey, _hostName, new DistributedCacheEntryOptions | ||
{ | ||
AbsoluteExpirationRelativeToNow = | ||
TimeSpan.FromSeconds(_tokenPriceWorkerOption.Period + _tokenPriceWorkerOption.Period / 2) | ||
}); | ||
_logger.LogInformation("TokenPriceWorker: update token price...."); | ||
|
||
await _tokenPriceService.RefreshCurrentPriceAsync(); | ||
|
||
_logger.LogInformation("TokenPriceWorker: update token price finished..."); | ||
} | ||
_logger.LogInformation("TokenPriceWorker: update token price...."); | ||
await _tokenPriceService.RefreshCurrentPriceAsync(); | ||
_logger.LogInformation("TokenPriceWorker: update token price finished..."); | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "TokenPriceWorker: task error."); | ||
} | ||
_logger.LogInformation("token price background worker finished..."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters