Beispiel #1
0
        private static IState ElectState(
            ElectStateContext context,
            IEnumerable <IElectStateFilter> filters)
        {
            var statesToAppend = new List <IState>();

            foreach (var filter in filters)
            {
                var oldState = context.CandidateState;
                filter.OnStateElection(context);

                if (oldState != context.CandidateState)
                {
                    statesToAppend.Add(oldState);
                }
            }

            if (statesToAppend.Count > 0)
            {
                using (var transaction = context.Connection.CreateWriteTransaction())
                {
                    foreach (var state in statesToAppend)
                    {
                        transaction.AddJobState(context.JobId, state);
                    }

                    transaction.Commit();
                }
            }

            return(context.CandidateState);
        }
Beispiel #2
0
        public bool ChangeState(StateContext context, IState toState, string oldStateName)
        {
            try
            {
                var filterInfo = GetFilters(context.Job);

                var electStateContext = new ElectStateContext(context, toState, oldStateName);
                var electedState      = ElectState(electStateContext, filterInfo.ElectStateFilters);

                var applyStateContext = new ApplyStateContext(context, electedState, oldStateName);
                ApplyState(applyStateContext, filterInfo.ApplyStateFilters);

                // State transition was succeeded.
                return(true);
            }
            catch (Exception ex)
            {
                var failedState = new FailedState(ex)
                {
                    Reason = "An exception occurred during the transition of job's state"
                };

                var applyStateContext = new ApplyStateContext(context, failedState, oldStateName);

                // We should not use any state changed filters, because
                // some of the could cause an exception.
                ApplyState(applyStateContext, Enumerable.Empty <IApplyStateFilter>());

                // State transition was failed due to exception.
                return(false);
            }
        }