AutomaticRestartEntryPoint.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Controller;
  6. using MediaBrowser.Controller.Configuration;
  7. using MediaBrowser.Controller.LiveTv;
  8. using MediaBrowser.Controller.Plugins;
  9. using MediaBrowser.Controller.Session;
  10. using MediaBrowser.Model.LiveTv;
  11. using MediaBrowser.Model.Tasks;
  12. using MediaBrowser.Model.Threading;
  13. using Microsoft.Extensions.Logging;
  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(15), TimeSpan.FromMinutes(15));
  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. _logger.LogInformation("Automatically restarting the system because it is idle and a restart is required.");
  60. try
  61. {
  62. _appHost.Restart();
  63. }
  64. catch (Exception ex)
  65. {
  66. _logger.LogError(ex, "Error restarting server");
  67. }
  68. }
  69. }
  70. }
  71. private async Task<bool> IsIdle()
  72. {
  73. if (_iTaskManager.ScheduledTasks.Any(i => i.State != TaskState.Idle))
  74. {
  75. return false;
  76. }
  77. if (_liveTvManager.Services.Count == 1)
  78. {
  79. try
  80. {
  81. var timers = await _liveTvManager.GetTimers(new TimerQuery(), CancellationToken.None).ConfigureAwait(false);
  82. if (timers.Items.Any(i => i.Status == RecordingStatus.InProgress))
  83. {
  84. return false;
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. _logger.LogError(ex, "Error getting timers");
  90. }
  91. }
  92. var now = DateTime.UtcNow;
  93. return !_sessionManager.Sessions.Any(i => (now - i.LastActivityDate).TotalMinutes < 30);
  94. }
  95. public void Dispose()
  96. {
  97. _appHost.HasPendingRestartChanged -= _appHost_HasPendingRestartChanged;
  98. DisposeTimer();
  99. }
  100. private void DisposeTimer()
  101. {
  102. if (_timer != null)
  103. {
  104. _timer.Dispose();
  105. _timer = null;
  106. }
  107. }
  108. }
  109. }