运行 两个 Web 作业同时进行,所以如果一个失败,另一个将处理文件

Running two Web jobs simultaneously, So if one fails other will process the files

我想在 blob 存储中上传 CSV 文件时对其进行处理。对于这个要求,我正在编写带有 blob 触发器的 Web 作业。

为了确保连续的 CSV 处理,我正在编写一个带有 blob 触发器的网络作业。

因此,如果一个网络作业失败,另一个网络作业将处理 csv。

现在,我的问题是当两个 Web 作业 运行 它们正在处理相同的 CSV 文件并最终创建重复数据时。

如何锁定文件以便只有一个 Web 作业可以处理 CSV 文件?

如果第一个 Web 作业要关闭,我如何触发第二个 Web 作业?

How can I trigger second web job if first web job is going to shut down?

我建议您在第一个 WebJob 中使用 try-catch 来处理异常。如果发生任何异常,我们可以将 blob 名称写入队列以触发另一个 WebJob。

public static void ProcessCSVFile([BlobTrigger("input/{blobname}.csv")] TextReader input, [Queue("myqueue")] out string outputBlobName, string blobname)
{
    try
    {
        //process the csv file

        //if none exception occurs, set the value of outputBlobName to null
        outputBlobName = null;
    }
    catch
    {
        //add the blob name to a queue and another function named RepeatProcessCSVFile will be triggered.
        outputBlobName = blobname;
    }
}

我们可以在另一个 WebJob 中创建一个 QueueTrigger 函数。在这个函数中,我们可以读出 blob 名称并重新处理 csv。如果出现新的异常,我们也可以重新将 blob 名称添加到队列中,此函数将一次又一次地执行,直到成功处理 csv 文件。

public static void RepeatProcessCSVFile([QueueTrigger("myqueue")] string blobName, [Queue("myqueue")] out string outputBlobName)
{
    try
    {
        //process the csv file

        //if none exception occurs, set the value of outputBlobName to null.
        outputBlobName = null;
    }
    catch
    {
        //re-add the blobName to the queue and this function will be executed again until the csv file has been handled successfully.
        outputBlobName = blobName;
    }
}

我喜欢 Amor 的解决方案,但有一些建议要添加到其中。

如果您放弃 BlobTrigger 方法,而是将指示需要处理的 blob 的服务总线队列消息加入队列,则可以使用 ServiceBusTrigger 触发您的处理。在发生异常的情况下,放弃该消息,它将可用于另一次处理尝试。这将使您只有一个 webjob,并且仍然有冗余。

使用服务总线队列的另一个优点是,您可以保证至少处理一次,最多处理一次,并在读取消息时保证消息锁定。标准存储队列不是这种情况。如果您想添加第二个 Webjob 实例来监视相同的服务总线队列,这也会在将来为您提供可扩展性选项。