Skip to content

Commit

Permalink
Merge pull request #5 from planetarium/rc-2
Browse files Browse the repository at this point in the history
Introduce worker timeout
  • Loading branch information
ipdae authored Nov 5, 2024
2 parents 1c1097b + 32ebb63 commit 30bcb4e
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 117 deletions.
5 changes: 3 additions & 2 deletions ArenaService/ArenaService/ArenaService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="8.0.1" />
<PackageReference Include="GraphQL" Version="4.7.1" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="5.1.1" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore.SystemTextJson" Version="5.1.1" />
<PackageReference Include="GraphQL.Server.Ui.Playground" Version="5.1.1" />
<PackageReference Include="Grpc.AspNetCore" Version="2.57.0"/>
<PackageReference Include="Lib9c" Version="1.18.0-dev.776c9113f7fd8f99b5645222d45b57dd7ad2cb82" />
<PackageReference Include="Libplanet.Action" Version="5.2.2" />
<PackageReference Include="Lib9c" Version="1.19.0" />
<PackageReference Include="Libplanet" Version="5.4.0-dev.20241028012328" />
<PackageReference Include="MagicOnion.Client" Version="6.1.4" />
<PackageReference Include="StackExchange.Redis" Version="2.8.0" />
</ItemGroup>
Expand Down
20 changes: 11 additions & 9 deletions ArenaService/ArenaService/ArenaWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class ArenaParticipantsWorker : BackgroundService
private int _interval;
private IRedisArenaParticipantsService _service;
private ILogger<ArenaParticipantsWorker> _logger;
private static readonly CancellationTokenSource _cts = new CancellationTokenSource();

public ArenaParticipantsWorker(RpcClient rpcClient, IRedisArenaParticipantsService service, ILogger<ArenaParticipantsWorker> logger)
{
Expand All @@ -27,10 +28,9 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
await PrepareArenaParticipants();
}
}
catch (OperationCanceledException)
finally
{
//pass
_logger.LogInformation("[ArenaParticipantsWorker]Cancel ArenaParticipantsWorker");
_cts.Dispose();
}
}

Expand All @@ -46,19 +46,21 @@ public async Task PrepareArenaParticipants()
// Copy from NineChronicles RxProps.Arena
// https://github.com/planetarium/NineChronicles/blob/80.0.1/nekoyume/Assets/_Scripts/State/RxProps.Arena.cs#L279
var retry = 0;
_cts.CancelAfter(TimeSpan.FromMinutes(5));
var cancellationToken = _cts.Token;
while (_rpcClient.Tip?.Index == _rpcClient.PreviousTip?.Index)
{
await Task.Delay((5 - retry) * 1000);
await Task.Delay((5 - retry) * 1000, cancellationToken);
retry++;
if (retry >= 3)
{
throw new InvalidOperationException();
}
}

var tip = _rpcClient.Tip;
var currentRoundData = await _rpcClient.GetRoundData(tip);
var participants = await _rpcClient.GetArenaParticipantsState(tip, currentRoundData);
var tip = _rpcClient.Tip!;
var currentRoundData = await _rpcClient.GetRoundData(tip, cancellationToken);
var participants = await _rpcClient.GetArenaParticipantsState(tip, currentRoundData, cancellationToken);
var cacheKey = $"{currentRoundData.ChampionshipId}_{currentRoundData.Round}";
var scoreCacheKey = $"{cacheKey}_scores";
var prevAddrAndScores = await _service.GetAvatarAddrAndScores(scoreCacheKey);
Expand All @@ -73,13 +75,13 @@ public async Task PrepareArenaParticipants()

var avatarAddrList = participants.AvatarAddresses;
// 최신상태의 아바타 주소, 점수를 조회
var avatarAddrAndScores = await _rpcClient.GetAvatarAddrAndScores(tip, avatarAddrList, currentRoundData);
var avatarAddrAndScores = await _rpcClient.GetAvatarAddrAndScores(tip, avatarAddrList, currentRoundData, cancellationToken);
// 이전상태의 아바타 주소, 점수를 비교해서 추가되거나 점수가 변경된 대상만 찾음
var updatedAddressAndScores = avatarAddrAndScores.Except(prevAddrAndScores).ToList();
// 전체목록의 랭킹 순서 처리
var avatarAddrAndScoresWithRank = _rpcClient.AvatarAddrAndScoresWithRank(avatarAddrAndScores);
// 전체목록의 ArenaParticipant 업데이트
var result = await _rpcClient.GetArenaParticipants(tip, updatedAddressAndScores.Select(i => i.AvatarAddr).ToList(), avatarAddrAndScoresWithRank, prevArenaParticipants);
var result = await _rpcClient.GetArenaParticipants(tip, updatedAddressAndScores.Select(i => i.AvatarAddr).ToList(), avatarAddrAndScoresWithRank, prevArenaParticipants, cancellationToken);
// 캐시 업데이트
await _service.SetArenaParticipantsAsync(cacheKey, result, expiry);
await _service.SetSeasonAsync(cacheKey, expiry);
Expand Down
4 changes: 1 addition & 3 deletions ArenaService/ArenaService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
builder.Services
.AddSingleton<IConnectionMultiplexer>(_ => redis)
.AddScoped<ISchema, StandaloneSchema>()
.AddSingleton<RedisHealthCheck>()
.AddSingleton<IRedisArenaParticipantsService, RedisArenaParticipantsService>()
.AddHostedService<RedisHealthCheckService>()
.AddGraphQL(options => options.EnableMetrics = true)
.AddSystemTextJson()
.AddGraphTypes(typeof(AddressType))
Expand All @@ -47,7 +45,7 @@

var healthChecksBuilder = builder.Services
.AddHealthChecks()
.AddCheck<RedisHealthCheck>(nameof(RedisHealthCheck));
.AddRedis(redis);
if (enableWorker)
{
healthChecksBuilder.AddCheck<RpcNodeHealthCheck>(nameof(RpcNodeHealthCheck));
Expand Down
2 changes: 1 addition & 1 deletion ArenaService/ArenaService/RedisArenaParticipantsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace ArenaService;

public class RedisArenaParticipantsService(IConnectionMultiplexer redis, RedisHealthCheck redisHealthCheck)
public class RedisArenaParticipantsService(IConnectionMultiplexer redis)
: IRedisArenaParticipantsService
{
public const string SeasonKey = "season";
Expand Down
24 changes: 0 additions & 24 deletions ArenaService/ArenaService/RedisHealthCheck.cs

This file was deleted.

28 changes: 0 additions & 28 deletions ArenaService/ArenaService/RedisHealthCheckService.cs

This file was deleted.

Loading

0 comments on commit 30bcb4e

Please sign in to comment.