Skip to content

Commit

Permalink
fix lifecycle + use AddGrainStore
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenlautier committed Feb 14, 2024
1 parent e46322d commit 0a3b558
Showing 1 changed file with 35 additions and 38 deletions.
73 changes: 35 additions & 38 deletions Orleans.Persistence.Redis/Config/RedisSiloHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
#nullable enable
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
Expand All @@ -9,8 +9,7 @@
using Orleans.Persistence.Redis.Config;
using Orleans.Persistence.Redis.Core;
using Orleans.Persistence.Redis.Serialization;
using Orleans.Providers;
using Orleans.Runtime;
using Orleans.Runtime.Hosting;
using Orleans.Serialization;
using Orleans.Storage;
using JsonSerializer = Orleans.Persistence.Redis.Serialization.JsonSerializer;
Expand All @@ -21,10 +20,8 @@ namespace Orleans.Hosting;

public static class RedisSiloBuilderExtensions
{
public static RedisStorageOptionsBuilder AddRedisGrainStorage(
this ISiloBuilder builder,
string name
) => new RedisStorageOptionsBuilder(builder, name);
public static RedisStorageOptionsBuilder AddRedisGrainStorage(this ISiloBuilder builder, string name)
=> new(builder, name);

public static RedisStorageOptionsBuilder AddRedisGrainStorageAsDefault(
this ISiloBuilder builder
Expand All @@ -33,39 +30,35 @@ this ISiloBuilder builder
internal static IServiceCollection AddRedisGrainStorage(
this IServiceCollection services,
string name,
Action<OptionsBuilder<RedisStorageOptions>> configureOptions = null
Action<OptionsBuilder<RedisStorageOptions>>? configureOptions = null
)
{
configureOptions?.Invoke(services.AddOptions<RedisStorageOptions>(name));
// services.AddTransient<IConfigurationValidator>(sp => new DynamoDBGrainStorageOptionsValidator(sp.GetService<IOptionsSnapshot<RedisStorageOptions>>().Get(name), name));
services.AddKeyedSingleton<IGrainStateStore>(name, (sp, k) =>
{
var key = (string)k;
var connection = sp.GetRequiredKeyedService<DbConnection>(key);
var serializer = sp.GetRequiredKeyedService<ISerializer>(key);
var humanReadableSerializer = sp.GetKeyedService<IHumanReadableSerializer>(key);
var options = sp.GetRequiredService<IOptionsSnapshot<RedisStorageOptions>>();
var logger = sp.GetRequiredService<ILogger<GrainStateStore>>();

return ActivatorUtilities.CreateInstance<GrainStateStore>(
sp,
key,
connection,
options.Get(key),
serializer,
humanReadableSerializer,
logger,
sp
);
}
);
services.ConfigureNamedOptionForLogging<RedisStorageOptions>(name);
services.TryAddSingleton(sp =>
sp.GetKeyedService<IGrainStorage>(ProviderConstants.DEFAULT_STORAGE_PROVIDER_NAME));

// services.AddTransient<IConfigurationValidator>(sp => new DynamoDBGrainStorageOptionsValidator(sp.GetService<IOptionsSnapshot<RedisStorageOptions>>().Get(name), name));
return services

//.AddKeyedSingleton(name, (sp, key) => CreateRedisStorage(sp, key as string))
.AddKeyedSingleton<IGrainStateStore>(name, (sp, k) =>
{
var key = (string)k;
var connection = sp.GetRequiredKeyedService<DbConnection>(key);
var serializer = sp.GetRequiredKeyedService<ISerializer>(key);
var humanReadableSerializer = sp.GetKeyedService<IHumanReadableSerializer>(key);
var options = sp.GetRequiredService<IOptionsSnapshot<RedisStorageOptions>>();
var logger = sp.GetRequiredService<ILogger<GrainStateStore>>();

return ActivatorUtilities.CreateInstance<GrainStateStore>(
sp,
key,
connection,
options.Get(key),
serializer,
humanReadableSerializer,
logger,
sp
);
}
)
.AddKeyedSingleton(name, (sp, k) =>
{
var key = (string)k;
Expand All @@ -79,10 +72,14 @@ internal static IServiceCollection AddRedisGrainStorage(
var store = sp.GetRequiredKeyedService<IGrainStateStore>(key);
var connection = sp.GetRequiredKeyedService<DbConnection>(key);
return ActivatorUtilities.CreateInstance<RedisGrainStorage>(sp, key, store, connection);
}
)
.AddGrainStorage(name, (sp, key) =>
{
var store = sp.GetRequiredKeyedService<IGrainStateStore>(key);
var connection = sp.GetRequiredKeyedService<DbConnection>(key);
return ActivatorUtilities.CreateInstance<RedisGrainStorage>(sp, key, store, connection);
})
.AddKeyedSingleton(name, (sp, n) =>
(ILifecycleParticipant<ISiloLifecycle>)sp.GetRequiredKeyedService<IGrainStorage>(n as string))
//.AddGrainStorage(name, )
;
}

Expand Down

0 comments on commit 0a3b558

Please sign in to comment.