Ejemplo n.º 1
0
    public void Execute()
    {
        var user = ExecutionMode.Global().Using(_ => CurrentProcess.User.RetrieveAndRemember());

        using (UserHolder.UserSession(user))
        {
            using (ProcessLogic.OnApplySession(CurrentProcess))
            {
                if (UserEntity.Current == null)
                {
                    UserEntity.Current = AuthLogic.SystemUser !;
                }
                try
                {
                    Algorithm.Execute(this);

                    CurrentProcess.ExecutionEnd = Clock.Now;
                    CurrentProcess.State        = ProcessState.Finished;
                    CurrentProcess.Progress     = null;
                    CurrentProcess.User.ClearEntity();
                    using (OperationLogic.AllowSave <ProcessEntity>())
                        CurrentProcess.Save();
                }
                catch (OperationCanceledException e)
                {
                    if (!e.CancellationToken.Equals(this.CancellationToken))
                    {
                        throw;
                    }

                    CurrentProcess.SuspendDate = Clock.Now;
                    CurrentProcess.State       = ProcessState.Suspended;
                    using (OperationLogic.AllowSave <ProcessEntity>())
                        CurrentProcess.Save();
                }
                catch (Exception e)
                {
                    if (Transaction.InTestTransaction)
                    {
                        throw;
                    }

                    CurrentProcess.State         = ProcessState.Error;
                    CurrentProcess.ExceptionDate = Clock.Now;
                    CurrentProcess.Exception     = e.LogException(el => el.ActionName = CurrentProcess.Algorithm.ToString()).ToLite();
                    using (OperationLogic.AllowSave <ProcessEntity>())
                        CurrentProcess.Save();
                }
                finally
                {
                    ProcessRunnerLogic.OnFinally?.Invoke(this);
                }
            }
        }
    }