public PrimeController( IMapper mapper, IMediator mediator, ILinksService linksService, ILoggingManager loggingManager, PolicyConfiguration policyConfiguration) { _mapper = mapper; _mediator = mediator; _linksService = linksService; _loggingManager = loggingManager; _policyConfiguration = policyConfiguration; _circuitBreakerPolicy = Policy .Handle <TaskCanceledException>() .CircuitBreakerAsync(policyConfiguration.CircuitBreakerAllowedExceptions, TimeSpan.FromMinutes(_policyConfiguration.CircuitBreakerShutdownDurationInMinutes), (ex, t) => { _loggingManager.LogFatal("Circuit broken!"); }, () => { _loggingManager.LogInformation("Circuit Reset!"); }); }
public async Task <ActionResult <NumberIsPrimeDto> > CheckNumberIsPrime(CheckNumberDto checkNumberDto) { _loggingManager.LogInformation($"CheckNumberIsPrime endpoint started. number: {checkNumberDto.Number}"); try { bool isPrimeResult = false; await _circuitBreakerPolicy.ExecuteAsync(async() => { var cancelTokenSource = new CancellationTokenSource(); cancelTokenSource.CancelAfter(TimeSpan.FromSeconds(_policyConfiguration.TimoutInMinutes)); var numberIsPrimeResult = await _mediator.Send( new CheckNumberIsPrimeCommand { Number = checkNumberDto.Number }, cancelTokenSource.Token); isPrimeResult = numberIsPrimeResult.IsPrime; }); return(Ok(new NumberIsPrimeDto { IsPrime = isPrimeResult })); } catch (TaskCanceledException taskCanceledException) { _loggingManager.LogError(taskCanceledException.Message); return(Problem( title: "The computation runs too long. Please check back later or use a HATEOAS enabled endpoint.", detail: taskCanceledException.Message, statusCode: (int)HttpStatusCode.RequestTimeout)); } catch (Exception exception) { _loggingManager.LogError(exception.Message); return(Problem( title: "Unexpected error. Please contact support.", detail: exception.Message, statusCode: (int)HttpStatusCode.InternalServerError)); } }