using System;
using Emby.Server.Implementations.QuickConnect;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.QuickConnect;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
namespace Jellyfin.Server.Extensions;
///
/// Extensions for registering the quick connect store.
///
public static class QuickConnectStoreServiceCollectionExtensions
{
///
/// Registers the quick connect store, Redis-backed when a connection string is configured and
/// process-local otherwise, and reports the selected store at .
///
///
/// The connection string is only set for a multi-instance deployment, which is the only shape where
/// the initiate, authorize and exchange legs of one flow can land on different instances.
///
/// The service collection.
/// The configuration to read the Redis connection string from.
/// The logger to report the selected store on.
/// The updated service collection.
public static IServiceCollection AddQuickConnectStore(
this IServiceCollection serviceCollection,
IConfiguration configuration,
ILogger logger)
{
ArgumentNullException.ThrowIfNull(configuration);
ArgumentNullException.ThrowIfNull(logger);
if (string.IsNullOrEmpty(configuration[TranscodeStoreOptions.RedisConnectionStringKey]))
{
logger.LogInformation(
"Quick connect store: {Store}. A quick connect flow has to complete against one instance; set {Key} to share it.",
nameof(InMemoryQuickConnectStore),
TranscodeStoreOptions.RedisConnectionStringKey);
return serviceCollection.AddSingleton();
}
logger.LogInformation(
"Quick connect store: {Store}. Quick connect flows complete across any instance.",
nameof(RedisQuickConnectStore));
return serviceCollection.AddSingleton(sp =>
{
try
{
return new RedisQuickConnectStore(
sp.GetRequiredService(),
sp.GetRequiredService>());
}
catch (Exception ex)
{
// Fail open: an unreachable Redis degrades to the single-instance behaviour of a flow
// having to complete against one instance, rather than taking quick connect down.
sp.GetRequiredService>().LogError(
ex,
"Redis is configured but unavailable, so quick connect flows will not complete across instances. Check {Key}.",
TranscodeStoreOptions.RedisConnectionStringKey);
return new InMemoryQuickConnectStore();
}
});
}
}