public void Create(Card newCard) { try { _repoWrapper.Card.Create(newCard); _repoWrapper.Save(); } catch (Exception ex) { _loggingManager.LogError(ex.ToString()); } }
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)); } }
public IActionResult GetAllReadingTypes() { try { var readingTypes = _repoWrapper.ReadingType.GetAllReadingTypes(); if (readingTypes == null) { _loggingManager.LogError("There was an error retrieving ReadingType.FindAll() : null value"); return(BadRequest("There was an error retrieving Reading Types")); } var readingTypeDtos = _mapper.Map <IEnumerable <ReadingType_Read_Dto> >(readingTypes); return(Ok(readingTypeDtos)); } catch (Exception ex) { _loggingManager.LogError($"There was an error in the GetAllReadingTypes action : {ex.Message}"); return(StatusCode(500, "Internal Server Error")); } }
public async Task <CheckNumberIsPrimeCommandResult> Handle(CheckNumberIsPrimeCommand request, CancellationToken cancellationToken) { var calculation = await _calculationRepository.GetCalculationByNumberAsync(request.Number); var result = new CheckNumberIsPrimeCommandResult(); if (calculation == null || calculation.CalculationStatus == CalculationStatus.Failed) { if (calculation == null) { await _calculationRepository.StartNewCalculationAsync(request.Number); } try { var scienceRequestData = new PrimeCalculationRequest { Number = request.Number }; var scienceReply = await UseScienceInterface("NumberIsPrime", scienceRequestData.ToByteArray(), cancellationToken); var scienceResult = NumberIsPrimeResponse.Parser.ParseFrom(scienceReply.RawBytes); await _calculationRepository.UpdateCalculationAsync(new CalculationModel { Number = request.Number, CalculationStatus = CalculationStatus.Done, IsPrime = scienceResult.IsPrime }); result.IsPrime = scienceResult.IsPrime; } catch (TaskCanceledException taskCanceledException) { _loggingManager.LogFatal(taskCanceledException.Message); await _calculationRepository.UpdateCalculationAsync(new CalculationModel { Number = request.Number, CalculationStatus = CalculationStatus.Failed, }); throw; } catch (Exception exception) { _loggingManager.LogError(exception.Message); await _calculationRepository.UpdateCalculationAsync(new CalculationModel { Number = request.Number, CalculationStatus = CalculationStatus.Unknown, }); throw; } } else if (calculation.CalculationStatus == CalculationStatus.Done) { result.IsPrime = calculation.IsPrime; return(result); } else if (calculation.CalculationStatus == CalculationStatus.InProgress) { //TODO: refactor to appsettings //TODO: use polly for timeout policy TimeSpan checkTime = TimeSpan.FromMinutes(0.25); while (true) { await Task.Delay(checkTime, cancellationToken); var currentState = await _calculationRepository.GetCalculationByNumberAsync(request.Number); if (currentState.CalculationStatus == CalculationStatus.Done) { result.IsPrime = currentState.IsPrime; break; } } } else { throw new Exception("Something went wrong please contact support."); } //TODO: check if task is in progress or unknown return(result); }