private async Task PenaltyWorker(CancellationToken cancellationToken)
        {
            try{
                var penalties = await _repository.GetActivePenalties(cancellationToken);

                foreach (var penalty in penalties)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }
                    try{
                        _logger.LogInformation($"PenaltyWorker: Start process penalty: {penalty.Id}");
                        await _penaltyProcessor.Process(penalty, cancellationToken);

                        await _repository.Save(penalty);

                        _logger.LogInformation($"PenaltyWorker: End process penalty: {penalty.Id}");
                    }
                    catch (Exception e) {
                        _logger.LogError(e, $"PenaltyWorker: {e.Message}");
                    }
                }
            }
            catch (Exception e) {
                _logger.LogError(e, $"PenaltyWorker: {e.Message}");
            }
        }
Beispiel #2
0
        public async Task Handle(
            PenaltyOperationCreated message,
            CancellationToken cancellationToken)
        {
            _logger.LogInformation($"PenaltyOperationCreated for penalty {message.PenaltyId} has received");
            var penalty = await _penaltyRepository.Get(message.PenaltyId, cancellationToken);

            if (penalty == null)
            {
                throw new InvalidOperationException($"Penalty: {message.PenaltyId} not found");
            }
            await _penaltyProcessor.Process(penalty, cancellationToken);

            await _penaltyRepository.Save(penalty);
        }
        public async Task <IActionResult> CancelPenalty(
            [FromServices] IPenaltyRepository repository,
            [FromRoute] Guid id,
            CancellationToken cancellationToken)
        {
            var penalty = await repository.Get(id, HttpContext.GetGuildId(), cancellationToken);

            if (penalty == null)
            {
                throw new ApiException(HttpStatusCode.NotFound, ErrorCodes.PenaltyNotFound, $"Penalty {id} not found");
            }

            try{
                penalty.MakeCancel();
                await repository.Save(penalty);
            }
            catch (InvalidOperationException) {
                throw new ApiException(HttpStatusCode.Conflict, ErrorCodes.IncorrectOperation, $"Incorrect penalty state");
            }

            return(Ok(new { }));
        }