public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.AddToSet(
         "processing",
         context.JobId,
         JobHelper.ToTimestamp(DateTime.UtcNow));
 }
Esempio 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;
            }
        }
        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);
        }
Esempio 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);
            }
Esempio 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);
            }
Esempio n. 6
0
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.DecrementCounter("stats:deleted");
 }
Esempio n. 7
0
 public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.IncrementCounter("stats:succeeded");
 }
Esempio n. 8
0
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
 }
Esempio n. 9
0
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.RemoveFromSet("schedule", context.JobId);
 }
Esempio 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();
            }
        }
Esempio n. 11
0
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.DecrementCounter("stats:succeeded");
 }
Esempio n. 12
0
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.RemoveFromSet("schedule", context.JobId);
 }
Esempio n. 13
0
 public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.IncrementCounter("stats:deleted");
 }
Esempio n. 14
0
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.RemoveFromList("deleted", context.JobId);
 }
Esempio n. 15
0
 public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.InsertToList("deleted", context.JobId);
     transaction.TrimList("deleted", 0, 99);
 }