【问题标题】:What could be the reason behind the failure of the task?任务失败的原因可能是什么?
【发布时间】:2018-05-04 03:24:52
【问题描述】:

我有一个名为TableStorageController.cs的班级

public class TableStorageController
{
    private static Dictionary<string, BlockingCollection<CloudModelDetail>> s_dictionary = new Dictionary<string, BlockingCollection<CloudModelDetail>>();
    private static StorageAccount s_azureStorageAccount;
    private readonly CloudModelDetail _cloudModelDetail;
    private static CancellationTokenSource s_cancellationTokenSource = new CancellationTokenSource();
    private static int s_retailerId;

    /// <summary>
    /// Static task to log transactions to Azure Table Storage after every 5 minutes.
    /// </summary>
    static TableStorageController()
    {
        try
        {
            Task.Run(async () =>
            {
                while (true)
                {
                    await Task.Delay(300000, s_cancellationTokenSource.Token);
                    foreach (var retaileridkey in s_dictionary.Keys)
                    {
                        var batchOperation = new TableBatchOperation();
                        while (s_dictionary[retaileridkey].Count != 0 && batchOperation.Count < 101)
                        {
                            batchOperation.InsertOrMerge(s_dictionary[retaileridkey].Take());
                        }
                        if (batchOperation?.Count != 0)
                            await s_azureStorageAccount.VerifyCloudTable.ExecuteBatchAsync(batchOperation);
                    }
                }
            });
        }
        catch (Exception ex)
        {
            s_log.Fatal("Azure task run failed", ex);
        }
    }
}

此任务每 5 分钟运行一次,并将字典中存在的任何项目记录到 Azure 表存储。 在本地运行我的代码时,我可以看到它每 5 分钟触发一次。 但是,不知何故,一旦在另一个环境(生产)中部署后,它就会失败。

谁能指出我错过了什么?

注意:我从来没有遇到过异常Azure task run failed

【问题讨论】:

    标签: c# async-await task azure-table-storage


    【解决方案1】:

    尝试将异常处理放在Task.Run 块中。您无需等待对 Task.Run 的调用,因此不会注意到异常。而且由于await Task.Delay 已经是非阻塞的,我不明白你为什么需要额外的任务。试试这个:

    static async void TableStorageController()
    {
        try
        {
            while (true)
            {
                await Task.Delay(TimeSpan.FromMinutes(5), s_cancellationTokenSource.Token);
                foreach (var retaileridkey in s_dictionary.Keys)
                {
                    var batchOperation = new TableBatchOperation();
                    while (s_dictionary[retaileridkey].Count != 0 && batchOperation.Count < 101)
                    {
                        batchOperation.InsertOrMerge(s_dictionary[retaileridkey].Take());
                    }
                    if (batchOperation?.Count != 0)
                        await s_azureStorageAccount.VerifyCloudTable.ExecuteBatchAsync(batchOperation);
                }
            }
        }
        catch (Exception ex)
        {
            s_log.Fatal("Azure task run failed", ex);
        }
    }
    

    顺便说一句,您还可以使用计时器而不是带有 CancellationToken 的 Task.Delay

    【讨论】:

      猜你喜欢
      • 2012-02-24
      • 2021-03-20
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多