jellyfin-server/Emby.Server.Implementations/EntryPoints/AutomaticRestartEntryPoint.cs

126 lines
3.8 KiB
C#
Raw Normal View History

using System;
2014-01-11 20:13:18 +00:00
using System.Linq;
using System.Threading;
2016-06-19 06:18:29 +00:00
using System.Threading.Tasks;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
2016-06-14 06:40:21 +00:00
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Session;
2016-06-14 06:40:21 +00:00
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
2014-01-11 20:13:18 +00:00
namespace Emby.Server.Implementations.EntryPoints
2014-01-11 20:13:18 +00:00
{
public class AutomaticRestartEntryPoint : IServerEntryPoint
{
private readonly IServerApplicationHost _appHost;
private readonly ILogger _logger;
private readonly ITaskManager _iTaskManager;
private readonly ISessionManager _sessionManager;
2014-04-09 02:12:17 +00:00
private readonly IServerConfigurationManager _config;
2016-06-14 06:40:21 +00:00
private readonly ILiveTvManager _liveTvManager;
2014-01-11 20:13:18 +00:00
2019-02-05 08:49:46 +00:00
private Timer _timer;
2014-01-11 20:13:18 +00:00
2019-02-05 08:49:46 +00:00
public AutomaticRestartEntryPoint(IServerApplicationHost appHost, ILogger logger, ITaskManager iTaskManager, ISessionManager sessionManager, IServerConfigurationManager config, ILiveTvManager liveTvManager)
2014-01-11 20:13:18 +00:00
{
_appHost = appHost;
_logger = logger;
_iTaskManager = iTaskManager;
_sessionManager = sessionManager;
2014-04-09 02:12:17 +00:00
_config = config;
2016-06-14 06:40:21 +00:00
_liveTvManager = liveTvManager;
2014-01-11 20:13:18 +00:00
}
2019-01-27 14:40:37 +00:00
public Task RunAsync()
2014-01-11 20:13:18 +00:00
{
if (_appHost.CanSelfRestart)
{
_appHost.HasPendingRestartChanged += _appHost_HasPendingRestartChanged;
}
2019-01-27 14:40:37 +00:00
return Task.CompletedTask;
2014-01-11 20:13:18 +00:00
}
void _appHost_HasPendingRestartChanged(object sender, EventArgs e)
{
DisposeTimer();
if (_appHost.HasPendingRestart)
{
2019-02-05 08:49:46 +00:00
_timer = new Timer(TimerCallback, null, TimeSpan.FromMinutes(15), TimeSpan.FromMinutes(15));
2014-01-11 20:13:18 +00:00
}
}
2016-06-19 06:18:29 +00:00
private async void TimerCallback(object state)
2014-01-11 20:13:18 +00:00
{
2016-06-19 06:18:29 +00:00
if (_config.Configuration.EnableAutomaticRestart)
2014-01-11 20:13:18 +00:00
{
2016-06-19 06:18:29 +00:00
var isIdle = await IsIdle().ConfigureAwait(false);
2014-01-11 20:13:18 +00:00
2016-06-19 06:18:29 +00:00
if (isIdle)
2014-01-11 20:13:18 +00:00
{
2016-06-19 06:18:29 +00:00
DisposeTimer();
_logger.LogInformation("Automatically restarting the system because it is idle and a restart is required.");
2016-12-26 17:38:12 +00:00
2016-06-19 06:18:29 +00:00
try
{
_appHost.Restart();
}
catch (Exception ex)
{
2018-12-20 12:11:26 +00:00
_logger.LogError(ex, "Error restarting server");
2016-06-19 06:18:29 +00:00
}
2014-01-11 20:13:18 +00:00
}
}
}
2016-06-19 06:18:29 +00:00
private async Task<bool> IsIdle()
2014-01-11 20:13:18 +00:00
{
if (_iTaskManager.ScheduledTasks.Any(i => i.State != TaskState.Idle))
{
return false;
}
2016-06-14 06:40:21 +00:00
if (_liveTvManager.Services.Count == 1)
{
try
{
2016-06-19 06:18:29 +00:00
var timers = await _liveTvManager.GetTimers(new TimerQuery(), CancellationToken.None).ConfigureAwait(false);
2016-06-14 06:40:21 +00:00
if (timers.Items.Any(i => i.Status == RecordingStatus.InProgress))
{
return false;
}
}
catch (Exception ex)
{
2018-12-20 12:11:26 +00:00
_logger.LogError(ex, "Error getting timers");
2016-06-14 06:40:21 +00:00
}
}
2014-01-11 20:13:18 +00:00
var now = DateTime.UtcNow;
2014-04-16 02:17:48 +00:00
return !_sessionManager.Sessions.Any(i => (now - i.LastActivityDate).TotalMinutes < 30);
2014-01-11 20:13:18 +00:00
}
public void Dispose()
{
_appHost.HasPendingRestartChanged -= _appHost_HasPendingRestartChanged;
DisposeTimer();
}
private void DisposeTimer()
{
if (_timer != null)
{
_timer.Dispose();
_timer = null;
}
}
}
}