public static void ForEachLine <T>(this ExecutingProcess executingProcess, IQueryable <T> remainingLines, Action <T> action, int groupsOf = 100) where T : Entity, IProcessLineDataEntity, new() { var remainingNotExceptionsLines = remainingLines.Where(li => li.Exception(executingProcess.CurrentProcess) == null); var totalCount = remainingNotExceptionsLines.Count(); int j = 0; while (true) { List <T> lines = remainingNotExceptionsLines.Take(groupsOf).ToList(); if (lines.IsEmpty()) { return; } for (int i = 0; i < lines.Count; i++) { executingProcess.CancellationToken.ThrowIfCancellationRequested(); T pl = lines[i]; using (HeavyProfiler.Log("ProcessLine", () => pl.ToString())) { try { Transaction.ForceNew().EndUsing(tr => { action(pl); tr.Commit(); }); } catch (Exception e) { if (Transaction.InTestTransaction) { throw; } var exLog = e.LogException(); Transaction.ForceNew().EndUsing(tr => { new ProcessExceptionLineEntity { Exception = exLog.ToLite(), Line = pl.ToLite(), Process = executingProcess.CurrentProcess.ToLite() }.Save(); tr.Commit(); }); } executingProcess.ProgressChanged(j++, totalCount); } } } }
public static void ForEachNonTransactional <T>(this ExecutingProcess executingProcess, List <T> collection, Func <T, string> elementInfo, Action <T> action, string?status = null) { if (executingProcess == null) { collection.ProgressForeach(elementInfo, action, transactional: false); } else { var totalCount = collection.Count; int j = 0; foreach (var item in collection) { executingProcess.CancellationToken.ThrowIfCancellationRequested(); using (HeavyProfiler.Log("ProgressForeach", () => elementInfo(item))) { try { action(item); } catch (Exception e) { if (Transaction.InTestTransaction) { throw; } var exLog = e.LogException(); Transaction.ForceNew().EndUsing(tr => { new ProcessExceptionLineEntity { Exception = exLog.ToLite(), ElementInfo = elementInfo(item), Process = executingProcess.CurrentProcess.ToLite() }.Save(); tr.Commit(); }); } executingProcess.ProgressChanged(j++, totalCount, status); } } } }