public CalculationExceptionPersister(ICalculatorEngineFactory calculatorEngineFactory, IEventPublisher publisher) { this.calculatorEngineFactory = calculatorEngineFactory; this.publisher = publisher; Task.Factory.StartNew(() => { CalculationExceptionReady data; while (!calcEnded) { while (queue.TryDequeue(out data)) { var engine = calculatorEngineFactory.Create(); try { engine.AddCalculationException(data.CalculationId, data.EmployeeId, data.CalculationPathNo, data.Messages); } finally { calculatorEngineFactory.Release(engine); } } trigger.WaitOne(); } var x = 1; } ); }
public JobIndexPointPersister(ICalculatorEngineFactory calculatorEngineFactory, IEventPublisher publisher) { this.calculatorEngineFactory = calculatorEngineFactory; this.publisher = publisher; Task.Factory.StartNew(() => { JobIndexPointsReady data; while (!calcEnded) { while (queue.TryDequeue(out data)) { var engine = calculatorEngineFactory.Create(); try { if (data.PointsHolder.EmployeePointsForAdd != null) { engine.AddEmployeePoints(data.PointsHolder.EmployeePointsForAdd); } if (data.PointsHolder.EmployeePointsForUpdate != null) { engine.UpdateEmployeePoints(data.PointsHolder.EmployeePointsForUpdate); } } catch (Exception ex) { hasExceptionInPersist = true; publisher.Publish(new CalculationExceptionReady(data.CalculationId, data.EmployeeId, data.CalculationPathNo, ex)); } finally { calculatorEngineFactory.Release(engine); } } if (pathCompleted) { publisher.Publish(new PathPersisteCompleted(hasExceptionInPersist)); pathCompleted = false; } trigger.WaitOne(); } } ); }
private void start(Calculation calculation, bool doResume) { long totalCalcOperationCount = 0; long preCalcOperationDoneCount = 0; calculatorSession = new CalculatorSession { PathNo = 1 }; if (doResume) { var calcResult = calculation.CalculationResult; foreach (string s in calcResult.Messages) { messages.Add(s); } messages.Add("====================================================="); totalCalcOperationCount = calcResult.TotalEmployeesCount; preCalcOperationDoneCount = calcResult.EmployeesCalculatedCount; progress.SetProgress(totalCalcOperationCount, preCalcOperationDoneCount); if (calcResult.LastCalculatedPath.HasValue) { progress.SetLastCalculatedEmployee(calcResult.LastCalculatedEmployeeId, calcResult.LastCalculatedPath.Value); calculatorSession.PathNo = calcResult.LastCalculatedPath.Value; } var engine = calculatorEngineFactory.Create(); try { calculatorSession.AddCalculationPoints(engine.GetCalculationPiontBy(calculation.Id)); } finally { calculatorEngineFactory.Release(engine); } } Policy policy = null; messages.Add(DateTime.Now + "شروع محاسبه "); try { startTrigger.Set(); CalculationCompletedSub = new DelegateHandler <CalculationCompleted>(e => { var engine = calculatorEngineFactory.Create(); try { engine.AddUpdateCalculationPoint(calculatorSession.CalculationPoints); engine.UpdateCalculationResult(calculation, progress, this, messages); } finally { calculatorEngineFactory.Release(engine); } }); publisher.RegisterHandler(CalculationCompletedSub); rulsCompiledSub = new DelegateHandler <RulesCompiled>(e => { if (!doResume) { Task.Factory.StartNew(() => { var engine = calculatorEngineFactory.Create(); try { engine.UpdateCompileResult(calculation.Id, e.CompileResult.LibraryText, e.Rules); } finally { calculatorEngineFactory.Release(engine); } messages.Add(DateTime.Now + " قوانین کامپایل شدند"); }); } }); publisher.RegisterHandler(rulsCompiledSub); publisher.RegisterHandler <JobIndexPointsReady>(jobIndexPointPersister); publisher.RegisterHandler <CalculationExceptionReady>(calculationExceptionPersister); publisher.RegisterHandler <CalculationCompleted>(jobIndexPointPersister); publisher.RegisterHandler <CalculationCompleted>(calculationExceptionPersister); publisher.RegisterHandler <CalculationForPathCompleted>(jobIndexPointPersister); publisher.RegisterHandler <PathPersisteCompleted>(this); Dictionary <int, IList <Employee> > employeesWithPath; Period period; fetchPolicyAndEmployees(calculation, doResume, calculatorSession.PathNo, out policy, out employeesWithPath, out period); deleteCalculationException(calculation); long currentCalcOperationCount = 0; foreach (var employees in employeesWithPath) { currentCalcOperationCount = currentCalcOperationCount + employees.Value.Count(); } if (!doResume) { totalCalcOperationCount = currentCalcOperationCount; progress.SetProgress(totalCalcOperationCount, 0); } messages.Add(DateTime.Now + " تعداد " + currentCalcOperationCount + " عملیات محاسبه برای " + calculation.EmployeeCount + " کارمند آماده می باشد"); var currentCalcOperationDoneCount = preCalcOperationDoneCount; var pathCount = employeesWithPath.Count(); foreach (var emlpoyees in employeesWithPath) { calculatorSession.PathNo = emlpoyees.Key; foreach (var employee in emlpoyees.Value) { try { if (doStop || doPause) { break; } var pointsHolder = calculateIndices(calculation, policy, period, employee, calculatorSession); currentCalcOperationDoneCount++; progress.SetProgress(totalCalcOperationCount, currentCalcOperationDoneCount); addOrUpdateCalculationPoints(pointsHolder.CalculationPoints); publisher.Publish(new JobIndexPointsReady(pointsHolder, calculation.Id, employee.Id, calculatorSession.PathNo)); progress.SetLastCalculatedEmployee(employee.Id, calculatorSession.PathNo); } catch (Exception ex) { publisher.Publish(new CalculationExceptionReady(calculation.Id, employee.Id, calculatorSession.PathNo, ex)); calculatorSession.HasEmployeeCalculationFailed = true; messages.Add("*** خطا در محاسبه شاخص های کارمند " + employee.Id.EmployeeNo + " **** "); var logServiceMngt = LogServiceFactory.Create(); try { var logService = logServiceMngt.GetService(); logService.AddEventLog("JobIndexPointCalculator_EmpId:" + employee.Id.EmployeeNo, LogLevel.Error, null, this.GetType().Name, "start", ex.Message, ex.StackTrace); logService.AddExceptionLog(ex); } finally { LogServiceFactory.Release(logServiceMngt); } } } if (pathCount > calculatorSession.PathNo) { publisher.Publish(new CalculationForPathCompleted()); pathPersisterTrigger.WaitOne(); } if (calculatorSession.HasEmployeeCalculationFailed) { break; } } messages.Add(DateTime.Now + " تعداد " + currentCalcOperationDoneCount + " عملیات محاسبه انجام شد"); if (!(doStop || doPause)) { publisher.Publish(new CalculationCompleted()); } else if (doStop) { messages.Add(DateTime.Now + " لغو محاسبه."); } else if (doPause) { messages.Add(DateTime.Now + " وقفه در محاسبه."); } if (doPause || doStop) { var engine = calculatorEngineFactory.Create(); try { engine.AddUpdateCalculationPoint(calculatorSession.CalculationPoints); } finally { calculatorEngineFactory.Release(engine); } } pauseTrigger.Set(); } catch (Exception e) { startTrigger.Set(); calculatorSession.HasEmployeeCalculationFailed = true; messages.Add(DateTime.Now + "در آماده سازی محاسبه با مشکل مواجه شده است"); publisher.Publish(new CalculationCompleted()); } finally { state = CalculatorState.Completed; if (policy != null) { policy.Dispose(); } } }