public async Task <ActionResult> Put(string fabelioProductURL) { var(validateResult, pageResponse) = await Validate(fabelioProductURL, scenario : "update-product"); if (validateResult is BadRequestObjectResult) { return(validateResult); } var product = _productRepo.ProductSet.First(o => o.PageUrl == fabelioProductURL); if (ProductInRunners.Contains(product.Id.ToString())) { return(Reply($"Product in running for updated")); } else { await AddProductToRunner(product.Id.ToString()); } await new ProductSync(fabelioProductURL, _httpClient, entity: product, enableUrlValidation: false).StartSync(IntervalProductRecordedInMinutes); product.LastSyncStatus = pageResponse.StatusCode; _productRepo.ProductSet.Update(product); await _unitOfWork.SaveChangesAsync(); await RemoveProductFromRunner(product.Id.ToString()); return(Reply($"Product has been updated")); }
public async Task <ActionResult> Sync(string syncTime) { if (ProductInRunners.Count >= MAX_RUNNER_INSTANCE) { return(Reply("Waiting available runner")); } var producerRunners = ProductInRunners.Select(o => Guid.Parse(o)); var productsToUpdate = _productRepo.ProductSet.AsNoTracking().Where(c => c.NextSyncAt < DateTime.Now && !producerRunners.Contains(c.Id)).Select(s => new { id = s.Id, url = s.PageUrl }).ToList(); if (productsToUpdate.Any()) { var httpClient = _clientFactory.CreateClient(); Parallel.ForEach(productsToUpdate, async p => { using (var scope = _provider.CreateScope()) { var productRepo = scope.ServiceProvider.GetRequiredService <IProductRepository>(); var unitOfWork = scope.ServiceProvider.GetRequiredService <IUnitOfWork>(); if (ProductInRunners.Contains(p.id.ToString())) { return; } else { await AddProductToRunner(p.id.ToString()); } var product = productRepo.ProductSet.Find(p.id); try { await new ProductSync(p.url, httpClient, product).StartSync(IntervalProductRecordedInMinutes); } catch (System.Net.Http.HttpRequestException) { product.LastSyncStatus = HttpStatusCode.RequestTimeout; product.LastSyncAt = DateTime.Now; product.GenNextSync(IntervalProductRecordedInMinutes); } catch (OpenQA.Selenium.NotFoundException) { product.LastSyncStatus = HttpStatusCode.NotFound; product.LastSyncAt = DateTime.Now; product.GenNextSync(IntervalProductRecordedInMinutes); } catch (OpenQA.Selenium.WebDriverException) { product.LastSyncStatus = HttpStatusCode.RequestTimeout; product.LastSyncAt = DateTime.Now; product.GenNextSync(IntervalProductRecordedInMinutes); } catch (Exception) { product.LastSyncStatus = HttpStatusCode.RequestTimeout; product.LastSyncAt = DateTime.Now; product.GenNextSync(IntervalProductRecordedInMinutes); } productRepo.ProductSet.Update(product); await unitOfWork.SaveChangesAsync(); await RemoveProductFromRunner(product.Id.ToString()); } }); } return(await Task.FromResult(Reply("Successfully Sync"))); }