Пример #1
0
        public void SubscribeToQueue(InterserviceQueueTypeEnum queueType)
        {
            _logger.Information($"Subscribing to queue with type '{queueType}'...");

            if (_channelModel == null)
            {
                var connection = _rabbitConnectionService.GetConnection();
                _channelModel = connection.CreateModel();
            }

            var consumer = new EventingBasicConsumer(_channelModel);

            consumer.Received += async(model, eventArgs) =>
            {
                try
                {
                    await ProcessReceivedMessageAsync(model, eventArgs);
                }
                catch (Exception exception)
                {
                    _logger.Write(exception);
                }
            };

            var queueDeclareResult = _channelModel.QueueDeclare(queueType);

            _channelModel.BasicConsume(queueDeclareResult.QueueName, true, consumer);

            _logger.Information($"Successfully subscribed to queue with type '{queueType}'");
        }
Пример #2
0
        /// <summary>
        /// Отправить HTTP-запрос (core-метод)
        /// </summary>
        private async Task <HttpResponseMessage> SendRequestCoreAsync(HttpRequestModel requestModel)
        {
            var httpRequestLogText = $"HTTP {requestModel.Method.Method} request to uri '{requestModel.Uri}'";

            _logger.Information($"Sending {httpRequestLogText}...");

            using var httpRequest = new HttpRequestMessage(requestModel.Method, requestModel.Uri);

            foreach (var httpHeader in requestModel.Headers)
            {
                httpRequest.Headers.Add(httpHeader.Key, httpHeader.Value);
            }

            if (requestModel.Body != null)
            {
                var httpRequestBodyJson = requestModel.Body.ToJson();
                httpRequest.Content = new StringContent(httpRequestBodyJson, Encoding.UTF8, MediaTypeNames.Application.Json);

                _logger.Trace($"HTTP request body = '{httpRequestBodyJson}'");
            }

            var httpResponse = await _httpClient.SendAsync(httpRequest);

            if (httpResponse.IsSuccessStatusCode)
            {
                _logger.Information($"{httpRequestLogText} was successfully sent");
            }
            else
            {
                _logger.Warning($"{httpRequestLogText} failed (status code = '{httpResponse.StatusCode}')");
            }

            return(httpResponse);
        }
Пример #3
0
        public async Task SendAsync()
        {
            _logger.Information("Sending report...");

            var participantCount = await _participantService.GetCountAsync();

            var messageModel = new ParticipantReportMessageModel
            {
                ParticipantCount = participantCount
            };

            _rabbitPublicationService.PublishMessage(InterserviceQueueTypeEnum.Deneb, messageModel);

            _logger.Information("Report was sucessfully sent...");
        }
Пример #4
0
        public async Task <bool> AreCredentialsValidAsync(UserCredentialsModel userCredentialsModel)
        {
            var userCredentialsLogText = $"user credentials with login '{userCredentialsModel.Login}'";

            _logger.Information($"Checking if {userCredentialsLogText} are valid...");

            var vegaConfiguration = _starsConfigurationService.Root.Vega;

            if (string.IsNullOrEmpty(vegaConfiguration.HostName))
            {
                throw new ConfigurationParameterException("Vega host name is null or empty");
            }

            var uri = $"{vegaConfiguration.HostName}:{vegaConfiguration.Port}";

            uri = uri.AddQueryPath(StarsApiConstants.Vega.USER_ACCOUNT_ARE_CREDENTIALS_VALID);

            var requestDto = new UserAreCredentialsValidRequestDto
            {
                Login = userCredentialsModel.Login,
                PasswordHashBase64 = userCredentialsModel.Password.GetSHA256().ToBase64()
            };

            var requestModel = new HttpRequestModel
            {
                Method = HttpMethod.Post,
                Uri    = uri,
                Body   = requestDto
            };

            var response = await _httpService.SendRequestAsync <UserAreCredentialsValidResponseDto>(requestModel);

            if (!response.IsSuccessful)
            {
                throw new InterserviceApiException($"Request to Vega API failed (uri = '{uri}')");
            }

            if (response.Body.AreUserCredentialsValid)
            {
                _logger.Information($"Check has been completed, {userCredentialsLogText} are valid");
            }
            else
            {
                _logger.Information($"Check has been completed, {userCredentialsLogText} are not valid");
            }

            return(response.Body.AreUserCredentialsValid);
        }
Пример #5
0
        public void CreateConnection()
        {
            var hostName = _starsConfigurationService.Root.Rabbit.HostName;
            var port     = _starsConfigurationService.Root.Rabbit.Port;

            _logger.Information($"Creating connection to RabbitMQ server '{hostName}:{port}'...");

            var connectionFactory = new ConnectionFactory()
            {
                HostName = hostName,
                Port     = port
            };

            _connection?.Dispose();
            _connection = connectionFactory.CreateConnection();

            _logger.Information($"Connection to RabbitMQ server '{hostName}:{port}' was successfully established");
        }
Пример #6
0
        public void PublishMessage(InterserviceQueueTypeEnum queueType, IInterserviceMessageModel messageModel)
        {
            _logger.Information($"Publishing interservice message to queue with type = '{queueType}'...");
            _logger.Debug($"Interservice message = '{messageModel.ToJson()}'");

            var connection = _rabbitConnectionService.GetConnection();

            using var channelModel = connection.CreateModel();

            var queueDeclareResult = channelModel.QueueDeclare(queueType);

            var messageJson  = messageModel.ToJson();
            var messageBytes = Encoding.UTF8.GetBytes(messageJson);

            channelModel.BasicPublish("", queueDeclareResult.QueueName, null, messageBytes);

            _logger.Information($"Interservice message was successfully published to queue with type = '{queueType}'");
        }
Пример #7
0
        public async Task <ReportModel[]> GetAllOrderedAsync()
        {
            _logger.Information("Getting all reports ordered by date of creation...");

            var reports = await _repository.GetNoTrackingQuery()
                          .OrderBy(report => report.DateOfCreationUtc)
                          .ForEachOfQueryable(report =>
            {
                report.DateOfCreationUtc = DateTime.SpecifyKind(report.DateOfCreationUtc, DateTimeKind.Utc);
            })
                          .ToArrayAsync();

            var reportModels = _mapper.Map <ReportModel[]>(reports);

            _logger.Information($"All reports were successfully loaded (count = {reportModels.Length})");

            return(reportModels);
        }
Пример #8
0
        public async Task <int> GetCountAsync()
        {
            _logger.Information("Getting participant count...");

            var participantCount = await _repository.GetCountAsync();

            _logger.Information($"Participant count = {participantCount}");

            return(participantCount);
        }
Пример #9
0
        public async Task <UserAccountModel> GetByIdAsync(int userAccountId)
        {
            _logger.Information($"Getting user account with id = {userAccountId}...");

            var userAccount = await _repository.GetByIdAsync(userAccountId);

            if (userAccount == null)
            {
                throw new DataModelNotFoundException($"User account with id = {userAccountId} was not found");
            }

            ConvertDateTimeValuesToUtc(userAccount);
            var userAccountModel = _mapper.Map <UserAccountModel>(userAccount);

            _logger.Information($"User account with id = {userAccountId} was successfully loaded");

            return(userAccountModel);
        }