从列表中查找不完整任务的详细信息

问题描述 投票:0回答:1

我有一个递归下降函数,它将父目录中的所有文件和来自任意数量子目录的所有文件发送到AWS S3。我有一个5分钟的超时,based from this post,设置为让文件夹中的所有文件都被推送到S3,如果它需要更长的时间,我想取消任何剩余的任务。当我为令牌设置取消标志时,无论DelayWaitWhenAny是否达到超时,我都希望能够从列表中完成所有未完成的任务并拉出详细信息。请求记录。微软称IdCurrentId的任务不能被认为是独一无二的。

如何从任务对象获取创建任务的请求对象?

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
            }
        }
    }
}
c# async-await
1个回答
1
投票

您可以在创建每个工作项后为其保存一些唯一键,然后使用该键进行记录。在这个例子中,我使用item.FullName作为关键。此外,我冒昧地在类型之前删除长名称空间以获得更好的可读性,希望你不介意:

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())
    {
        CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
        Dictionary<string, Task<Amazon.S3.Model.PutObjectResponse>> _responses =
            new Dictionary<string, Task<Amazon.S3.Model.PutObjectResponse>>(1000);

        foreach (var item in di.GetFiles())
        {
            // use any unique information about your item here
            var itemName = item.FullName;
            _responses[itemName] = _client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
            {
                BucketName = SiteSettings.Bucket,
                CannedACL = Amazon.S3.S3CannedACL.PublicRead,
                FilePath = itemName,
                Key = item.FullName.Replace(SiteSettings.OutputRoot, string.Empty).Replace(@"\", "/")
            }, _cancellationTokenSource.Token);
        }
        // Wait 5 Mins + 1 sec
        Task.WhenAny(Task<Amazon.S3.Model.PutObjectResponse>.WhenAll(_responses.Values)
            ,Task.Delay(_timeOut)).Wait(_timeOut + 1000);

        _cancellationTokenSource.Cancel(); //Cancel the remaining pushes for this folder.
        foreach (var item in _responses)
        {
            if (!item.Value.IsCompleted)
            {
                //Pull the key value to log
                var keyValue = item.Key;
            }
        }
    }
}

你看,我用List<Task<Amazon.S3.Model.PutObjectResponse>>交换了Dictionary<string, Task<Amazon.S3.Model.PutObjectResponse>>,其中key是文件的全名。因此,如果字典中的某些任务在5分钟内没有完成,您将能够获得未加载的文件的名称。

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.