public void OnStateUnapplied(
            ApplyStateContext context, IWriteOnlyTransaction transaction)
        {
            Assert.NotNull(context);
            Assert.NotNull(transaction);

            _results.Add(String.Format("{0}::{1}", _name, "OnStateUnapplied"));
        }
Example #2
0
 public override void Apply(
     ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.AddToSet(
         "failed",
         context.JobId,
         JobHelper.ToTimestamp(DateTime.UtcNow));
 }
 public ScheduledStateHandlerFacts()
 {
     var methodData = MethodData.FromExpression(() => Console.WriteLine());
     _context = new ApplyStateContext(
         new Mock<IStorageConnection>().Object,
         new StateContext(JobId, methodData), 
         new ScheduledState(EnqueueAt), 
         null);
 }
 public ProcessingStateHandlerFacts()
 {
     var methodData = MethodData.FromExpression(() => Console.WriteLine());
     _context = new ApplyStateContext(
         new Mock<IStorageConnection>().Object,
         new StateContext(JobId, methodData),
         new ProcessingState("SomeServer"), 
         null);
 }
        public SucceededStateHandlerFacts()
        {
            var methodData = MethodData.FromExpression(() => Console.WriteLine());

            _context = new ApplyStateContext(
                new Mock<IStorageConnection>().Object,
                new StateContext("1", methodData), 
                new SucceededState(), 
                null);
        }
Example #6
0
            public override 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);
            }
Example #7
0
            public override 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);
            }
 public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.AddToSet("processing", context.JobId, JobHelper.ToTimestamp(DateTime.UtcNow));
 }
 public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
 }
Example #10
0
 public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.InsertToList("deleted", context.JobId);
     transaction.TrimList("deleted", 0, 99);
 }
 public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     //context.JobExpirationTimeout = TimeSpan.FromSeconds(10);
 }
Example #12
0
 /// <summary>
 /// 应用
 /// </summary>
 /// <param name="context">应用状态上下文</param>
 /// <param name="transaction">事务</param>
 public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction) => transaction.AddToSet(Const.Failed, context.BackgroundJob.Id, JobHelper.ToTimestamp(DateTime.UtcNow));
Example #13
0
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.RemoveFromList(State.Deleted, context.BackgroundJob.Id);
 }
 public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     context.JobExpirationTimeout = JobExpirationTimeout;
 }
 public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     Logger.InfoFormat($"Job `{context.BackgroundJob?.Id}` state `{context.OldStateName}` was unapplied.");
 }
 public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     Logger.InfoFormat(
         $"Job `{context.BackgroundJob?.Id}` state was changed from `{context.OldStateName}` to `{context.NewState?.Name}`");
 }
Example #17
0
 public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.InsertToList(State.Succeeded, context.BackgroundJob.Id);
     transaction.TrimList(State.Succeeded, 0, 99);
 }
Example #18
0
 public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     //context.JobExpirationTimeout = TimeSpan.MaxValue;
     context.JobExpirationTimeout = TimeSpan.FromDays(365 * 40);
 }
Example #19
0
        internal virtual void ApplyState(
            StateContext stateContext, 
            State electedState,
            string oldStateName,
            IEnumerable<IApplyStateFilter> filters)
        {
            var context = new ApplyStateContext(
                _connection, stateContext, electedState, oldStateName);

            context.ApplyState(GetHandlers(), filters);
        }
