UsageEntryPoint.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Implementations.Security;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Controller.Plugins;
  5. using MediaBrowser.Model.Logging;
  6. using System;
  7. using System.Threading;
  8. namespace MediaBrowser.Server.Implementations.EntryPoints
  9. {
  10. /// <summary>
  11. /// Class UsageEntryPoint
  12. /// </summary>
  13. public class UsageEntryPoint : IServerEntryPoint
  14. {
  15. private readonly IApplicationHost _applicationHost;
  16. private readonly INetworkManager _networkManager;
  17. private readonly IHttpClient _httpClient;
  18. private readonly ILogger _logger;
  19. private Timer _timer;
  20. private readonly TimeSpan _frequency = TimeSpan.FromHours(24);
  21. public UsageEntryPoint(ILogger logger, IApplicationHost applicationHost, INetworkManager networkManager, IHttpClient httpClient)
  22. {
  23. _logger = logger;
  24. _applicationHost = applicationHost;
  25. _networkManager = networkManager;
  26. _httpClient = httpClient;
  27. }
  28. public void Run()
  29. {
  30. _timer = new Timer(OnTimerFired, null, TimeSpan.FromMilliseconds(5000), _frequency);
  31. }
  32. /// <summary>
  33. /// Called when [timer fired].
  34. /// </summary>
  35. /// <param name="state">The state.</param>
  36. private async void OnTimerFired(object state)
  37. {
  38. try
  39. {
  40. await new UsageReporter(_applicationHost, _networkManager, _httpClient).ReportUsage(CancellationToken.None)
  41. .ConfigureAwait(false);
  42. }
  43. catch (Exception ex)
  44. {
  45. _logger.ErrorException("Error sending anonymous usage statistics.", ex);
  46. }
  47. }
  48. public void Dispose()
  49. {
  50. if (_timer != null)
  51. {
  52. _timer.Dispose();
  53. _timer = null;
  54. }
  55. }
  56. }
  57. }