Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Redis Health Check #13589

Merged
merged 25 commits into from
Apr 10, 2024
Merged

Add Redis Health Check #13589

merged 25 commits into from
Apr 10, 2024

Conversation

hishamco
Copy link
Member

@hishamco hishamco commented Apr 22, 2023

Part of #13588.

@hishamco hishamco mentioned this pull request Apr 22, 2023
8 tasks
@hishamco hishamco requested a review from jtkech April 22, 2023 20:30
@hishamco
Copy link
Member Author

@jtkech unable to resolve service for type OrchardCore.Redis.IRedisService

@jtkech
Copy link
Member

jtkech commented May 14, 2023

Maybe it failed to configure RedisOptions, in that case IRedisService is not registered and you may have a logged error message.

services.Configure<RedisOptions>(options =>
{
options.Configuration = configuration;
options.ConfigurationOptions = configurationOptions;
options.InstancePrefix = instancePrefix;
});
}
catch (Exception e)
{
_logger.LogError(e, "'Redis' features are not active on tenant '{TenantName}' as the 'Configuration' string is missing or invalid.", _tenant);
return;
}
services.AddSingleton<IRedisService, RedisService>();

The other Redis features check if IRedisService is registered, they can do that because they depend on the main OC.Redis feature (manifest dependencies) so their related startups execute after.

if (services.Any(d => d.ServiceType == typeof(IRedisService)))
{
services.AddSingleton<IDistributedCache, RedisCacheWrapper>();
services.AddTransient<IConfigureOptions<RedisCacheOptions>, RedisCacheOptionsSetup>();
services.AddScoped<ITagCache, RedisTagCache>();
}

In the HealthChecks startup we could override the Order property to a quite high value and then check if IRedisService is registered as done in the other startups.

Hmm, but I think it's better to update RedisHealthCheck to not inject IRedisService but ServiceProvider, then resolve lazily IRedisService by not using GetRequiredService() but by using .GetService() which doesn't throw, then if it is null it would mean that it is Unhealthy.

Does PingAsync() can throw an exception? If so you may need to use a try/catch block.

@hishamco
Copy link
Member Author

Hmm, but I think it's better to update RedisHealthCheck to not inject IRedisService but ServiceProvider

This is may be a better solution but I'm not sure why it can't be retrieved from DI directly, let me check and come back to you

@jtkech
Copy link
Member

jtkech commented May 14, 2023

but I'm not sure why it can't be retrieved from DI directly

See my previous comment and look at the Redis Startup to see how it is done.

Maybe it failed to configure RedisOptions, in that case IRedisService is not registered and you may have a logged error message.

@hishamco
Copy link
Member Author

Everything works as expected, the RedisOptions wasn't set previously, I didn't think it will break IRedisService

@hishamco hishamco requested a review from jtkech May 14, 2023 21:09
@hishamco
Copy link
Member Author

We might need to add abortConnect=false to the redis connection

@hishamco
Copy link
Member Author

@jtkech could you please have a quick review once you have time

[RequireFeatures("OrchardCore.HealthChecks")]
public class Startup : StartupBase
{
public override int Order => 100;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment on why this is necessary, and what's the reason for 100 in particular.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might need to check this one, I remember this might be necessary to run the health check early

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, also document what's the reason for 100 in particular. This is a magic number; can it be 90? Can it be 110? Why not 1000?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be, no particular reason for using 10 but usually we're using 100 or more to avoid start ordering issue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so please document it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW there are many places where we use 1000 for instance without docs :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And those should have been documented as well :). Having less-than-ideal pieces of code currently doesn't mean that we should keep writing the same.

@hishamco hishamco requested a review from agriffard as a code owner March 27, 2024 02:05
@hishamco hishamco requested a review from Piedone March 27, 2024 02:06
@hishamco hishamco removed request for agriffard and jtkech March 27, 2024 02:06
src/docs/reference/modules/Redis/README.md Outdated Show resolved Hide resolved
[RequireFeatures("OrchardCore.HealthChecks")]
public class Startup : StartupBase
{
public override int Order => 100;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, also document what's the reason for 100 in particular. This is a magic number; can it be 90? Can it be 110? Why not 1000?

@hishamco
Copy link
Member Author

Let's finalize the health checks PRs, so I can focus to finishing the others

@hishamco hishamco requested a review from Piedone March 29, 2024 21:41
@hishamco hishamco requested a review from Piedone April 1, 2024 02:40
Copy link
Member

@Piedone Piedone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hishamco
Copy link
Member Author

hishamco commented Apr 1, 2024

I already added a comment, as I said before 100 does not matter, the important thing is to choose an order greater than 0

@Piedone
Copy link
Member

Piedone commented Apr 1, 2024

Great, all clear, now add this to the comment :).

@hishamco
Copy link
Member Author

hishamco commented Apr 1, 2024

Seems the added comment is not enough, time to modify :)

@hishamco hishamco requested a review from Piedone April 1, 2024 19:35
Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
@Piedone Piedone merged commit 7296191 into main Apr 10, 2024
5 checks passed
@Piedone Piedone deleted the hishamco/redis-health-check branch April 10, 2024 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants