Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Mimir/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<IConnectionMultiplexer>(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;
}
}
2 changes: 2 additions & 0 deletions Mimir/Options/HangfireOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 6 additions & 38 deletions Mimir/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -121,47 +122,14 @@

// ~MongoDB repositories.

// Hangfire and Redis configuration
// Hangfire configuration
var hangfireOption = builder
.Configuration.GetSection(HangfireOption.SectionName)
.Get<HangfireOption>();
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<IConnectionMultiplexer>(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
Expand Down Expand Up @@ -242,7 +210,7 @@
var app = builder.Build();

// Hangfire dashboard
if (hangfireOption != null)
if (hangfireOption?.IsEnabled == true)
{
app.UseHangfireDashboard(
hangfireOption.DashboardPath,
Expand Down
1 change: 1 addition & 0 deletions Mimir/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"ApiKeys": ["YOUR_CMC_API_KEY1", "YOUR_CMC_API_KEY2"]
},
"Hangfire": {
"IsEnabled": false,
"RedisHost": "localhost",
"RedisPort": 6379,
"RedisUsername": "",
Expand Down
Loading