Exemplo n.º 1
0
        public async Task RunAsync()
        {
            var chromeOptions = new ChromeOptions();

            chromeOptions.AddArgument("--headless");
            var domains = await domainServiceProvider.Get();

            int    counter    = 1;
            object lockObject = new object();
            var    list       = new ConcurrentQueue <Task>();

            Parallel.ForEach(domains, parallelOptions: new ParallelOptions {
                MaxDegreeOfParallelism = 16
            }, async(domain) =>
            {
                try
                {
                    using (var driver = new RemoteWebDriver(new Uri(options.Value.Url), chromeOptions))
                    {
                        driver.Navigate().GoToUrl($"http://{domain}");
                        var screenshoot = driver.GetScreenshot();
                        list.Enqueue(azureStorage.Put(screenshoot.AsByteArray, domain));
                        lock (lockObject)
                        {
                            logger.LogInformation($"processed {counter}/{domains.Count}");
                            Interlocked.Increment(ref counter);
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "Selenium error");
                }
            });

            await Task.WhenAll(list);
        }