diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2014-01-11 15:13:18 -0500 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2014-01-11 15:13:18 -0500 |
| commit | 57a2a309890506a26e80707380851b5ae5e40044 (patch) | |
| tree | 8f4b2ffbbf502ca4a7372fcf963a543960d52795 /MediaBrowser.Server.Implementations/EntryPoints | |
| parent | 4d80f04e211b62c1628555988387d21f28c76c59 (diff) | |
added automatic restart option
Diffstat (limited to 'MediaBrowser.Server.Implementations/EntryPoints')
| -rw-r--r-- | MediaBrowser.Server.Implementations/EntryPoints/AutomaticRestartEntryPoint.cs | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/MediaBrowser.Server.Implementations/EntryPoints/AutomaticRestartEntryPoint.cs b/MediaBrowser.Server.Implementations/EntryPoints/AutomaticRestartEntryPoint.cs new file mode 100644 index 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; + } + } + } +} |
