Skip to content

Commit

Permalink
Merge pull request #390 from dabutvin/img-timeout
Browse files Browse the repository at this point in the history
add timeout to each image process
  • Loading branch information
dabutvin authored May 20, 2019
2 parents e8349d2 + ec5d086 commit 943b409
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions CompressImagesFunction/CompressImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Common;
using Common.Messages;
Expand Down Expand Up @@ -157,18 +158,32 @@ private static CompressionResult[] OptimizeImages(Repository repo, string localP
{
try
{
Console.WriteLine(image);
FileInfo file = new FileInfo(image);
double before = file.Length;
if (aggressiveCompression ? imageOptimizer.Compress(file) : imageOptimizer.LosslessCompress(file))
{
optimizedImages.Add(new CompressionResult
var tokenSource = new CancellationTokenSource();
var task = Task.Factory.StartNew(
() =>
{
Title = image.Substring(localPath.Length),
OriginalPath = image,
SizeBefore = before / 1024d,
SizeAfter = file.Length / 1024d,
});
Console.WriteLine(image);
FileInfo file = new FileInfo(image);
double before = file.Length;
if (aggressiveCompression ? imageOptimizer.Compress(file) : imageOptimizer.LosslessCompress(file))
{
optimizedImages.Add(new CompressionResult
{
Title = image.Substring(localPath.Length),
OriginalPath = image,
SizeBefore = before / 1024d,
SizeAfter = file.Length / 1024d,
});
}
},
tokenSource.Token);

// returns true if the Task completed execution within the allotted time; otherwise, false.
// Cancel and continue with the rest
if (task.Wait(10 * 1000) == false)
{
logger.LogInformation("Timeout processing {Image}", image);
tokenSource.Cancel();
}
}
catch (Exception ex)
Expand Down

0 comments on commit 943b409

Please sign in to comment.