示例#1
0
        public async Task <IList <WebSiteDto> > GetWebSitesListAsync()
        {
            HttpResponseMessage result =
                await SendHttpRequestAsync("/api/webserver/websites", HttpMethod.Get);

            if (result == null || !result.IsSuccessStatusCode)
            {
                await _slackLoggerService.LogAsync(LogLevel.Error, $"Can not GetWebSitesListAsync | Reason: {result?.ReasonPhrase}");

                return(null);
            }

            var body = await result.Content.ReadAsStringAsync();

            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <JObject, WebSiteDto>();
                cfg.AddProfile <WebSitesMappingProfile>();
            });

            var mapper = config.CreateMapper();

            var jsonObj = JObject.Parse(body);

            return(mapper.Map <List <WebSiteDto> >(jsonObj));
        }
示例#2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

                foreach (var server in _config.TargetServers)
                {
                    try
                    {
                        _webSiteService.SetServerAddress(server.Key, server.Value);

                        var sites = await _webSiteService.GetWebSitesListAsync();

                        var website = sites?.FirstOrDefault(c => string.Equals(c.Name, _config.WebSiteName, StringComparison.CurrentCultureIgnoreCase));

                        if (website == null)
                        {
                            continue;
                        }

                        var site = await _webSiteService.GetWebSiteAsync(website.Id);

                        if (site.ApplicationPool.Status != Status.Started)
                        {
                            await _slackLoggerService.LogAsync(LogLevel.Warning, $"{server.Key} ApplicationPool status is Stopped!");

                            if (await _webSiteService.SetAppPoolStatus(site.ApplicationPool.Id, Status.Started))
                            {
                                await _slackLoggerService.LogAsync(LogLevel.Information, $"{server.Key} ApplicationPool status started successfully.");
                            }
                        }

                        if (site.Status == Status.Started)
                        {
                            continue;
                        }

                        await _slackLoggerService.LogAsync(LogLevel.Warning, $"{server.Key} WebSite status is Stopped!");

                        if (await _webSiteService.SetWebSiteStatus(site.Id, Status.Started))
                        {
                            await _slackLoggerService.LogAsync(LogLevel.Information, $"{server.Key} WebSite status started successfully.");
                        }
                    }
                    catch (Exception ex)
                    {
                        await _slackLoggerService.LogAsync(LogLevel.Error, $"Exception thrown in at working on server {server.Key} | Reason: {ex.Message}");

                        _logger.LogError(ex, $"Exception thrown in at working on server {server.Key}");
                    }
                }

                await Task.Delay(_config.CheckingTimeInterval * 1000, stoppingToken);
            }
        }