public void Run() { while (true) { Product product; try { lock (_lockStack) { Log.WriteLog($"{_workerType} stack length {Products.Count}"); if (Products.Count > 0) { product = Products.Pop(); } else { break; } } } catch (Exception e) { Log.WriteLog($"Error while POPing data from stack: {e.Message}"); continue; } Log.WriteLog($"Starting sync for product: {JsonConvert.SerializeObject(product)}"); try { var resultProduct = Worker.GetSyncedData(product); if (resultProduct == null) { Log.WriteLog($"Synced product reference is null."); throw new Exception("Synced product reference is null."); } Worker.UpdateProductInDb(product); Log.WriteLog($"Updated product: {JsonConvert.SerializeObject(product)}"); } catch (InnerException e) { UnsuccessfulItemsHandler.AddError(e.Error); Log.WriteLog($"Inner error while syncing data: {e.Message} {Environment.NewLine} Stack trace: {e.StackTrace}"); } catch (Exception e) { UnsuccessfulItemsHandler.AddUnsuccessfulProduct(product.InternalId, "Техническая ошибка"); Log.WriteLog($"Unknown error while sync data: {e.Message} {Environment.NewLine} Stack trace: {e.StackTrace}"); } } }
private static void StartSynchronization(IEnumerable <SyncRunner> runners) { var threads = new List <Thread>(); MapsHelper.UpdateMaps(); try { var sw = Stopwatch.StartNew(); foreach (var runner in runners) { for (var i = 0; i < ConfigHelper.Config.ThreadsPerResource; i++) { var thread = new Thread(() => runner.Run()); thread.Start(); threads.Add(thread); } } foreach (var thread in threads) { thread.Join(); } sw.Stop(); Log.ResultLog(sw.Elapsed.ToString(), UnsuccessfulItemsHandler.GetErrors()); MySqlHelper.UpdateUnsuccessfulProducts(UnsuccessfulItemsHandler.GetUnsuccessfulProductIdsToUpdate()); } catch { Log.WriteLog("Unknown error when running sync processes"); } UnsuccessfulItemsHandler.ClearErrors(); }