The Free Software Media System - Server Backend & API
added automatic restart option
Luke Pulverenti57a2a3098905
diff --git a/MediaBrowser.Model/Configuration/ServerConfiguration.cs b/MediaBrowser.Model/Configuration/ServerConfiguration.csindex b119bacdc5..bdf072b504 100644--- a/MediaBrowser.Model/Configuration/ServerConfiguration.cs+++ b/MediaBrowser.Model/Configuration/ServerConfiguration.cs@@ -222,6 +222,8 @@ namespace MediaBrowser.Model.Configurationpublic bool EnableDebugEncodingLogging { get; set; }public string TranscodingTempPath { get; set; }+ public bool EnableAutomaticRestart { get; set; }+/// <summary>/// Initializes a new instance of the <see cref="ServerConfiguration" /> class./// </summary>@@ -239,6 +241,7 @@ namespace MediaBrowser.Model.ConfigurationEnableMovieChapterImageExtraction = true;EnableEpisodeChapterImageExtraction = false;EnableOtherVideoChapterImageExtraction = false;+ EnableAutomaticRestart = true;MinResumePct = 5;MaxResumePct = 90;diff --git a/MediaBrowser.Server.Implementations/EntryPoints/AutomaticRestartEntryPoint.cs b/MediaBrowser.Server.Implementations/EntryPoints/AutomaticRestartEntryPoint.csnew file mode 100644index 0000000000..7e45716352--- /dev/null+++ b/MediaBrowser.Server.Implementations/EntryPoints/AutomaticRestartEntryPoint.cs@@ -0,0 +1,97 @@+using MediaBrowser.Common.ScheduledTasks;+using MediaBrowser.Controller;+using MediaBrowser.Controller.Plugins;+using MediaBrowser.Controller.Session;+using MediaBrowser.Model.Logging;+using MediaBrowser.Model.Tasks;+using System;+using System.Linq;+using System.Threading;++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;++ private Timer _timer;++ public AutomaticRestartEntryPoint(IServerApplicationHost appHost, ILogger logger, ITaskManager iTaskManager, ISessionManager sessionManager)+ {+ _appHost = appHost;+ _logger = logger;+ _iTaskManager = iTaskManager;+ _sessionManager = sessionManager;+ }++ public void Run()+ {+ if (_appHost.CanSelfRestart)+ {+ _appHost.HasPendingRestartChanged += _appHost_HasPendingRestartChanged;+ }+ }++ void _appHost_HasPendingRestartChanged(object sender, EventArgs e)+ {+ DisposeTimer();++ if (_appHost.HasPendingRestart)+ {+ _timer = new Timer(TimerCallback, null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5));+ }+ }++ private void TimerCallback(object state)+ {+ if (IsIdle())+ {+ 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;+ }++ var now = DateTime.UtcNow;+ if (_sessionManager.Sessions.Any(i => !string.IsNullOrEmpty(i.NowViewingItemName) || (now - i.LastActivityDate).TotalMinutes < 30))+ {+ return false;+ }++ return true;+ }++ public void Dispose()+ {+ _appHost.HasPendingRestartChanged -= _appHost_HasPendingRestartChanged;++ DisposeTimer();+ }++ private void DisposeTimer()+ {+ if (_timer != null)+ {+ _timer.Dispose();+ _timer = null;+ }+ }+ }+}diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csprojindex 6513446b7e..f1e0d6a8db 100644--- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj+++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj@@ -110,6 +110,7 @@<Compile Include="Drawing\PlayedIndicatorDrawer.cs" /><Compile Include="Drawing\UnplayedCountIndicator.cs" /><Compile Include="Dto\DtoService.cs" />+ <Compile Include="EntryPoints\AutomaticRestartEntryPoint.cs" /><Compile Include="EntryPoints\LibraryChangedNotifier.cs" /><Compile Include="EntryPoints\LoadRegistrations.cs" /><Compile Include="EntryPoints\Notifications\Notifier.cs" />