Beispiel #1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    var endpoints = await _mongoService.GetAllEndpoints();

                    var products = await _mongoService.GetAllProducts();

                    Parallel.ForEach(endpoints, async endpoint =>
                    {
                        _logger.LogInformation($"Opening new thread for Endpoint ({endpoint.Name}) <{Thread.CurrentThread.ManagedThreadId}>.");
                        var process = ProcessFactoryResolver.Resolve(endpoint.Name, _logger, _config, _mongoService);

                        if (process != null)
                        {
                            await process.ExecuteProcessAsync(products, endpoint);
                        }
                        else
                        {
                            _logger.LogWarning($"No Process is implemented for endpoint ({endpoint.Name}).");
                        }
                    });
                }
                catch (Exception e)
                {
                    _logger.LogError($"Snatcher Worker failed unexpectedly => {e}", e);
                    // throw;
                }

                // 60 Seconds by Default
                await Task.Delay(int.Parse(_config.GetSection("Workers")["scraperInterval"]), stoppingToken);
            }
        }