AutomaticRestartEntryPoint.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Plugins;
  4. using MediaBrowser.Controller.Session;
  5. using MediaBrowser.Model.Logging;
  6. using MediaBrowser.Model.Tasks;
  7. using System;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using MediaBrowser.Controller.LiveTv;
  12. using MediaBrowser.Model.LiveTv;
  13. using MediaBrowser.Model.Threading;
  14. namespace Emby.Server.Implementations.EntryPoints
  15. {
  16. public class AutomaticRestartEntryPoint : IServerEntryPoint
  17. {
  18. private readonly IServerApplicationHost _appHost;
  19. private readonly ILogger _logger;
  20. private readonly ITaskManager _iTaskManager;
  21. private readonly ISessionManager _sessionManager;
  22. private readonly IServerConfigurationManager _config;
  23. private readonly ILiveTvManager _liveTvManager;
  24. private readonly ITimerFactory _timerFactory;
  25. private ITimer _timer;
  26. public AutomaticRestartEntryPoint(IServerApplicationHost appHost, ILogger logger, ITaskManager iTaskManager, ISessionManager sessionManager, IServerConfigurationManager config, ILiveTvManager liveTvManager, ITimerFactory timerFactory)
  27. {
  28. _appHost = appHost;
  29. _logger = logger;
  30. _iTaskManager = iTaskManager;
  31. _sessionManager = sessionManager;
  32. _config = config;
  33. _liveTvManager = liveTvManager;
  34. _timerFactory = timerFactory;
  35. }
  36. public void Run()
  37. {
  38. if (_appHost.CanSelfRestart)
  39. {
  40. _appHost.HasPendingRestartChanged += _appHost_HasPendingRestartChanged;
  41. }
  42. }
  43. void _appHost_HasPendingRestartChanged(object sender, EventArgs e)
  44. {
  45. DisposeTimer();
  46. if (_appHost.HasPendingRestart)
  47. {
  48. _timer = _timerFactory.Create(TimerCallback, null, TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10));
  49. }
  50. }
  51. private async void TimerCallback(object state)
  52. {
  53. if (_config.Configuration.EnableAutomaticRestart)
  54. {
  55. var isIdle = await IsIdle().ConfigureAwait(false);
  56. if (isIdle)
  57. {
  58. DisposeTimer();
  59. try
  60. {
  61. _appHost.Restart();
  62. }
  63. catch (Exception ex)
  64. {
  65. _logger.ErrorException("Error restarting server", ex);
  66. }
  67. }
  68. }
  69. }
  70. private async Task<bool> IsIdle()
  71. {
  72. if (_iTaskManager.ScheduledTasks.Any(i => i.State != TaskState.Idle))
  73. {
  74. return false;
  75. }
  76. if (_liveTvManager.Services.Count == 1)
  77. {
  78. try
  79. {
  80. var timers = await _liveTvManager.GetTimers(new TimerQuery(), CancellationToken.None).ConfigureAwait(false);
  81. if (timers.Items.Any(i => i.Status == RecordingStatus.InProgress))
  82. {
  83. return false;
  84. }
  85. }
  86. catch (Exception ex)
  87. {
  88. _logger.ErrorException("Error getting timers", ex);
  89. }
  90. }
  91. var now = DateTime.UtcNow;
  92. return !_sessionManager.Sessions.Any(i => (now - i.LastActivityDate).TotalMinutes < 30);
  93. }
  94. public void Dispose()
  95. {
  96. _appHost.HasPendingRestartChanged -= _appHost_HasPendingRestartChanged;
  97. DisposeTimer();
  98. }
  99. private void DisposeTimer()
  100. {
  101. if (_timer != null)
  102. {
  103. _timer.Dispose();
  104. _timer = null;
  105. }
  106. }
  107. }
  108. }