/// <summary> /// /// </summary> /// <param name="cancellationToken"></param> /// <param name="mainFuncAsync">the main function</param> /// <param name="cancelFuncAsync">task cancel function</param> /// <param name="otherExceptionHandlerAsync"> other type of exception handler </param> /// <returns></returns> public async Task ProcessAsync(CancellationToken cancellationToken, Func <Task> mainFuncAsync, Func <Exception, Task> cancelFuncAsync = null, Func <CancellationToken, Exception, Task> otherExceptionHandlerAsync = null) { try { await mainFuncAsync(); } catch (OperationCanceledException e) { _serviceEventSource?.LoggingServiceMessage(_serviceContext, "ProcessAsync is canceled, the type:{0}, Exception: {1}", e.GetType().Name, e.ToString()); if (cancellationToken.IsCancellationRequested && cancelFuncAsync != null) { await cancelFuncAsync(e); } throw; } catch (TimeoutException te) { // transient error. Retry. _serviceEventSource?.LoggingServiceMessage(_serviceContext, "ProcessAsync met TimeoutException, the type:{0}, Exception: {1}", te.GetType().Name, te.ToString()); } catch (FabricTransientException fte) { // transient error. Retry. _serviceEventSource?.LoggingServiceMessage(_serviceContext, "ProcessAsync met FabricTransientException: {0}", fte.Message); } catch (FabricNotPrimaryException fnpe) { // not primary any more, time to quit. _serviceEventSource?.LoggingServiceMessage(_serviceContext, "ProcessAsync met FabricNotPrimaryException: {0}", fnpe.Message); if (cancelFuncAsync != null) { await cancelFuncAsync(fnpe); } throw; } catch (Exception e) { _serviceEventSource?.LoggingServiceMessage(_serviceContext, "ProcessAsync met exception, the type: {0}, exception= {1} ", e.GetType().Name, e.ToString()); if (otherExceptionHandlerAsync != null) { await otherExceptionHandlerAsync(cancellationToken, e); } else { await DefaultHandleOtherExceptionAsync(cancellationToken, e); } } }
//public async Task ProcessEndLessTask(CancellationToken cancellationToken, // Func<CancellationToken, Task> funcAsync) //{ // await Task.Run(() => ProcessEndLessAsync(cancellationToken, funcAsync), cancellationToken); //} public async Task RunEndLessTask(CancellationToken cancellationToken, Func <CancellationToken, Task> funcAsync) { long retryNum = 0; while (true) { _serviceEventSource?.LoggingServiceMessage(_serviceContext, "RunEndLessTask is starting funcAsync {0} times at {1} .", ++retryNum, DateTimeOffset.UtcNow.ToString()); try { cancellationToken.ThrowIfCancellationRequested(); await funcAsync(cancellationToken); } catch (OperationCanceledException e) { _serviceEventSource?.LoggingServiceMessage(_serviceContext, "funcAsync met exception, the type: {0}, exception= {1} ", e.GetType().Name, e.ToString()); if (cancellationToken.IsCancellationRequested) { throw; } } catch (FabricNotPrimaryException e) { _serviceEventSource?.LoggingServiceMessage(_serviceContext, "funcAsync met exception, the type: {0}, exception= {1} ", e.GetType().Name, e.ToString()); throw; } catch (Exception e) { _serviceEventSource?.LoggingServiceMessage(_serviceContext, "funcAsync met exception, the type: {0}, exception= {1} ", e.GetType().Name, e.ToString()); } } }