Handle all exceptions inside TaskExecutor.cs.
This commit is contained in:
parent
8a9217aede
commit
134f833216
2 changed files with 21 additions and 11 deletions
|
@ -9,7 +9,7 @@ namespace ConsoleInterface
|
||||||
{
|
{
|
||||||
var loggerFactory = LoggerFactory.Create(b => b.AddConsole());
|
var loggerFactory = LoggerFactory.Create(b => b.AddConsole());
|
||||||
var outputFormatter = OriginalFilenameOutputFormatter.Create(@"C:\Users\nutiu\Downloads\Photos-001\clean");
|
var outputFormatter = OriginalFilenameOutputFormatter.Create(@"C:\Users\nutiu\Downloads\Photos-001\clean");
|
||||||
var executor = TasksExecutor.Create();
|
var executor = TaskExecutor.Create();
|
||||||
executor.Logger = loggerFactory.CreateLogger("Executor");
|
executor.Logger = loggerFactory.CreateLogger("Executor");
|
||||||
executor.ParallelCleanImages(new[]
|
executor.ParallelCleanImages(new[]
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
@ -6,28 +7,37 @@ using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
|
||||||
namespace Image
|
namespace Image
|
||||||
{
|
{
|
||||||
public class TasksExecutor
|
public class TaskExecutor
|
||||||
{
|
{
|
||||||
public ILogger Logger = NullLogger.Instance;
|
public ILogger Logger = NullLogger.Instance;
|
||||||
|
|
||||||
public static TasksExecutor Create()
|
public static TaskExecutor Create()
|
||||||
{
|
{
|
||||||
return new TasksExecutor();
|
return new TaskExecutor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CleanImage(string fileName, string newFilename)
|
public bool CleanImage(string fileName, string newFilename)
|
||||||
{
|
{
|
||||||
ICleaner cleaner = new Cleaner(fileName);
|
try
|
||||||
cleaner.CleanImage(newFilename);
|
{
|
||||||
|
ICleaner cleaner = new Cleaner(fileName);
|
||||||
|
cleaner.CleanImage(newFilename);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Logger.LogError(e.ToString());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ParallelCleanImages(IEnumerable<string> fileNames, IOutputFormatter outputFormatter)
|
public void ParallelCleanImages(IEnumerable<string> fileNames, IOutputFormatter outputFormatter)
|
||||||
{
|
{
|
||||||
Logger.LogInformation("Starting parallel image cleaning.");
|
Logger.LogInformation("Starting parallel image cleaning.");
|
||||||
var tasks = new List<Task>();
|
var tasks = new List<Task<bool>>();
|
||||||
foreach (var fileName in fileNames)
|
foreach (var fileName in fileNames)
|
||||||
{
|
{
|
||||||
var task = new Task(() => { CleanImage(fileName, outputFormatter.FormatOutputPath(fileName)); });
|
var task = new Task<bool>(() => CleanImage(fileName, outputFormatter.FormatOutputPath(fileName)));
|
||||||
tasks.Add(task);
|
tasks.Add(task);
|
||||||
task.Start();
|
task.Start();
|
||||||
}
|
}
|
||||||
|
@ -35,7 +45,7 @@ namespace Image
|
||||||
var result = Task.WhenAll(tasks);
|
var result = Task.WhenAll(tasks);
|
||||||
result.Wait();
|
result.Wait();
|
||||||
|
|
||||||
var successTasks = tasks.Count(t => t.IsCompletedSuccessfully);
|
var successTasks = tasks.Count(t => t.IsCompletedSuccessfully && t.Result);
|
||||||
var errorTasks = tasks.Count() - successTasks;
|
var errorTasks = tasks.Count() - successTasks;
|
||||||
Logger.LogInformation($"All tasks completed. Success: {successTasks}, Errors: {errorTasks}");
|
Logger.LogInformation($"All tasks completed. Success: {successTasks}, Errors: {errorTasks}");
|
||||||
}
|
}
|
Loading…
Reference in a new issue