95 lines
4.2 KiB
C#
95 lines
4.2 KiB
C#
using System;
|
|
using Emby.Server.Implementations.Session;
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
using MediaBrowser.Controller.Session;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using StackExchange.Redis;
|
|
|
|
namespace Jellyfin.Server.Extensions;
|
|
|
|
/// <summary>
|
|
/// Extensions for registering the session directory and the instance-addressed message bus.
|
|
/// </summary>
|
|
public static class SessionDirectoryServiceCollectionExtensions
|
|
{
|
|
/// <summary>
|
|
/// Registers the session directory and message bus, Redis-backed when a connection string is
|
|
/// configured and no-op otherwise, and reports the selection at <see cref="LogLevel.Information"/>.
|
|
/// </summary>
|
|
/// <param name="serviceCollection">The service collection.</param>
|
|
/// <param name="configuration">The configuration to read <c>Jellyfin:SessionDirectory</c> from.</param>
|
|
/// <param name="logger">The logger to report the selection on.</param>
|
|
/// <returns>The updated service collection.</returns>
|
|
public static IServiceCollection AddSessionDirectory(
|
|
this IServiceCollection serviceCollection,
|
|
IConfiguration configuration,
|
|
ILogger logger)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(configuration);
|
|
ArgumentNullException.ThrowIfNull(logger);
|
|
|
|
serviceCollection.Configure<SessionDirectoryOptions>(configuration.GetSection(SessionDirectoryOptions.ConfigurationSection));
|
|
|
|
if (string.IsNullOrEmpty(configuration[TranscodeStoreOptions.RedisConnectionStringKey]))
|
|
{
|
|
logger.LogInformation(
|
|
"Session directory: {Directory}. The session list and remote control only reach the sessions this instance holds; set {Key} to share them.",
|
|
nameof(NullSessionDirectory),
|
|
TranscodeStoreOptions.RedisConnectionStringKey);
|
|
}
|
|
else
|
|
{
|
|
logger.LogInformation(
|
|
"Session directory: {Directory}. Sessions are visible to, and controllable from, every instance.",
|
|
nameof(RedisSessionDirectory));
|
|
|
|
serviceCollection.AddSingleton(SharedSessionServices.Create);
|
|
}
|
|
|
|
serviceCollection.AddSingleton<IPodMessageBus>(sp => sp.GetService<SharedSessionServices>()?.Bus ?? NullPodMessageBus.Instance);
|
|
|
|
return serviceCollection.AddSingleton<ISessionDirectory>(sp => sp.GetService<SharedSessionServices>()?.Directory ?? NullSessionDirectory.Instance);
|
|
}
|
|
|
|
// A Redis directory paired with a no-op bus would leave every routed command permanently
|
|
// undeliverable, so the pair is built together and falls back together.
|
|
private sealed class SharedSessionServices
|
|
{
|
|
private SharedSessionServices(ISessionDirectory directory, IPodMessageBus bus)
|
|
{
|
|
Directory = directory;
|
|
Bus = bus;
|
|
}
|
|
|
|
public ISessionDirectory Directory { get; }
|
|
|
|
public IPodMessageBus Bus { get; }
|
|
|
|
// Fail open: an unreachable Redis degrades to the single-instance behaviour rather than aborting startup.
|
|
public static SharedSessionServices Create(IServiceProvider serviceProvider)
|
|
{
|
|
try
|
|
{
|
|
var redis = serviceProvider.GetRequiredService<IConnectionMultiplexer>();
|
|
var options = serviceProvider.GetRequiredService<IOptions<SessionDirectoryOptions>>();
|
|
|
|
return new SharedSessionServices(
|
|
new RedisSessionDirectory(redis, options, serviceProvider.GetRequiredService<ILogger<RedisSessionDirectory>>()),
|
|
new RedisPodMessageBus(redis, options, PodIdentity.Current, serviceProvider.GetRequiredService<ILogger<RedisPodMessageBus>>()));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
serviceProvider.GetRequiredService<ILogger<CoreAppHost>>().LogError(
|
|
ex,
|
|
"Redis is configured but unavailable, so sessions will not be shared between instances. Check {Key}.",
|
|
TranscodeStoreOptions.RedisConnectionStringKey);
|
|
|
|
return new SharedSessionServices(NullSessionDirectory.Instance, NullPodMessageBus.Instance);
|
|
}
|
|
}
|
|
}
|
|
}
|