From 9786b8143c5dc31faf874e2ef5d7eb336af16298 Mon Sep 17 00:00:00 2001 From: Yang Chun Ung Date: Fri, 30 Aug 2024 11:10:52 +0900 Subject: [PATCH] Run worker by options --- ArenaService/ArenaService/Program.cs | 28 +++++++++++++------ .../RedisArenaParticipantsService.cs | 11 ++------ ArenaService/ArenaService/StandaloneQuery.cs | 2 +- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/ArenaService/ArenaService/Program.cs b/ArenaService/ArenaService/Program.cs index 1b4bded..afc8b11 100644 --- a/ArenaService/ArenaService/Program.cs +++ b/ArenaService/ArenaService/Program.cs @@ -11,6 +11,7 @@ var configuration = builder.Configuration; var redisConnectionString = configuration["Redis:ConnectionString"]!; var timeOut = int.Parse(configuration["Redis:TimeOut"]!); +var enableWorker = bool.Parse(configuration["Worker"]!); var configurationOptions = new ConfigurationOptions { EndPoints = { redisConnectionString }, @@ -24,22 +25,33 @@ // Add services to the container. builder.Services - .AddSingleton() - .AddHostedService() - .AddSingleton(new PrivateKey()) .AddSingleton(_ => redis) - .AddSingleton() - .AddSingleton() - .AddHostedService() .AddScoped() + .AddSingleton() + .AddSingleton() + .AddHostedService() .AddGraphQL(options => options.EnableMetrics = true) .AddSystemTextJson() .AddGraphTypes(typeof(AddressType)) .AddGraphTypes(typeof(StandaloneQuery)); -builder.Services +if (enableWorker) +{ + builder.Services + .AddSingleton() + .AddHostedService() + .AddSingleton(new PrivateKey()) + .AddSingleton() + .AddHostedService(); +} + +var healthChecksBuilder = builder.Services .AddHealthChecks() - .AddCheck(nameof(RpcNodeHealthCheck)); + .AddCheck(nameof(RedisHealthCheck)); +if (enableWorker) +{ + healthChecksBuilder.AddCheck(nameof(RpcNodeHealthCheck)); +} var app = builder.Build(); diff --git a/ArenaService/ArenaService/RedisArenaParticipantsService.cs b/ArenaService/ArenaService/RedisArenaParticipantsService.cs index 280168e..c96d6d5 100644 --- a/ArenaService/ArenaService/RedisArenaParticipantsService.cs +++ b/ArenaService/ArenaService/RedisArenaParticipantsService.cs @@ -1,19 +1,14 @@ using System.Text.Json; -using Libplanet.Crypto; using StackExchange.Redis; namespace ArenaService; -public class RedisArenaParticipantsService : IRedisArenaParticipantsService +public class RedisArenaParticipantsService(IConnectionMultiplexer redis, RedisHealthCheck redisHealthCheck) + : IRedisArenaParticipantsService { public const string SeasonKey = "season"; - private readonly IDatabase _db; - - public RedisArenaParticipantsService(IConnectionMultiplexer redis) - { - _db = redis.GetDatabase(); - } + private readonly IDatabase _db = redis.GetDatabase(); public async Task> GetArenaParticipantsAsync(string key) { diff --git a/ArenaService/ArenaService/StandaloneQuery.cs b/ArenaService/ArenaService/StandaloneQuery.cs index 796618f..ea38b83 100644 --- a/ArenaService/ArenaService/StandaloneQuery.cs +++ b/ArenaService/ArenaService/StandaloneQuery.cs @@ -4,7 +4,7 @@ namespace ArenaService; public class StandaloneQuery : ObjectGraphType { - public StandaloneQuery(RpcClient rpcClient, IRedisArenaParticipantsService redisArenaParticipantsService) + public StandaloneQuery(IRedisArenaParticipantsService redisArenaParticipantsService) { Field>(name: "stateQuery", resolve: _ => new StateQuery(redisArenaParticipantsService)); }