From 4ebba2b2e87e33f083c095957a2294b6f8ae3828 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 15 Feb 2014 17:42:06 -0500 Subject: change usage reporting to a timer --- .../EntryPoints/UsageEntryPoint.cs | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 MediaBrowser.Server.Implementations/EntryPoints/UsageEntryPoint.cs (limited to 'MediaBrowser.Server.Implementations/EntryPoints/UsageEntryPoint.cs') diff --git a/MediaBrowser.Server.Implementations/EntryPoints/UsageEntryPoint.cs b/MediaBrowser.Server.Implementations/EntryPoints/UsageEntryPoint.cs new file mode 100644 index 000000000..3ab47f51b --- /dev/null +++ b/MediaBrowser.Server.Implementations/EntryPoints/UsageEntryPoint.cs @@ -0,0 +1,63 @@ +using MediaBrowser.Common; +using MediaBrowser.Common.Implementations.Security; +using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Plugins; +using MediaBrowser.Model.Logging; +using System; +using System.Threading; + +namespace MediaBrowser.Server.Implementations.EntryPoints +{ + /// + /// Class UsageEntryPoint + /// + public class UsageEntryPoint : IServerEntryPoint + { + private readonly IApplicationHost _applicationHost; + private readonly INetworkManager _networkManager; + private readonly IHttpClient _httpClient; + private readonly ILogger _logger; + + private Timer _timer; + private readonly TimeSpan _frequency = TimeSpan.FromHours(24); + + public UsageEntryPoint(ILogger logger, IApplicationHost applicationHost, INetworkManager networkManager, IHttpClient httpClient) + { + _logger = logger; + _applicationHost = applicationHost; + _networkManager = networkManager; + _httpClient = httpClient; + } + + public void Run() + { + _timer = new Timer(OnTimerFired, null, TimeSpan.FromMilliseconds(5000), _frequency); + } + + /// + /// Called when [timer fired]. + /// + /// The state. + private async void OnTimerFired(object state) + { + try + { + await new UsageReporter(_applicationHost, _networkManager, _httpClient).ReportUsage(CancellationToken.None) + .ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.ErrorException("Error sending anonymous usage statistics.", ex); + } + } + + public void Dispose() + { + if (_timer != null) + { + _timer.Dispose(); + _timer = null; + } + } + } +} -- cgit v1.2.3