Exemple #1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Process Id: {id} - Worker executando em: {time}",
                                       Process.GetCurrentProcess().Id,
                                       DateTimeOffset.Now);

                foreach (string host in _serviceConfigurations.Hosts)
                {
                    _logger.LogInformation(
                        $"Verificando a disponibilidade do host {host}");

                    var resultado = new ResultadoMonitoramento();
                    resultado.Framework = _versaoFramework;
                    resultado.Horario   =
                        DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    resultado.Host = host;

                    // Verifica a disponibilidade efetuando um ping
                    // no host que foi configurado em appsettings.json
                    try
                    {
                        using (Ping p = new Ping())
                        {
                            var resposta = p.Send(host);
                            resultado.Status = resposta.Status.ToString();
                        }
                    }
                    catch (Exception ex)
                    {
                        resultado.Status    = "Exception";
                        resultado.Exception = ex.Message;
                    }


                    string jsonResultado =
                        JsonSerializer.Serialize(resultado, _jsonOptions);

                    if (resultado.Exception == null)
                    {
                        _logger.LogInformation(jsonResultado);
                    }
                    else
                    {
                        _logger.LogError(jsonResultado);
                    }
                }

                await Task.Delay(
                    _serviceConfigurations.Intervalo, stoppingToken);
            }
        }
Exemple #2
0
        public override Task StopAsync(CancellationToken cancellationToken)
        {
            var dadosLog = new ResultadoMonitoramento()
            {
                Site          = "",
                Horario       = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                DescricaoErro = "The service has been stopped...",
                Status        = "Stopped"
            };

            disponibilidadeCollection.InsertOne(dadosLog);

            _logger.LogInformation("The service has been stopped...");
            return(base.StopAsync(cancellationToken));
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation(
                $"{IDENTIFICACAO_WORKER} - iniciando execução em: {DateTime.Now}");

            var mongoClient = new MongoClient(
                _configuration.GetConnectionString("BaseMonitoramentoSites"));
            var db = mongoClient.GetDatabase("DBMonitoramento");
            var disponibilidadeCollection =
                db.GetCollection <ResultadoMonitoramento>("Disponibilidade");

            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

                var sites = _configuration["Sites"]
                            .Split("|", StringSplitOptions.RemoveEmptyEntries);
                foreach (string site in sites)
                {
                    var dadosLog = new ResultadoMonitoramento()
                    {
                        Site    = site,
                        Horario =
                            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                    };

                    using (var client = new HttpClient())
                    {
                        client.BaseAddress = new Uri(site);
                        client.DefaultRequestHeaders.Accept.Clear();

                        try
                        {
                            // Envio da requisicao a fim de determinar se
                            // o site esta no ar
                            HttpResponseMessage response =
                                client.GetAsync("").Result;

                            dadosLog.Status = (int)response.StatusCode + " " +
                                              response.StatusCode;
                            if (response.StatusCode != System.Net.HttpStatusCode.OK)
                            {
                                dadosLog.DescricaoErro = response.ReasonPhrase;
                            }
                        }
                        catch (Exception ex)
                        {
                            dadosLog.Status        = "Exception";
                            dadosLog.DescricaoErro = ex.Message;
                        }
                    }

                    string jsonResultado =
                        JsonSerializer.Serialize(dadosLog);

                    if (dadosLog.DescricaoErro == null)
                    {
                        _logger.LogInformation(jsonResultado);
                    }
                    else
                    {
                        _logger.LogError(jsonResultado);
                    }

                    disponibilidadeCollection.InsertOne(dadosLog);
                }

                await Task.Delay(Convert.ToInt32(
                                     _configuration["IntervaloExecucao"]), stoppingToken);
            }
        }
Exemple #4
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation("Azure na Pratica - 2o. dia...");
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Process Id: {id} - Worker executando em: {time}",
                                       Process.GetCurrentProcess().Id,
                                       DateTimeOffset.Now);

                foreach (string host in _serviceConfigurations.Hosts)
                {
                    _logger.LogInformation(
                        $"Verificando a disponibilidade do host {host}");

                    var resultado = new ResultadoMonitoramento();
                    resultado.Framework = _versaoFramework;
                    resultado.Horario   =
                        DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    resultado.Host = host;

                    using (var client = new HttpClient())
                    {
                        client.BaseAddress = new Uri(host);
                        client.DefaultRequestHeaders.Accept.Clear();

                        try
                        {
                            // Envio da requisicao a fim de determinar se
                            // o site esta no ar
                            HttpResponseMessage response =
                                client.GetAsync("").Result;

                            resultado.Status = (int)response.StatusCode + " " +
                                               response.StatusCode;
                            if (response.StatusCode != HttpStatusCode.OK)
                            {
                                resultado.Exception = response.ReasonPhrase;
                            }
                        }
                        catch (Exception ex)
                        {
                            resultado.Status    = "Exception";
                            resultado.Exception = ex.Message;
                        }
                    }

                    string jsonResultado =
                        JsonSerializer.Serialize(resultado, _jsonOptions);

                    if (resultado.Exception == null)
                    {
                        _logger.LogInformation(jsonResultado);
                    }
                    else
                    {
                        _logger.LogError(jsonResultado);
                    }


                    // Gravando o resultado utilizando Azure Table Storage
                    MonitoramentoEntity dadosMonitoramento =
                        new MonitoramentoEntity(
                            "JobMonitoramentoSites", resultado.Horario);
                    dadosMonitoramento.Local    = Environment.MachineName;
                    dadosMonitoramento.DadosLog = jsonResultado;

                    var insertOperation = TableOperation.Insert(dadosMonitoramento);
                    var resultInsert    = _monitoramentoTable.ExecuteAsync(insertOperation).Result;
                    _logger.LogInformation(JsonSerializer.Serialize(resultInsert));

                    Thread.Sleep(3000);
                }

                await Task.Delay(
                    _serviceConfigurations.Intervalo, stoppingToken);
            }
        }