Skip to content

Commit

Permalink
Merge pull request #39 from Moesif/refactor-schedule-worker
Browse files Browse the repository at this point in the history
Refactor: Schedule Worker for NetFramework
  • Loading branch information
dkm199 authored Feb 4, 2021
2 parents 40c9fba + 16bff2c commit 7b075ec
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 43 deletions.
55 changes: 33 additions & 22 deletions Moesif.Middleware/Helpers/Tasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,48 @@ public List<EventModel> QueueGetAll(Queue<EventModel> 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
Expand Down
2 changes: 1 addition & 1 deletion Moesif.Middleware/Moesif.Middleware.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Moesif.Middleware</id>
<version>1.3.4</version>
<version>1.3.5</version>
<title>MoesifMiddleware</title>
<authors>Moesif</authors>
<owners>Moesif</owners>
Expand Down
49 changes: 31 additions & 18 deletions Moesif.Middleware/NetFramework/MoesifMiddlewareNetFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventModel> MoesifQueue; // Moesif Queue
Expand All @@ -70,6 +72,7 @@ public MoesifMiddlewareNetFramework(OwinMiddleware next, Dictionary<string, obje
logBody = LoggerHelper.GetConfigBoolValues(moesifOptions, "LogBody", true);
isBatchingEnabled = LoggerHelper.GetConfigBoolValues(moesifOptions, "EnableBatching", true); // Enable batching
batchSize = LoggerHelper.GetConfigIntValues(moesifOptions, "BatchSize", 25); // Batch Size
queueSize = LoggerHelper.GetConfigIntValues(moesifOptions, "QueueSize", 1000); // Event Queue Size
batchMaxTime = LoggerHelper.GetConfigIntValues(moesifOptions, "batchMaxTime", 2); // Batch max time in seconds
appConfig = new AppConfig(); // Create a new instance of AppConfig
userHelper = new UserHelper(); // Create a new instance of userHelper
Expand Down Expand Up @@ -98,25 +101,27 @@ public MoesifMiddlewareNetFramework(OwinMiddleware next, Dictionary<string, obje
}
}

private void ScheduleWorker()
private void ScheduleWorker()
{
try
{
var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromSeconds(batchMaxTime);
Tasks task = new Tasks();

eventsWorker = new Timer(async (e) =>
new Thread(async () => // Create a new thread to read the queue and send event to moesif
{

Tasks task = new Tasks();
while (true)
{
Thread.Sleep(batchMaxTime * 1000);
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
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions Moesif.Middleware/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]

0 comments on commit 7b075ec

Please sign in to comment.