Esempio n. 1
0
        public void BackgroundJobExecutionException()
        {
            (var timer, var store, var worker, var executor, _) = Getclasses();
            var info = new BackgroundJobInfo
            {
                Id           = Guid.NewGuid(),
                JobName      = typeof(string).FullName,
                JobArgs      = "",
                Priority     = BackgroundJobPriority.Normal,
                CreationTime = DateTime.MinValue,
                NextTryTime  = DateTime.MinValue
            };

            store.Configure().GetWaitingJobsAsync(Arg.Any <int>()).ReturnsForAnyArgs(new List <BackgroundJobInfo> {
                info
            }
                                                                                     );
            executor.Configure().ExecuteAsync(Arg.Any <JobExecutionContext>()).ThrowsForAnyArgs <BackgroundJobExecutionException>();
            Should.NotThrow(() => worker.StartAsync());
            timer.Elapsed += Raise.Event();
            info.TryCount.ShouldBe <short>(1);
            info.LastTryTime.ShouldBe(DateTime.MinValue);
            executor.ReceivedWithAnyArgs(1).ExecuteAsync(Arg.Any <JobExecutionContext>());
            store.ReceivedWithAnyArgs(0).DeleteAsync(Arg.Any <Guid>());
            info.NextTryTime.ShouldBe(DateTime.MinValue.AddSeconds(60));
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="jobInfo"></param>
        /// <returns></returns>
        public virtual Task UpdateAsync(BackgroundJobInfo jobInfo)
        {
            if (jobInfo.IsAbandoned)
            {
                return(DeleteAsync(jobInfo.Id));
            }

            return(Task.FromResult(0));
        }
Esempio n. 3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="store"></param>
 /// <param name="jobInfo"></param>
 /// <returns></returns>
 protected virtual async Task TryUpdateAsync(IBackgroundJobStore store, BackgroundJobInfo jobInfo)
 {
     try
     {
         await store.UpdateAsync(jobInfo);
     }
     catch (Exception updateEx)
     {
         Logger.LogException(updateEx);
     }
 }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="jobInfo"></param>
        /// <param name="clock"></param>
        /// <returns></returns>
        protected virtual DateTime?CalculateNextTryTime(BackgroundJobInfo jobInfo, IClock clock)
        {
            var nextWaitDuration = WorkerOptions.DefaultFirstWaitDuration * Math.Pow(WorkerOptions.DefaultWaitFactor, jobInfo.TryCount - 1);
            var nextTryDate      = jobInfo.LastTryTime?.AddSeconds(nextWaitDuration) ??
                                   clock.Now.AddSeconds(nextWaitDuration);

            if (nextTryDate.Subtract(jobInfo.CreationTime).TotalSeconds > WorkerOptions.DefaultTimeout)
            {
                return(null);
            }

            return(nextTryDate);
        }
Esempio n. 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="jobName"></param>
        /// <param name="args"></param>
        /// <param name="priority"></param>
        /// <param name="delay"></param>
        /// <returns></returns>
        protected virtual async Task <Guid> EnqueueAsync(string jobName, object args, BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan?delay = null)
        {
            var jobInfo = new BackgroundJobInfo
            {
                Id           = Guid.NewGuid(),
                JobName      = jobName,
                JobArgs      = Serializer.Serialize(args),
                Priority     = priority,
                CreationTime = Clock.Now,
                NextTryTime  = Clock.Now
            };

            if (delay.HasValue)
            {
                jobInfo.NextTryTime = Clock.Now.Add(delay.Value);
            }

            await Store.InsertAsync(jobInfo);

            return(jobInfo.Id);
        }
Esempio n. 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="jobInfo"></param>
        /// <returns></returns>
        public virtual Task InsertAsync(BackgroundJobInfo jobInfo)
        {
            _jobs[jobInfo.Id] = jobInfo;

            return(Task.FromResult(0));
        }