Пример #1
0
        public async Task <Fundamento> ProcessarAsync(Papel papel)
        {
            try
            {
                // Busca o HtmlDocument do papel
                var url     = string.Format(BaseUrl, papel.Nome.Trim().ToUpper());
                var htmlDoc = await HtmlHelper.GetHtmlDocumentAsync(url);

                // Busca os dados da página e monta o fundamento
                var fundamento = new Fundamento
                {
                    PapelId      = papel.Id,
                    LPA          = ParseDoubleValue(htmlDoc, LPAPath),
                    ROE          = ParseDoubleValue(htmlDoc, ROEPath),
                    VPA          = ParseDoubleValue(htmlDoc, VPAPath),
                    ROIC         = ParseDoubleValue(htmlDoc, ROICPath),
                    ValorMercado = ParseDecimalValue(htmlDoc, ValorMercadoPath)
                };

                _logger.LogInformation($"Processado com sucesso o papel {papel.Nome}.");

                // Retorna o fundamento processado
                return(fundamento);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                throw;
            }
        }
Пример #2
0
 public async void ReplicarFundamento(Fundamento fundamento)
 {
     if (fundamento == null)
     {
         throw new ArgumentNullException(nameof(fundamento));
     }
     await ReplicarFundamentoAsync(fundamento);
 }
Пример #3
0
        private async Task ReplicarFundamentoAsync(Fundamento fundamento)
        {
            // Busca os nodos de replicação ativos
            var nodosReplicacao = await _unitOfWork.Replicacoes.Find(x => x.Ativo).ToListAsync();

            // Replica o fundamento em cada um dos nodos enviando uma requisição HTTP
            foreach (var nodo in nodosReplicacao)
            {
                var fundamentoDto = _mapper.Map <FundamentoDto>(fundamento);
                await ReplicarFundamentoAsync(nodo.Url, fundamentoDto);
            }
        }
Пример #4
0
        private async Task ReplicarFundamentoAsync(Fundamento fundamento, SemaphoreSlim semaforo, int intervalo)
        {
//            try
//            {
//                // Processa o fundamento e adiciona na lista
//                var fundamento = await ProcessarAsync(papel);
//                fundamentos.Add(fundamento);
//
//                // Aguarda cinco segundos entre processamentos para evitar bloqueio de IP
//                _logger.LogInformation($"Delay de {intervaloProcessamento} segundos após processar o papel {papel.Id}...");
//                await Task.Delay(TimeSpan.FromSeconds(intervaloProcessamento));
//            }
//            catch (Exception e)
//            {
//                _logger.LogError($"Falha em processar o papel {papel.Nome}:{e.Message}");
//            }
//            finally
//            {
//                semaforo.Release();
//                _logger.LogDebug($"Papel {papel.Nome} saiu do semáforo.");
//            }

            try
            {
                await ReplicarFundamentoAsync(fundamento);

                // Aguarda X segundos entre processamentos
                _logger.LogInformation($"Delay de {intervalo} segundos após processar o fundamento do papel de id {fundamento.PapelId}...");
                await Task.Delay(TimeSpan.FromSeconds(intervalo));
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
            }
            finally
            {
                semaforo.Release();
//                _logger.LogDebug($"Papel {papel.Nome} saiu do semáforo.");
            }
        }