Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Azure.Storage.Blobs;
- using Elicit_Stampen_Screenshot_WorkerService.Interface;
- using Elicit_Stampen_Screenshot_WorkerService.Service;
- using OpenQA.Selenium.Chrome;
- using OpenQA.Selenium.Support.UI;
- using OpenQA.Selenium;
- using OpenQA.Selenium.Support.Extensions;
- using System.Drawing;
- using System.Diagnostics;
- using Microsoft.Extensions.Logging;
- namespace Elicit_Stampen_Screenshot_WorkerService
- {
- public class Worker : BackgroundService
- {
- private readonly ILogger<Worker> _logger;
- private readonly IBlobStorageServiceClient _blobServiceClient;
- private readonly IConfiguration _configuration;
- public Worker(ILogger<Worker> logger, IBlobStorageServiceClient blobServiceClient, IConfiguration configuration)
- {
- _logger = logger;
- _blobServiceClient = blobServiceClient;
- _configuration = configuration;
- }
- protected override async Task ExecuteAsync(CancellationToken stoppingToken)
- {
- int taskDelayInMilliseconds = 0;
- while (!stoppingToken.IsCancellationRequested)
- {
- _logger.LogInformation("Worker running at: {time}", DateTime.Now);
- int timeout = Convert.ToInt32(_configuration.GetSection("WorkerSetting")["TimeoutInSeconds"]);
- int timeIntervalInSeconds = Convert.ToInt32(_configuration.GetSection("WorkerSetting")["TimeIntervalInSeconds"]);
- int numberOfThreads = Convert.ToInt32(_configuration.GetSection("WorkerSetting")["NumberOfThreads"]);
- _logger.LogInformation("Reading urls values from blob");
- var urlConfiguration = await _blobServiceClient.DownloadConfigrationBlob();
- if (urlConfiguration?.Urls is null)
- {
- _logger.LogWarning("Reading returned no urls, check the blobstorage for url values");
- }
- else
- {
- Parallel.ForEach(urlConfiguration.Urls, new ParallelOptions { MaxDegreeOfParallelism = numberOfThreads }, item =>
- {
- if (Convert.ToBoolean(_configuration.GetSection("WorkerSetting")["TaskDelayForAllUrls"]))
- {
- taskDelayInMilliseconds = Convert.ToInt32(_configuration.GetSection("WorkerSetting")["SameTaskDelayInMilliseconds"]);
- }
- else
- {
- taskDelayInMilliseconds = Convert.ToInt32(item.WaitForLoadinMilliseconds);
- }
- _logger.LogInformation("Preparing headless web browser");
- var options = new ChromeOptions();
- options.AddArguments(new List<string> { "--headless", "--hide-scrollbars" });
- using var driver = new ChromeDriver(options);
- _logger.LogInformation("web browser complete");
- driver.Manage().Window.Size = new Size(item.Width, item.Height);
- driver.Navigate().GoToUrl(item.Url);
- driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(timeout);
- try
- {
- _logger.LogInformation($"Searching class element: {timeout}");
- var element = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout)).Until(async (d) =>
- {
- d.Navigate().GoToUrl(item.Url);
- await Task.Delay(taskDelayInMilliseconds);
- return d.FindElement(By.ClassName("v-menu"));
- });
- var screenshot = driver.TakeScreenshot();
- #if DEBUG
- screenshot.SaveAsFile(item?.Name ?? $"{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.png");
- #endif
- //Save to blobStorage
- _blobServiceClient.UploadImageToBlobAsync(screenshot, item?.Name);
- }
- catch (NoSuchElementException e)
- {
- _logger.LogInformation($"Exception trying login was timed out: {e.Message}");
- }
- catch (WebDriverTimeoutException e)
- {
- _logger.LogInformation($"Exception trying login was timed out: {e.Message}");
- }
- catch (Exception e)
- {
- _logger.LogInformation($"Exception: {e.Message}");
- }
- });
- }
- await Task.Delay(TimeSpan.FromSeconds(timeIntervalInSeconds), stoppingToken);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement