I've have a recursive descent function that takes all of the files in the parent directory and all of the files from any number of child directories to send to AWS S3. I have a 5 min timeout, based from this post, set to let all of the files in the folder get pushed to S3 and if it takes longer than that I want to cancel any of the remaining tasks. While I'm setting the cancel flag for the token regardless of if the Delay
or Wait
of the WhenAny
hit the timeout or not I want to be able take all of the tasks that didn't complete from the list and pull the details of the request for logging. Microsoft says the Id and CurrentId of the task can't be considered unique.
How can I get the request object that created the task from the task object?
private static void ProcessDirectory(System.IO.DirectoryInfo di)
{
int _timeOut = 5 * 60 * 1000;
foreach (var item in di.GetDirectories())
{
ProcessDirectory(item);
}
using (Amazon.S3.AmazonS3Client _client = new Amazon.S3.AmazonS3Client())
{
System.Threading.CancellationTokenSource _cancellationTokenSource = new System.Threading.CancellationTokenSource();
System.Collections.Generic.List<System.Threading.Tasks.Task<Amazon.S3.Model.PutObjectResponse>> _responses = new List<System.Threading.Tasks.Task<Amazon.S3.Model.PutObjectResponse>>(1000);
foreach (var item in di.GetFiles())
{
_responses.Add(_client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
{
BucketName = SiteSettings.Bucket,
CannedACL = Amazon.S3.S3CannedACL.PublicRead,
FilePath = item.FullName,
Key = item.FullName.Replace(SiteSettings.OutputRoot, string.Empty).Replace(@"\", "/")
}, _cancellationTokenSource.Token));
}
// Wait 5 Mins + 1 sec
System.Threading.Tasks.Task.WhenAny(System.Threading.Tasks.Task<Amazon.S3.Model.PutObjectResponse>.WhenAll(_responses)
, System.Threading.Tasks.Task.Delay(_timeOut)).Wait(_timeOut + 1000);
_cancellationTokenSource.Cancel(); //Cancel the remaining pushes for this folder.
foreach (var item in _responses)
{
if (!item.IsCompleted)
{
//Pull the key value to log
}
}
}
}