using System; using System.Threading; using System.Threading.Tasks; using MediaBrowser.Controller.MediaEncoding; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using StackExchange.Redis; namespace Emby.Server.Implementations.MediaEncoding; /// /// Pings the configured Redis transcode session store once at startup so an unreachable store is /// reported there instead of being discovered as a silent loss of cross-pod takeover. /// public sealed class TranscodeStoreConnectivityProbe : IHostedService { private readonly IServiceProvider _serviceProvider; private readonly ILogger _logger; /// /// Initializes a new instance of the class. /// /// The service provider used to resolve the Redis connection. /// The logger. public TranscodeStoreConnectivityProbe(IServiceProvider serviceProvider, ILogger logger) { _serviceProvider = serviceProvider; _logger = logger; } /// public async Task StartAsync(CancellationToken cancellationToken) { try { // Resolved here rather than injected: connecting must not be able to abort startup. var redis = _serviceProvider.GetRequiredService(); var roundTrip = await redis.GetDatabase().PingAsync().ConfigureAwait(false); _logger.LogInformation( "Redis transcode session store is reachable ({RoundTripMs}ms round trip). HA transcode takeover is active.", (long)roundTrip.TotalMilliseconds); } catch (Exception ex) { _logger.LogError( ex, "Redis transcode session store is configured but UNREACHABLE. HA transcode takeover is not working: sessions stay local to this instance and are lost when it restarts. Check {Key}.", TranscodeStoreOptions.RedisConnectionStringKey); } } /// public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; }