在TPL数据流中使用BroadcastBlock复制异常

时间:2014-02-03 17:47:35

标签: c# exception tpl-dataflow

我正在尝试使用TPL Dataflow来创建管道。到目前为止一切正常,我的管道定义如下(虽然我的问题只是广播员,submissionSucceeded,submissionFailed):

// Define tasks
var productListingBatchBuffer = new BufferBlock<PostSubmissionState>();
var splitFile = new TransformBlock<PostSubmissionState, PostSubmissionState>(s => SplitFile(s));
var saveFile = new TransformBlock<PostSubmissionState, PostSubmissionState>(s => SaveFile(s));
var postSubmission = new TransformBlock<PostSubmissionState, PostSubmissionState>(s => PostSubmission(s));
var broadcaster = new BroadcastBlock<PostSubmissionState>(state => state);
var submissionSucceeded = new ActionBlock<PostSubmissionState>(s => SubmissionSucceeded(s));
var submissionFailed = new ActionBlock<PostSubmissionState>(s => SubmissionFailed(s));

// Link em up
productListingBatchBuffer.LinkTo(splitFile, new DataflowLinkOptions() { PropagateCompletion = true });
splitFile.LinkTo(saveFile, new DataflowLinkOptions() { PropagateCompletion = true });
saveFile.LinkTo(postSubmission, new DataflowLinkOptions() { PropagateCompletion = true });
postSubmission.LinkTo(broadcaster, new DataflowLinkOptions() { PropagateCompletion = true });
broadcaster.LinkTo(submissionSucceeded, new DataflowLinkOptions() { PropagateCompletion = true }, state => state.PostSucceeded);
broadcaster.LinkTo(submissionFailed, new DataflowLinkOptions() { PropagateCompletion = true }, state => !state.PostSucceeded);

我遇到的问题是Exceptions的传播。因为我的BroadcastBlock将其完成(因此任何Fault)传播到两个块,如果发生异常,它会传播到两个块。因此,当我做

Task.WaitAll(submissionSucceeded.Completion, submissionFailed.Completion);

我最终得到一个包含两个例外的聚合异常。现在我能做的最好的就是过滤这些,即:

try
{
    Task.WaitAll(submissionSucceeded.Completion, submissionFailed.Completion);
}
catch (AggregateException ex)
{
    var uniqueExceptions = new AggregateException(ex.Flatten().InnerExceptions.Distinct());
    Console.WriteLine("An exception was thrown.\n{0}", uniqueExceptions.Flatten());
}

但我想知道是否有更好的方法来做到这一点。即如果只发生一个例外,我只想提出一个例外。我是Dataflow的新手,所以只是发现了所有的约定。

1 个答案:

答案 0 :(得分:1)

我编写了一个TPL DataFlow示例(https://github.com/squideyes/PodFetch),它采用了稍微不同的方法来完成和处理错误。这是Program.cs的第171行到第201行的相关代码:

    scraper.LinkTo(fetcher, link => link != null);
    scraper.LinkTo(DataflowBlock.NullTarget<Link>());

    scraper.HandleCompletion(fetcher);

    Status.Info.Log("Fetching APOD's archive list");

    links.ForEach(link => scraper.Post(link));

    scraper.Complete();

    try
    {
        await fetcher.Completion;

        Status.Finished.Log("Fetched: {0:N0}, Skipped: {1:N0}, Errors: {2:N0}, Seconds: {3:N2}",
            fetched, skipped, errored, (DateTime.UtcNow - startedOn).TotalMilliseconds / 1000.0);
    }
    catch (AggregateException errors)
    {
        foreach (var error in errors.InnerExceptions)
            Status.Failure.Log(error.Message);
    }
    catch (TaskCanceledException)
    {
        Status.Cancelled.Log("The process was manually cancelled!");
    }
    catch (Exception error)
    {
        Status.Failure.Log(error.Message);
    }

正如您所看到的,我将几个TPL块链接在一起,然后使用HandleCompletion扩展方法为处理完成做好准备:

    public static void HandleCompletion(
        this IDataflowBlock source, params IDataflowBlock[] targets)
    {
        source.Completion.ContinueWith(
            task =>
            {
                foreach (var target in targets)
                {
                    if (task.IsFaulted)
                        target.Fault(task.Exception);
                    else
                        target.Complete();
                }
            });
    }

非常重要的是,当我将对象传递给链中的第一个块时,我调用了scraper.Complete()。有了它,HandleCompletion扩展方法然后处理延续。而且,由于我正在等待fetcher(链中的最后一个块完成),因此很容易在try / catch中捕获任何结果错误。