optimize checkForInactiveStreams logic

This commit is contained in:
herby2212 2023-10-09 19:15:25 +02:00
parent 994619afb2
commit e8a05ad996

View File

@ -625,43 +625,37 @@ namespace Emby.Server.Implementations.Session
private async void CheckForInactiveSteams(object state) private async void CheckForInactiveSteams(object state)
{ {
var pausedSessions = Sessions.Where(i => var inactiveSessions = Sessions.Where(i =>
i.NowPlayingItem is not null i.NowPlayingItem is not null
&& i.PlayState.IsPaused && i.PlayState.IsPaused
&& i.LastPausedDate is not null) && (DateTime.UtcNow - i.LastPausedDate).Value.TotalMinutes > _config.Configuration.InactiveSessionThreshold);
.ToList();
if (pausedSessions.Count > 0) foreach (var session in inactiveSessions)
{ {
var inactiveSessions = pausedSessions.Where(i => (DateTime.UtcNow - i.LastPausedDate).Value.TotalMinutes > _config.Configuration.InactiveSessionThreshold).ToList(); _logger.LogDebug("Session {Session} has been inactive for {InactiveTime} minutes. Stopping it.", session.Id, _config.Configuration.InactiveSessionThreshold);
foreach (var session in inactiveSessions) try
{ {
_logger.LogDebug("Session {Session} has been inactive for {InactiveTime} minutes. Stopping it.", session.Id, _config.Configuration.InactiveSessionThreshold); await SendPlaystateCommand(
session.Id,
try session.Id,
{ new PlaystateRequest()
await SendPlaystateCommand( {
session.Id, Command = PlaystateCommand.Stop,
session.Id, ControllingUserId = session.UserId.ToString(),
new PlaystateRequest() SeekPositionTicks = session.PlayState?.PositionTicks
{ },
Command = PlaystateCommand.Stop, CancellationToken.None).ConfigureAwait(true);
ControllingUserId = session.UserId.ToString(), }
SeekPositionTicks = session.PlayState?.PositionTicks catch (Exception ex)
}, {
CancellationToken.None).ConfigureAwait(true); _logger.LogDebug(ex, "Error calling SendPlaystateCommand for stopping inactive session {Session}.", session.Id);
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Error calling SendPlaystateCommand for stopping inactive session {Session}.", session.Id);
}
} }
} }
var playingSessions = Sessions.Where(i => i.NowPlayingItem is not null) bool playingSessions = Sessions.Any(i => i.NowPlayingItem is not null);
.ToList();
if (playingSessions.Count == 0) if (!playingSessions)
{ {
StopInactiveCheckTimer(); StopInactiveCheckTimer();
} }