2
0

AutomaticRestartEntryPoint.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 Task RunAsync()
  37. {
  38. if (_appHost.CanSelfRestart)
  39. {
  40. _appHost.HasPendingRestartChanged += _appHost_HasPendingRestartChanged;
  41. }
  42. return Task.CompletedTask;
  43. }
  44. void _appHost_HasPendingRestartChanged(object sender, EventArgs e)
  45. {
  46. DisposeTimer();
  47. if (_appHost.HasPendingRestart)
  48. {
  49. _timer = _timerFactory.Create(TimerCallback, null, TimeSpan.FromMinutes(15), TimeSpan.FromMinutes(15));
  50. }
  51. }
  52. private async void TimerCallback(object state)
  53. {
  54. if (_config.Configuration.EnableAutomaticRestart)
  55. {
  56. var isIdle = await IsIdle().ConfigureAwait(false);
  57. if (isIdle)
  58. {
  59. DisposeTimer();
  60. _logger.LogInformation("Automatically restarting the system because it is idle and a restart is required.");
  61. try
  62. {
  63. _appHost.Restart();
  64. }
  65. catch (Exception ex)
  66. {
  67. _logger.LogError(ex, "Error restarting server");
  68. }
  69. }
  70. }
  71. }
  72. private async Task<bool> IsIdle()
  73. {
  74. if (_iTaskManager.ScheduledTasks.Any(i => i.State != TaskState.Idle))
  75. {
  76. return false;
  77. }
  78. if (_liveTvManager.Services.Count == 1)
  79. {
  80. try
  81. {
  82. var timers = await _liveTvManager.GetTimers(new TimerQuery(), CancellationToken.None).ConfigureAwait(false);
  83. if (timers.Items.Any(i => i.Status == RecordingStatus.InProgress))
  84. {
  85. return false;
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. _logger.LogError(ex, "Error getting timers");
  91. }
  92. }
  93. var now = DateTime.UtcNow;
  94. return !_sessionManager.Sessions.Any(i => (now - i.LastActivityDate).TotalMinutes < 30);
  95. }
  96. public void Dispose()
  97. {
  98. _appHost.HasPendingRestartChanged -= _appHost_HasPendingRestartChanged;
  99. DisposeTimer();
  100. }
  101. private void DisposeTimer()
  102. {
  103. if (_timer != null)
  104. {
  105. _timer.Dispose();
  106. _timer = null;
  107. }
  108. }
  109. }
  110. }