2014-01-11 13:13:18 -07:00
|
|
|
|
using MediaBrowser.Common.ScheduledTasks;
|
|
|
|
|
using MediaBrowser.Controller;
|
2014-04-08 19:12:17 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-01-11 13:13:18 -07:00
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Tasks;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2016-06-13 23:40:21 -07:00
|
|
|
|
using MediaBrowser.Controller.LiveTv;
|
|
|
|
|
using MediaBrowser.Model.LiveTv;
|
2014-01-11 13:13:18 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.EntryPoints
|
|
|
|
|
{
|
|
|
|
|
public class AutomaticRestartEntryPoint : IServerEntryPoint
|
|
|
|
|
{
|
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly ITaskManager _iTaskManager;
|
|
|
|
|
private readonly ISessionManager _sessionManager;
|
2014-04-08 19:12:17 -07:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2016-06-13 23:40:21 -07:00
|
|
|
|
private readonly ILiveTvManager _liveTvManager;
|
2014-01-11 13:13:18 -07:00
|
|
|
|
|
|
|
|
|
private Timer _timer;
|
|
|
|
|
|
2016-06-13 23:40:21 -07:00
|
|
|
|
public AutomaticRestartEntryPoint(IServerApplicationHost appHost, ILogger logger, ITaskManager iTaskManager, ISessionManager sessionManager, IServerConfigurationManager config, ILiveTvManager liveTvManager)
|
2014-01-11 13:13:18 -07:00
|
|
|
|
{
|
|
|
|
|
_appHost = appHost;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_iTaskManager = iTaskManager;
|
|
|
|
|
_sessionManager = sessionManager;
|
2014-04-08 19:12:17 -07:00
|
|
|
|
_config = config;
|
2016-06-13 23:40:21 -07:00
|
|
|
|
_liveTvManager = liveTvManager;
|
2014-01-11 13:13:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
|
{
|
|
|
|
|
if (_appHost.CanSelfRestart)
|
|
|
|
|
{
|
|
|
|
|
_appHost.HasPendingRestartChanged += _appHost_HasPendingRestartChanged;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _appHost_HasPendingRestartChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
DisposeTimer();
|
|
|
|
|
|
|
|
|
|
if (_appHost.HasPendingRestart)
|
|
|
|
|
{
|
2016-06-13 23:40:21 -07:00
|
|
|
|
_timer = new Timer(TimerCallback, null, TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10));
|
2014-01-11 13:13:18 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TimerCallback(object state)
|
|
|
|
|
{
|
2014-04-08 19:12:17 -07:00
|
|
|
|
if (_config.Configuration.EnableAutomaticRestart && IsIdle())
|
2014-01-11 13:13:18 -07:00
|
|
|
|
{
|
|
|
|
|
DisposeTimer();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_appHost.Restart();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error restarting server", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsIdle()
|
|
|
|
|
{
|
|
|
|
|
if (_iTaskManager.ScheduledTasks.Any(i => i.State != TaskState.Idle))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-13 23:40:21 -07:00
|
|
|
|
if (_liveTvManager.Services.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var timers = _liveTvManager.GetTimers(new TimerQuery(), CancellationToken.None).Result;
|
|
|
|
|
if (timers.Items.Any(i => i.Status == RecordingStatus.InProgress))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting timers", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-11 13:13:18 -07:00
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
|
2014-04-15 19:17:48 -07:00
|
|
|
|
return !_sessionManager.Sessions.Any(i => (now - i.LastActivityDate).TotalMinutes < 30);
|
2014-01-11 13:13:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_appHost.HasPendingRestartChanged -= _appHost_HasPendingRestartChanged;
|
|
|
|
|
|
|
|
|
|
DisposeTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DisposeTimer()
|
|
|
|
|
{
|
|
|
|
|
if (_timer != null)
|
|
|
|
|
{
|
|
|
|
|
_timer.Dispose();
|
|
|
|
|
_timer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|