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 outputFormatter = OriginalFilenameOutputFormatter.Create(@"C:\Users\nutiu\Downloads\Photos-001\clean");
|
||||
var executor = TasksExecutor.Create();
|
||||
var executor = TaskExecutor.Create();
|
||||
executor.Logger = loggerFactory.CreateLogger("Executor");
|
||||
executor.ParallelCleanImages(new[]
|
||||
{
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
@ -6,28 +7,37 @@ using Microsoft.Extensions.Logging.Abstractions;
|
|||
|
||||
namespace Image
|
||||
{
|
||||
public class TasksExecutor
|
||||
public class TaskExecutor
|
||||
{
|
||||
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)
|
||||
{
|
||||
try
|
||||
{
|
||||
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)
|
||||
{
|
||||
Logger.LogInformation("Starting parallel image cleaning.");
|
||||
var tasks = new List<Task>();
|
||||
var tasks = new List<Task<bool>>();
|
||||
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);
|
||||
task.Start();
|
||||
}
|
||||
|
@ -35,7 +45,7 @@ namespace Image
|
|||
var result = Task.WhenAll(tasks);
|
||||
result.Wait();
|
||||
|
||||
var successTasks = tasks.Count(t => t.IsCompletedSuccessfully);
|
||||
var successTasks = tasks.Count(t => t.IsCompletedSuccessfully && t.Result);
|
||||
var errorTasks = tasks.Count() - successTasks;
|
||||
Logger.LogInformation($"All tasks completed. Success: {successTasks}, Errors: {errorTasks}");
|
||||
}
|
Loading…
Reference in a new issue