Advertisement
Guest User

Untitled

a guest
Apr 4th, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. using Azure.Storage.Blobs;
  2. using Elicit_Stampen_Screenshot_WorkerService.Interface;
  3. using Elicit_Stampen_Screenshot_WorkerService.Service;
  4. using OpenQA.Selenium.Chrome;
  5. using OpenQA.Selenium.Support.UI;
  6. using OpenQA.Selenium;
  7. using OpenQA.Selenium.Support.Extensions;
  8. using System.Drawing;
  9. using System.Diagnostics;
  10. using Microsoft.Extensions.Logging;
  11.  
  12. namespace Elicit_Stampen_Screenshot_WorkerService
  13. {
  14. public class Worker : BackgroundService
  15. {
  16. private readonly ILogger<Worker> _logger;
  17. private readonly IBlobStorageServiceClient _blobServiceClient;
  18. private readonly IConfiguration _configuration;
  19.  
  20. public Worker(ILogger<Worker> logger, IBlobStorageServiceClient blobServiceClient, IConfiguration configuration)
  21. {
  22. _logger = logger;
  23. _blobServiceClient = blobServiceClient;
  24. _configuration = configuration;
  25. }
  26.  
  27. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  28. {
  29. int taskDelayInMilliseconds = 0;
  30.  
  31. while (!stoppingToken.IsCancellationRequested)
  32. {
  33. _logger.LogInformation("Worker running at: {time}", DateTime.Now);
  34. int timeout = Convert.ToInt32(_configuration.GetSection("WorkerSetting")["TimeoutInSeconds"]);
  35. int timeIntervalInSeconds = Convert.ToInt32(_configuration.GetSection("WorkerSetting")["TimeIntervalInSeconds"]);
  36. int numberOfThreads = Convert.ToInt32(_configuration.GetSection("WorkerSetting")["NumberOfThreads"]);
  37.  
  38. _logger.LogInformation("Reading urls values from blob");
  39. var urlConfiguration = await _blobServiceClient.DownloadConfigrationBlob();
  40.  
  41.  
  42.  
  43. if (urlConfiguration?.Urls is null)
  44. {
  45. _logger.LogWarning("Reading returned no urls, check the blobstorage for url values");
  46. }
  47. else
  48. {
  49. Parallel.ForEach(urlConfiguration.Urls, new ParallelOptions { MaxDegreeOfParallelism = numberOfThreads }, item =>
  50. {
  51. if (Convert.ToBoolean(_configuration.GetSection("WorkerSetting")["TaskDelayForAllUrls"]))
  52. {
  53. taskDelayInMilliseconds = Convert.ToInt32(_configuration.GetSection("WorkerSetting")["SameTaskDelayInMilliseconds"]);
  54. }
  55. else
  56. {
  57. taskDelayInMilliseconds = Convert.ToInt32(item.WaitForLoadinMilliseconds);
  58. }
  59.  
  60. _logger.LogInformation("Preparing headless web browser");
  61. var options = new ChromeOptions();
  62. options.AddArguments(new List<string> { "--headless", "--hide-scrollbars" });
  63.  
  64. using var driver = new ChromeDriver(options);
  65. _logger.LogInformation("web browser complete");
  66. driver.Manage().Window.Size = new Size(item.Width, item.Height);
  67. driver.Navigate().GoToUrl(item.Url);
  68. driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(timeout);
  69.  
  70. try
  71. {
  72. _logger.LogInformation($"Searching class element: {timeout}");
  73. var element = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout)).Until(async (d) =>
  74. {
  75. d.Navigate().GoToUrl(item.Url);
  76. await Task.Delay(taskDelayInMilliseconds);
  77. return d.FindElement(By.ClassName("v-menu"));
  78. });
  79.  
  80. var screenshot = driver.TakeScreenshot();
  81. #if DEBUG
  82. screenshot.SaveAsFile(item?.Name ?? $"{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.png");
  83. #endif
  84. //Save to blobStorage
  85. _blobServiceClient.UploadImageToBlobAsync(screenshot, item?.Name);
  86. }
  87. catch (NoSuchElementException e)
  88. {
  89. _logger.LogInformation($"Exception trying login was timed out: {e.Message}");
  90. }
  91. catch (WebDriverTimeoutException e)
  92. {
  93. _logger.LogInformation($"Exception trying login was timed out: {e.Message}");
  94. }
  95. catch (Exception e)
  96. {
  97. _logger.LogInformation($"Exception: {e.Message}");
  98. }
  99. });
  100. }
  101. await Task.Delay(TimeSpan.FromSeconds(timeIntervalInSeconds), stoppingToken);
  102.  
  103. }
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement