Exemplo n.º 1
0
 /// <summary>
 /// اجرای محاسبات برای یک نفر
 /// </summary>
 /// <param name="personId">شناسه شخص</param>
 /// <param name="todate">انتهای بازه ی محاسبات</param>
 public void Execute(string CallerIdentity, decimal personId, DateTime toDate)
 {
     try
     {
         ExecutablePersonCalculation MustExecuted = ExecutablePersonCalculation.GetExecutablePersonCalcRepositoy(false).GetByPrsId(personId, toDate);
         ThreadHelper.CleanupThreads();
         logger.Logger.Info(
             string.Format("Execute for person '{0}' start at '{1}', Total Thread Count: '{2}', Runing Thread Count: '{3}'", personId.ToString()
                           , DateTime.Now.ToShortDateString()
                           , ThreadHelper.TotalThreadCount
                           , ThreadHelper.TotalExecutingThreadCount));
         logger.Flush();
         if (MustExecuted != null /*&& !MustExecuted.CalculationIsValid*/)
         {
             MustExecuted.ToDate = toDate;
             ExecutableThread.ThreadParam param = ThreadHelper.ForceToExecute(CallerIdentity, MustExecuted, this.FinishedCallback);
             if (param != null)
             {
                 this.Execute(param);
             }
         }
     }
     catch (BaseException ex)
     {
         throw ex;
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// این تابع توسط نخ فراخوانی میشود و نگاشت ترددها و اجرای قوانین را انجام میدهد
        /// </summary>
        /// <param name="ExeutableThread"></param>
        private void Execute(object state)
        {
            if (stopEngine)
            {
                return;
            }
            ExecutableThread.ThreadParam param = (ExecutableThread.ThreadParam)state;
            if (param == null)
            {
                return;
            }
            ThreadHelper.SetThreadContext(param.CallerIdentity, param.PersonId, Thread.CurrentThread);

            try
            {
                IEngineEnvironment engEnvironment = new EngineEnvironment();

                ExecuteTrafficMapper exTrafficMapper = new ExecuteTrafficMapper();
                exTrafficMapper.Execute(param.PersonId, param.FromDate, param.ToDate);
                //NHibernateSessionManager.Instance.ClearSession();
                ExecuteRule exRule = new ExecuteRule(engEnvironment);
                exRule.Execute(param.ExecutablePrsCalcId, param.PersonId, param.FromDate, param.ToDate);

                //تست باید گردد
                NHibernateSessionManager.Instance.ClearSession();
            }
            catch (Exception ex)
            {
                ThreadHelper.NotExecute(param.CallerIdentity, param.PersonId);
                logger.Logger.Error(String.Format("خطا در اجرای محاسبات PrsonId:{0}, Message:{1}", param.PersonId, ex.StackTrace), ex);
                logger.Flush();
                if (!param.ExecuteByRobot)
                {
                    throw ex;
                }
            }
            finally
            {
                if (param.FinishedCallback != null)
                {
                    this.FinishedCallback(param.CallerIdentity, param.PersonId);
                }
                GC.Collect();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// این تابع وظیفه اجرای قوانین برای تماممی اشخاص شده در پارامتر تا تاریخ ورودی را برعهده دارد
        /// </summary>
        /// <param name="Persons"></param>
        /// <param name="Date"></param>
        public void Execute(string CallerIdentity, IList <decimal> Persons, DateTime toDate)
        {
            try
            {
                IList <ExecutablePersonCalculation> MustExecuted = ExecutablePersonCalculation.GetExecutablePersonCalcRepositoy(false).GetAllByPrsIds(Persons, toDate);
                ThreadHelper.CleanupThreads();
                IList <ExecutablePersonCalculation> Threads = ThreadHelper.AddThreads(CallerIdentity, MustExecuted, this.FinishedCallback);
                logger.Logger.Info(
                    string.Format("Execute all start at '{0}', Total Thread Count: '{1}', Executable Thread Count: '{2}', Runing Thread Count: '{3}'", DateTime.Now.ToShortDateString()
                                  , ThreadHelper.TotalThreadCount
                                  , ThreadHelper.TotalThreadCount - ThreadHelper.TotalExecutingThreadCount
                                  , ThreadHelper.TotalExecutingThreadCount));
                logger.Flush();

                IList <Action> actions = new List <Action>();

                foreach (ExecutablePersonCalculation item in Threads /*.Where(x => x.CalculationIsValid == false)*/)
                {
                    item.ToDate = toDate;
                    ExecutableThread.ThreadParam param = ThreadHelper.PrepareToExecute(CallerIdentity, item);
                    if (param != null)
                    {
                        //ThreadPool.QueueUserWorkItem(new WaitCallback(this.Execute), param);
                        actions.Add(() => this.Execute(param));
                    }
                }
                Task.Factory.StartNew(() => Parallel.Invoke(
                                          new ParallelOptions()
                {
                    MaxDegreeOfParallelism = MaxThread
                },
                                          actions.ToArray()));
            }
            catch (Exception ex)
            {
                logger.Error("Execute(string CallerIdentity, IList<decimal> Persons, DateTime toDate)->", "" + ex.Message, ex);
                logger.Flush();
                throw ex;
            }
        }