Exemplo n.º 1
0
 public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.AddToSet(
         "processing",
         context.JobId,
         JobHelper.ToTimestamp(DateTime.UtcNow));
 }
Exemplo n.º 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;
            }
        }
Exemplo n.º 3
0
        public void Ctor_ShouldSetPropertiesCorrectly()
        {
            var context = new ApplyStateContext(
                _stateContext.Object,
                _newState.Object,
                OldState);

            Assert.Equal(OldState, context.OldStateName);
            Assert.Same(_newState.Object, context.NewState);
            Assert.Same(_job, context.Job);
        }
Exemplo n.º 4
0
            public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
            {
                var enqueuedState = context.NewState as EnqueuedState;
                if (enqueuedState == null)
                {
                    throw new InvalidOperationException(String.Format(
                        "`{0}` state handler can be registered only for the Enqueued state.",
                        typeof(Handler).FullName));
                }

                transaction.AddToQueue(enqueuedState.Queue, context.JobId);
            }
Exemplo n.º 5
0
            public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
            {
                var scheduledState = context.NewState as ScheduledState;
                if (scheduledState == null)
                {
                    throw new InvalidOperationException(String.Format(
                        "`{0}` state handler can be registered only for the Scheduled state.",
                        typeof(Handler).FullName));
                }

                var timestamp = JobHelper.ToTimestamp(scheduledState.EnqueueAt);
                transaction.AddToSet("schedule", context.JobId, timestamp);
            }
Exemplo n.º 6
0
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.DecrementCounter("stats:deleted");
 }
Exemplo n.º 7
0
 public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.IncrementCounter("stats:succeeded");
 }
Exemplo n.º 8
0
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
 }
Exemplo n.º 9
0
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.RemoveFromSet("schedule", context.JobId);
 }
Exemplo n.º 10
0
        private void ApplyState(ApplyStateContext context, IEnumerable<IApplyStateFilter> filters)
        {
            using (var transaction = context.Connection.CreateWriteTransaction())
            {
                foreach (var handler in _handlers.GetHandlers(context.OldStateName))
                {
                    handler.Unapply(context, transaction);
                }

                foreach (var filter in filters)
                {
                    filter.OnStateUnapplied(context, transaction);
                }

                transaction.SetJobState(context.JobId, context.NewState);

                foreach (var handler in _handlers.GetHandlers(context.NewState.Name))
                {
                    handler.Apply(context, transaction);
                }

                foreach (var filter in filters)
                {
                    filter.OnStateApplied(context, transaction);
                }

                if (context.NewState.IsFinal)
                {
                    transaction.ExpireJob(context.JobId, context.JobExpirationTimeout);
                }
                else
                {
                    transaction.PersistJob(context.JobId);
                }

                transaction.Commit();
            }
        }
Exemplo n.º 11
0
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.DecrementCounter("stats:succeeded");
 }
Exemplo n.º 12
0
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.RemoveFromSet("schedule", context.JobId);
 }
Exemplo n.º 13
0
 public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.IncrementCounter("stats:deleted");
 }
Exemplo n.º 14
0
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.RemoveFromList("deleted", context.JobId);
 }
Exemplo n.º 15
0
 public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.InsertToList("deleted", context.JobId);
     transaction.TrimList("deleted", 0, 99);
 }