diff --git a/Moesif.Middleware/Helpers/Tasks.cs b/Moesif.Middleware/Helpers/Tasks.cs index d51020c..f044758 100644 --- a/Moesif.Middleware/Helpers/Tasks.cs +++ b/Moesif.Middleware/Helpers/Tasks.cs @@ -43,37 +43,48 @@ public List QueueGetAll(Queue moesifQueue, int batchSize if ((batchEvents.Any())) { - // Send Batch Request - var createBatchEventResponse = await client.Api.CreateEventsBatchAsync(batchEvents); - var batchEventResponseConfigETag = createBatchEventResponse.ToDictionary(k => k.Key.ToLower(), k => k.Value)["x-moesif-config-etag"]; - - if (!(string.IsNullOrEmpty(batchEventResponseConfigETag)) && - !(string.IsNullOrEmpty(configETag)) && - configETag != batchEventResponseConfigETag && - DateTime.UtcNow > lastUpdatedTime.AddMinutes(5)) + try { - try + // Send Batch Request + var createBatchEventResponse = await client.Api.CreateEventsBatchAsync(batchEvents); + var batchEventResponseConfigETag = createBatchEventResponse.ToDictionary(k => k.Key.ToLower(), k => k.Value)["x-moesif-config-etag"]; + + if (!(string.IsNullOrEmpty(batchEventResponseConfigETag)) && + !(string.IsNullOrEmpty(configETag)) && + configETag != batchEventResponseConfigETag && + DateTime.UtcNow > lastUpdatedTime.AddMinutes(5)) { - Api.Http.Response.HttpStringResponse config; - // Get Application config - config = await appConfig.getConfig(client, debug); - if (!string.IsNullOrEmpty(config.ToString())) + try { - (configETag, samplingPercentage, lastUpdatedTime) = appConfig.parseConfiguration(config, debug); - return (config, configETag, samplingPercentage, lastUpdatedTime); + Api.Http.Response.HttpStringResponse config; + // Get Application config + config = await appConfig.getConfig(client, debug); + if (!string.IsNullOrEmpty(config.ToString())) + { + (configETag, samplingPercentage, lastUpdatedTime) = appConfig.parseConfiguration(config, debug); + return (config, configETag, samplingPercentage, lastUpdatedTime); + } } - } - catch (Exception ex) - { - if (debug) + catch (Exception ex) { - Console.WriteLine("Error while updating the application configuration"); + if (debug) + { + Console.WriteLine("Error while updating the application configuration"); + } } } + if (debug) + { + Console.WriteLine("Events sent successfully to Moesif"); + } } - if (debug) + catch (Exception ex) { - Console.WriteLine("Events sent successfully to Moesif"); + if (debug) + { + Console.WriteLine("Could not connect to Moesif server."); + } + return (defaultConfig, configETag, samplingPercentage, lastUpdatedTime); } } else diff --git a/Moesif.Middleware/Moesif.Middleware.nuspec b/Moesif.Middleware/Moesif.Middleware.nuspec index f85445f..0f1c9e2 100644 --- a/Moesif.Middleware/Moesif.Middleware.nuspec +++ b/Moesif.Middleware/Moesif.Middleware.nuspec @@ -2,7 +2,7 @@ Moesif.Middleware - 1.3.4 + 1.3.5 MoesifMiddleware Moesif Moesif diff --git a/Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs b/Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs index bf193f9..84c2c4b 100644 --- a/Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs +++ b/Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs @@ -46,6 +46,8 @@ public class MoesifMiddlewareNetFramework : OwinMiddleware public int batchSize; // Queue batch size + public int queueSize; // Event Queue size + public int batchMaxTime; // Time in seconds for next batch public Queue MoesifQueue; // Moesif Queue @@ -70,6 +72,7 @@ public MoesifMiddlewareNetFramework(OwinMiddleware next, Dictionary + new Thread(async () => // Create a new thread to read the queue and send event to moesif + { + + Tasks task = new Tasks(); + while (true) + { + Thread.Sleep(2000); + try { lastWorkerRun = DateTime.UtcNow; var updatedConfig = await task.AsyncClientCreateEvent(client, MoesifQueue, batchSize, debug, config, configETag, samplingPercentage, lastUpdatedTime, appConfig); (config, configETag, samplingPercentage, lastUpdatedTime) = (updatedConfig.Item1, updatedConfig.Item2, updatedConfig.Item3, updatedConfig.Item4); - }, null, startTimeSpan, periodTimeSpan); - } - catch (Exception ex) - { - LoggerHelper.LogDebugMessage(debug, "Error while scheduling events batch job"); - } + } + catch (Exception ex) + { + LoggerHelper.LogDebugMessage(debug, "Error while scheduling events batch job"); + } + } + }).Start(); } // Function to update user @@ -354,11 +359,19 @@ private async Task LogEventAsync(EventRequestModel event_request, EventResponseM { LoggerHelper.LogDebugMessage(debug, "Add Event to the batch"); // Add event to queue - MoesifQueue.Enqueue(eventModel); - if (eventsWorker == null || (lastWorkerRun.AddMinutes(1) < DateTime.UtcNow)) + if (MoesifQueue.Count < queueSize) { - LoggerHelper.LogDebugMessage(debug, "Scheduling worker thread. lastWorkerRun=" + lastWorkerRun.ToString()); - ScheduleWorker(); + MoesifQueue.Enqueue(eventModel); + if (DateTime.Compare(lastWorkerRun, DateTime.MinValue) != 0 ) + { + if (lastWorkerRun.AddMinutes(1) < DateTime.UtcNow) { + LoggerHelper.LogDebugMessage(debug, "Scheduling worker thread. lastWorkerRun=" + lastWorkerRun.ToString()); + ScheduleWorker(); + } + } + } + else { + LoggerHelper.LogDebugMessage(debug, "Queue is full, skip adding events "); } } else { var createEventResponse = await client.Api.CreateEventAsync(eventModel); diff --git a/Moesif.Middleware/Properties/AssemblyInfo.cs b/Moesif.Middleware/Properties/AssemblyInfo.cs index 56517f8..7d175b0 100644 --- a/Moesif.Middleware/Properties/AssemblyInfo.cs +++ b/Moesif.Middleware/Properties/AssemblyInfo.cs @@ -23,5 +23,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.3.4")] -[assembly: AssemblyFileVersion("1.3.4")] +[assembly: AssemblyVersion("1.3.5")] +[assembly: AssemblyFileVersion("1.3.5")]