Example #20
0
 public override void Apply(
     ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.InsertToList("succeeded", context.JobId);
     transaction.TrimList("succeeded", 0, 99);
 }
        public BackgroundJob Create(CreateContext context)
        {
            var attemptsLeft = Math.Max(RetryAttempts, 0);
            var parameters   = context.Parameters.ToDictionary(
                x => x.Key,
                x => SerializationHelper.Serialize(x.Value, SerializationOption.User));

            var createdAt = DateTime.UtcNow;

            // Retry may cause multiple background jobs to be created, especially when there's
            // a timeout-related exception. But initialization attempt will be performed only
            // for the most recent job, leaving all the previous ones in a non-initialized state
            // and making them invisible to other parts of the system, since no one knows their
            // identifiers. Since they also will be eventually expired leaving no trace, we can
            // consider that only one background job is created, regardless of retry attempts
            // number.
            var jobId = RetryOnException(ref attemptsLeft, _ => context.Connection.CreateExpiredJob(
                                             context.Job,
                                             parameters,
                                             createdAt,
                                             TimeSpan.FromDays(30)));

            if (String.IsNullOrEmpty(jobId))
            {
                return(null);
            }

            var backgroundJob = new BackgroundJob(jobId, context.Job, createdAt);

            if (context.InitialState != null)
            {
                RetryOnException(ref attemptsLeft, attempt =>
                {
                    if (attempt > 0)
                    {
                        // Normally, a distributed lock should be applied when making a retry, since
                        // it's possible to get a timeout exception, when transaction was actually
                        // committed. But since background job can't be returned to a position where
                        // it's state is null, and since only the current thread knows the job's identifier
                        // when its state is null, and since we shouldn't do anything when it's non-null,
                        // there will be no any race conditions.
                        var data = context.Connection.GetJobData(jobId);
                        if (data == null)
                        {
                            throw new InvalidOperationException($"Was unable to initialize a background job '{jobId}', because it doesn't exists.");
                        }

                        if (!String.IsNullOrEmpty(data.State))
                        {
                            return;
                        }
                    }

                    using (var transaction = context.Connection.CreateWriteTransaction())
                    {
                        var applyContext = new ApplyStateContext(
                            context.Storage,
                            context.Connection,
                            transaction,
                            backgroundJob,
                            context.InitialState,
                            oldStateName: null,
                            profiler: context.Profiler);

                        StateMachine.ApplyState(applyContext);

                        transaction.Commit();
                    }
                });
            }

            return(backgroundJob);
        }
Example #22
0
 public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     UpdateTagState(context);
 }
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
 }
 public void OnStateUnapplied(ApplyStateContext context, Hangfire.Storage.IWriteOnlyTransaction transaction)
 {
 }
Example #25
0
 public override void Unapply(
     ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.RemoveFromList("succeeded", context.JobId);
 }
Example #26
0
 public override void Unapply(
     ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.DecrementCounter("stats:succeeded");
 }
Example #27
0
 /// <summary>
 /// 取消应用
 /// </summary>
 /// <param name="context">应用状态上下文</param>
 /// <param name="transaction">事务</param>
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction) => transaction.RemoveFromSet(Const.Failed, context.BackgroundJob.Id);
 public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.InsertToList("deleted", context.BackgroundJob.Id);
     transaction.TrimList("deleted", 0, RedisStorage.DeletedListSize);
 }
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.RemoveFromList("succeeded", context.BackgroundJob.Id);
 }
 public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     //TODO change to FromDays after testing
     context.JobExpirationTimeout = TimeSpan.FromDays(appSettings.JobSchedules.JobLogExpirationTimeOutInDays);
 }
Example #31
0
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.RemoveFromList("deleted", context.JobId);
 }
Example #32
0
 void IApplyStateFilter.OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
 }
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.RemoveFromSet("processing", context.JobId);
 }
Example #34
0
 public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     //设置过期时间,任务将在三天后过期,过期的任务会自动被扫描并删除
     context.JobExpirationTimeout = TimeSpan.FromDays(3);
 }
 public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     context.JobExpirationTimeout = TimeSpan.FromDays(3);
 }
Example #36
0
 public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     //throw new NotImplementedException();
 }
Example #37
0
        public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
        {
            var globalTimeout = CodingUtil.JobTimeoutDays();

            context.JobExpirationTimeout = TimeSpan.FromDays(globalTimeout);
        }
Example #38
0
 public override void Unapply(
     ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.RemoveFromSet("failed", context.JobId);
 }
Example #39
0
 public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
      if (context.NewState.IsFinal && context.NewState.Name == "Succeeded")
         //Send your message to client via signalR
 }}
 public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
 {
     transaction.AddToSet("failed", context.BackgroundJob.Id, JobHelper.ToTimestamp(DateTime.Now));
 }
Example #41
0
 /// <summary>
 /// 获取任务参数
 /// </summary>
 /// <param name="context"></param>
 /// <param name="paraName"></param>
 /// <returns></returns>
 internal static string GetTaskParameter(this ApplyStateContext context, string paraName)
 {
     return(context.Connection.GetJobParameter(context.BackgroundJob.Id, paraName));
 }
 public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction) => transaction.DecrementCounter(StateStatKey);