Skip to content

Commit

Permalink
More Multi-threading, logging and finished the bullets
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonard Sperry committed Dec 20, 2020
1 parent edcfb70 commit b893797
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CsvProcessor/CsvProcessor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
</ItemGroup>

</Project>
80 changes: 47 additions & 33 deletions CsvProcessor/MultiThread/FileProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,52 @@ class FileProcessor
{
IWidgetLogic _widgetLogic;
IWidgetProvider _widgetProvider;
IWidgetQueueProvider _widgetQueueProvider;

public FileProcessor(IWidgetLogic logic, IWidgetProvider provider)
{
_widgetLogic = logic;
_widgetProvider = provider;
}

internal Task ProcessFile(string filePath)
{
var rdr = new CsvReader<Task>(filePath, line =>
{
var widget = new Widget(line);
return Task.Run(() => _widgetLogic.ProcessWidget(widget))
.ContinueWith(t =>
internal Task ProcessFile(string filePath)
{
if (t.Exception != null)
var rdr = new CsvReader<Task>(filePath, line =>
{
throw t.Exception;
}
return _widgetProvider.Save(widget);
});
});

return Task.WhenAll(rdr.Read());
}
var widget = new Widget(line);
return Task.Run(() => _widgetLogic.ProcessWidget(widget))
.ContinueWith(t =>
{
if (t.Exception != null)
{
throw t.Exception;
}
return _widgetProvider.Save(widget);
});
});

internal Task ProcessFileWithSemaphore(string filePath)
{
SemaphoreSlim slim = new SemaphoreSlim(10, 10);
var rdr = new CsvReader<Task>(filePath, line => {
slim.Wait();
try
{
var widget = new Widget(line);
_widgetLogic.ProcessWidget(widget);
return _widgetProvider.Save(widget);
return Task.WhenAll(rdr.Read());
}
finally

internal Task ProcessFileWithSemaphore(string filePath)
{
slim.Release();
}
});
SemaphoreSlim slim = new SemaphoreSlim(10, 10);
var rdr = new CsvReader<Task>(filePath, line => {
slim.Wait();
try
{
var widget = new Widget(line);
_widgetLogic.ProcessWidget(widget);
return _widgetProvider.Save(widget);
}
finally
{
slim.Release();
}
});

return Task.WhenAll(rdr.Read());
}
return Task.WhenAll(rdr.Read());
}

internal Task ProcessFileWithSemaphoreFixed(string filePath)
{
Expand Down Expand Up @@ -95,8 +96,16 @@ select Task.Run(() =>
});
return Task.WhenAll(batchTasks);
}
}


internal Task ProccessWithMessageQueue(string filePath)
{
var rdr = new CsvReader<Task>(filePath, line =>
_widgetQueueProvider.Enqueue(new Widget(line))
);
return Task.WhenAll(rdr.Read());
}
}
class Widget
{
public Widget(string[] fields)
Expand Down Expand Up @@ -138,4 +147,9 @@ public Task Save(IEnumerable<Widget> widgets)
throw new NotImplementedException();
}
}

interface IWidgetQueueProvider
{
Task Enqueue(Widget widget);
}
}
36 changes: 33 additions & 3 deletions Samples/Misc.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
using System;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Samples
{
class Misc
{

ILogger _logger = null;
void Asdf()
{
List<int> ints = new List<int>();
using (_logger.BeginScope("add context here"))
{
CallAnotherMethod();
}
}

private void CallAnotherMethod()
{
throw new NotImplementedException();
}

public void TopLevelMethod()
{
try
{
using (_logger.BeginScope("add context here"))
{
CallAnotherMethod();
}
}
catch (Exception ex) when (HandleException(ex, true))
{
}
}

public bool HandleException(Exception ex, bool propogate = false)
{
_logger.LogError(ex, "unhandled error");
return propogate;
}
}
}
1 change: 1 addition & 0 deletions Samples/Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Moq" Version="4.15.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down

0 comments on commit b893797

Please sign in to comment.