Exemplo n.º 1
0
        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));
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult <LinkDto> > RequestNextPrimeCalculation(CheckNumberDto checkNumberDto)
        {
            _loggingManager.LogInformation($"RequestNextPrimeCalculation endpoint started. number: {checkNumberDto.Number}");

            await Task.Run(() =>
            {
                var command = _mediator.Send(new StartNextPrimeCalculationCommand
                {
                    Number = checkNumberDto.Number
                });
            });

            var linkDto = new LinkDto {
                Id = checkNumberDto.Number.ToString()
            };

            await _linksService.AddLinksAsync(linkDto);

            return(Ok(linkDto));
        }