麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

分享一個異步任務(wù)在遇到IO異常時支持遞歸回調(diào)的輔助方法

2019-11-17 02:33:19
字體:
供稿:網(wǎng)友

分享一個異步任務(wù)在遇到IO異常時支持遞歸回調(diào)的輔助方法

Posted on 2015-02-26 15:25 netfocus 閱讀(...) 評論(...) 編輯 收藏
public void TryAsyncActionRecursively<TAsyncResult>(    string asyncActionName,    Func<Task<TAsyncResult>> asyncAction,    Action<int> mainAction,    Action<TAsyncResult> successAction,    Func<string> getContextInfoFunc,    Action<Exception> failedAction,    int retryTimes) where TAsyncResult : AsyncOperationResult{    var retryAction = new Action<int>(currentRetryTimes =>    {        if (currentRetryTimes >= _immediatelyRetryTimes)        {            Task.Factory.StartDelayedTask(_retryIntervalForIOException, () => mainAction(currentRetryTimes + 1));        }        else        {            mainAction(currentRetryTimes + 1);        }    });    var executeFailedAction = new Action<Exception>(ex =>    {        try        {            if (failedAction != null)            {                failedAction(ex);            }        }        catch (Exception unknownEx)        {            _logger.Error(string.Format("Failed to execute the failedCallbackAction of asyncAction:{0}, contextInfo:{1}",                asyncActionName, getContextInfoFunc()), unknownEx);        }    });    var PRocessTaskException = new Action<Exception, int>((ex, currentRetryTimes) =>    {        if (ex is IOException)        {            _logger.Error(string.Format("Async task '{0}' has io exception, contextInfo:{1}, current retryTimes:{2}",                asyncActionName, getContextInfoFunc(), currentRetryTimes), ex);            retryAction(retryTimes);        }        else        {            _logger.Error(string.Format("Async task '{0}' has unknown exception, contextInfo:{1}, current retryTimes:{2}",                asyncActionName, getContextInfoFunc(), currentRetryTimes), ex);            executeFailedAction(ex);        }    });    var completeAction = new Action<Task<TAsyncResult>>(t =>    {        if (t.Exception != null)        {            processTaskException(t.Exception.InnerException, retryTimes);            return;        }        if (t.IsCanceled)        {            _logger.ErrorFormat("Async task '{0}' was cancelled, contextInfo:{1}, current retryTimes:{2}",                asyncActionName, getContextInfoFunc(), retryTimes);            retryAction(retryTimes);            return;        }        var result = t.Result;        if (result.Status == AsyncOperationResultStatus.IOException)        {            _logger.ErrorFormat("Async task '{0}' has io exception, contextInfo:{1}, current retryTimes:{2}, errorMsg:{3}",                asyncActionName, getContextInfoFunc(), retryTimes, result.ErrorMessage);            retryAction(retryTimes);            return;        }        if (successAction != null)        {            successAction(result);        }    });    try    {        asyncAction().ContinueWith(completeAction);    }    catch (IOException ex)    {        _logger.Error(string.Format("Execute async action '{0}' failed, contextInfo:{1}, current retryTimes:{2}",            asyncActionName, getContextInfoFunc(), retryTimes), ex);        retryAction(retryTimes);    }    catch (Exception ex)    {        _logger.Error(string.Format("Execute async action '{0}' failed, contextInfo:{1}, current retryTimes:{2}",            asyncActionName, getContextInfoFunc(), retryTimes), ex);        executeFailedAction(ex);    }}

該函數(shù)的功能是:執(zhí)行一個異步任務(wù)(返回Task的方法),如果執(zhí)行出現(xiàn)IO異常,則重試當(dāng)前主函數(shù)(mainAction);用戶的mainAction中會再次調(diào)用TryAsyncActionRecursively方法。從而實現(xiàn)當(dāng)遇到IO異常時,能做到不斷重試。另外,重試只立即重試指定的次數(shù),超過指定次數(shù),則不立即重試,而是暫停一定間隔后再次執(zhí)行。該函數(shù)還提供當(dāng)acyncAction執(zhí)行成功或失敗后的回調(diào)函數(shù),以及允許傳入當(dāng)前上下文的一些說明信息,以便記錄有意義的錯誤日志信息。

下面是使用示例:

private void PublishEventAsync(ProcessingCommand processingCommand, EventStream eventStream, int retryTimes){    TryAsyncActionRecursively<AsyncOperationResult>("PublishEventAsync",    () => _eventPublisher.PublishAsync(eventStream),    currentRetryTimes => PublishEventAsync(processingCommand, eventStream, currentRetryTimes),    result =>    {        _logger.DebugFormat("Publish events success, {0}", eventStream);        processingCommand.Complete(new CommandResult(CommandStatus.Success, processingCommand.Command.Id));    },    () => string.Format("[eventStream:{0}]", eventStream),    ex => processingCommand.Complete(new CommandResult(CommandStatus.Failed, processingCommand.Command.Id)),    retryTimes);}
PublishEventAsync(processingCommand, eventStream, 0);


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 激情综合网俺也去 | 羞羞网站在线看 | 性欧美xxxx免费岛国不卡电影 | 国产一区二区三区高清 | 成人啪啪18免费网站 | 逼片| 特片网久久| jizzyouxxxx| 日本a v免费观看 | 一级电影免费 | 毛片观看网址 | 国产午夜精品一区二区三区免费 | 久久草在线观看视频 | 91看片王| 欧美成人精品一区二区 | 日韩高清影视 | 男女牲高爱潮免费视频男女 | 精品在线观看一区 | 久久精品视频首页 | 精品一区二区三区在线观看视频 | 精品久久久久久久久久久久久久 | 国产成人高清成人av片在线看 | 一区二区精品视频 | av电影在线观看网址 | 妇女毛片 | 国产精品剧情一区二区三区 | 88xx成人精品视频 | 日本羞羞的午夜电视剧 | 97se亚洲综合在线韩国专区福利 | 中国美女一级黄色大片 | 美女亚洲 | 91一级毛片 | 欧美一区二区精品夜夜嗨 | 国产视频在线一区 | 国产亚洲精品久久久久婷婷瑜伽 | 欧美日韩在线视频一区 | 亚洲日本韩国在线观看 | 日韩精品a在线观看 | 激情久久免费视频 | 91av资源在线| 国产91片 |