diff --git a/Mimir/Extensions/ServiceCollectionExtensions.cs b/Mimir/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 00000000..f12196a6 --- /dev/null +++ b/Mimir/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,56 @@ +using Hangfire; +using Hangfire.Redis; +using Microsoft.Extensions.DependencyInjection; +using Mimir.Options; +using StackExchange.Redis; + +namespace Mimir.Extensions; + +public static class ServiceCollectionExtensions +{ + public static IServiceCollection AddHangfireServices( + this IServiceCollection services, + HangfireOption hangfireOption) + { + var redisConfig = new ConfigurationOptions + { + DefaultDatabase = hangfireOption.RedisDatabase + }; + + redisConfig.EndPoints.Add(hangfireOption.RedisHost, hangfireOption.RedisPort); + + if (!string.IsNullOrEmpty(hangfireOption.RedisUsername)) + { + redisConfig.User = hangfireOption.RedisUsername; + } + + if (!string.IsNullOrEmpty(hangfireOption.RedisPassword)) + { + redisConfig.Password = hangfireOption.RedisPassword; + } + + var connectionMultiplexer = ConnectionMultiplexer.Connect(redisConfig); + services.AddSingleton(connectionMultiplexer); + + services.AddHangfire( + (provider, config) => + { + config.UseRedisStorage( + connectionMultiplexer, + new RedisStorageOptions + { + Prefix = hangfireOption.RedisPrefix, + Db = hangfireOption.RedisDatabase, + } + ); + } + ); + + services.AddHangfireServer(options => + { + options.WorkerCount = hangfireOption.WorkerCount; + }); + + return services; + } +} diff --git a/Mimir/Options/HangfireOption.cs b/Mimir/Options/HangfireOption.cs index e12aa771..18c2f767 100644 --- a/Mimir/Options/HangfireOption.cs +++ b/Mimir/Options/HangfireOption.cs @@ -3,6 +3,8 @@ namespace Mimir.Options; public class HangfireOption { public const string SectionName = "Hangfire"; + + public bool IsEnabled { get; set; } = false; public string RedisConnectionString { get; set; } = string.Empty; public string DashboardPath { get; set; } = "/hangfire"; public int WorkerCount { get; set; } = Environment.ProcessorCount; diff --git a/Mimir/Program.cs b/Mimir/Program.cs index 3437b198..c10f7f1a 100644 --- a/Mimir/Program.cs +++ b/Mimir/Program.cs @@ -11,6 +11,7 @@ using Libplanet.Crypto; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using Mimir.Extensions; using Mimir.GraphQL; using Mimir.MongoDB.Bson; using Mimir.MongoDB.Repositories; @@ -121,47 +122,14 @@ // ~MongoDB repositories. -// Hangfire and Redis configuration +// Hangfire configuration var hangfireOption = builder .Configuration.GetSection(HangfireOption.SectionName) .Get(); -if (hangfireOption != null) -{ - var redisConfig = new ConfigurationOptions { DefaultDatabase = hangfireOption.RedisDatabase }; - - redisConfig.EndPoints.Add(hangfireOption.RedisHost, hangfireOption.RedisPort); - if (!string.IsNullOrEmpty(hangfireOption.RedisUsername)) - { - redisConfig.User = hangfireOption.RedisUsername; - } - - if (!string.IsNullOrEmpty(hangfireOption.RedisPassword)) - { - redisConfig.Password = hangfireOption.RedisPassword; - } - - var connectionMultiplexer = ConnectionMultiplexer.Connect(redisConfig); - builder.Services.AddSingleton(connectionMultiplexer); - - builder.Services.AddHangfire( - (provider, config) => - { - config.UseRedisStorage( - connectionMultiplexer, - new RedisStorageOptions - { - Prefix = hangfireOption.RedisPrefix, - Db = hangfireOption.RedisDatabase, - } - ); - } - ); - - builder.Services.AddHangfireServer(options => - { - options.WorkerCount = hangfireOption.WorkerCount; - }); +if (hangfireOption?.IsEnabled == true) +{ + builder.Services.AddHangfireServices(hangfireOption); } // State recovery service @@ -242,7 +210,7 @@ var app = builder.Build(); // Hangfire dashboard -if (hangfireOption != null) +if (hangfireOption?.IsEnabled == true) { app.UseHangfireDashboard( hangfireOption.DashboardPath, diff --git a/Mimir/appsettings.json b/Mimir/appsettings.json index 8f8e923e..07912fa1 100644 --- a/Mimir/appsettings.json +++ b/Mimir/appsettings.json @@ -29,6 +29,7 @@ "ApiKeys": ["YOUR_CMC_API_KEY1", "YOUR_CMC_API_KEY2"] }, "Hangfire": { + "IsEnabled": false, "RedisHost": "localhost", "RedisPort": 6379, "RedisUsername": "",