public async Task <bool> GetLatestCities() { try { fileService.SaveFile(await httpClient.DownloadFile(), Constants.FILE_NAME); return(true); } catch (FileWriteException ex) { throw new FileWriteException(ex.Message, ex); } catch (Exception ex) { throw new GeneralException(ex.Message, ex); } }
public async Task <IActionResult> GetSourceScale() { try { var semaphore = new SemaphoreSlim(10, 50); var sources = (await cvmSourceService.GetAll(showDeactivated: true)).ToList(); List <int> errors = new List <int>(); List <Task> tasks = new List <Task>(); var i = 0; var lastId = sources.Last().Id; foreach (var s in sources) { try { tasks.Add(Task.Run(async() => { try { await semaphore.WaitAsync(); var content = await httpClientService.DownloadFile(s.Url); var scale = XmlParser <Scale> .GetScale(content, s.Document); s.CurrencyScale = scale.CurrencyScale; s.QuantityScale = scale.QuantityScale; var dbr = await cvmSourceService.Update(s); if (dbr != HttpStatusCode.OK) { Debug.Print($"Error: {s.Id}"); } else { Debug.Print($"Updated: {s.Id} out of {sources.Last().Id}"); } } catch (Exception e) { Debug.Print($"Error: {s.Id}"); errors.Add(s.Id); } finally { semaphore.Release(); } })); } catch (Exception ex) { Debug.Print($"ERROR {s.Id} | Reason: {ex.ToString()}"); } } Task.WaitAll(tasks.ToArray()); return(Ok(new { Code = 200, Message = "OK" })); } catch (Exception e) { logger.LogError(e, e.Message); return(StatusCode(500)); } }