diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs index 7fdf9b1fe0..c95c1eb2aa 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs @@ -36,8 +36,10 @@ namespace Volo.Abp.BackgroundJobs Logger = NullLogger.Instance; } - public void Execute(BackgroundJobInfo jobInfo) + public virtual void Execute(BackgroundJobInfo jobInfo) { + //TODO: Refactor (split to multiple methods). + try { jobInfo.TryCount++; @@ -53,8 +55,6 @@ namespace Volo.Abp.BackgroundJobs throw new AbpException("The job type is not registered to DI: " + jobType); } - //TODO: Type check for the job object - var jobExecuteMethod = job.GetType().GetMethod("Execute"); Debug.Assert(jobExecuteMethod != null, nameof(jobExecuteMethod) + " != null"); var argsType = jobExecuteMethod.GetParameters()[0].ParameterType; @@ -69,7 +69,7 @@ namespace Volo.Abp.BackgroundJobs { Logger.LogException(ex); - var nextTryTime = jobInfo.CalculateNextTryTime(Clock); + var nextTryTime = CalculateNextTryTime(jobInfo); if (nextTryTime.HasValue) { jobInfo.NextTryTime = nextTryTime.Value; @@ -104,7 +104,7 @@ namespace Volo.Abp.BackgroundJobs } } - private void TryUpdate(BackgroundJobInfo jobInfo) + protected virtual void TryUpdate(BackgroundJobInfo jobInfo) { try { @@ -115,5 +115,20 @@ namespace Volo.Abp.BackgroundJobs Logger.LogException(updateEx); } } + + protected virtual DateTime? CalculateNextTryTime(BackgroundJobInfo jobInfo) //TODO: Move to another place to override easier + { + var nextWaitDuration = Options.DefaultFirstWaitDuration * (Math.Pow(Options.DefaultWaitFactor, jobInfo.TryCount - 1)); + var nextTryDate = jobInfo.LastTryTime.HasValue + ? jobInfo.LastTryTime.Value.AddSeconds(nextWaitDuration) + : Clock.Now.AddSeconds(nextWaitDuration); + + if (nextTryDate.Subtract(jobInfo.CreationTime).TotalSeconds > Options.DefaultTimeout) + { + return null; + } + + return nextTryDate; + } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobInfo.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobInfo.cs index 08cbea12e5..a529ea2ecd 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobInfo.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobInfo.cs @@ -9,25 +9,6 @@ namespace Volo.Abp.BackgroundJobs /// public class BackgroundJobInfo { - /// - /// Default duration (as seconds) for the first wait on a failure. - /// Default value: 60 (1 minutes). - /// - public static int DefaultFirstWaitDuration { get; set; } //TODO: Move to configuration - - /// - /// Default timeout value (as seconds) for a job before it's abandoned (). - /// Default value: 172,800 (2 days). - /// - public static int DefaultTimeout { get; set; } //TODO: Move to configuration - - /// - /// Default wait factor for execution failures. - /// This amount is multiplated by last wait time to calculate next wait time. - /// Default value: 2.0. - /// - public static double DefaultWaitFactor { get; set; } //TODO: Move to configuration - public Guid Id { get; set; } /// @@ -72,13 +53,6 @@ namespace Volo.Abp.BackgroundJobs /// public virtual BackgroundJobPriority Priority { get; set; } - static BackgroundJobInfo() - { - DefaultFirstWaitDuration = 60; - DefaultTimeout = 172800; - DefaultWaitFactor = 2.0; - } - /// /// Initializes a new instance of the class. /// @@ -86,25 +60,5 @@ namespace Volo.Abp.BackgroundJobs { Priority = BackgroundJobPriority.Normal; } - - /// - /// Calculates next try time if a job fails. - /// Returns null if it will not wait anymore and job should be abandoned. - /// - /// - public virtual DateTime? CalculateNextTryTime(IClock clock) //TODO: Move to another place to override easier - { - var nextWaitDuration = DefaultFirstWaitDuration * (Math.Pow(DefaultWaitFactor, TryCount - 1)); - var nextTryDate = LastTryTime.HasValue - ? LastTryTime.Value.AddSeconds(nextWaitDuration) - : clock.Now.AddSeconds(nextWaitDuration); - - if (nextTryDate.Subtract(CreationTime).TotalSeconds > DefaultTimeout) - { - return null; - } - - return nextTryDate; - } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobOptions.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobOptions.cs index 5cf3e622a0..4bcfeb56f8 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobOptions.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobOptions.cs @@ -9,7 +9,7 @@ namespace Volo.Abp.BackgroundJobs public class BackgroundJobOptions { public Dictionary JobTypes { get; } - + /// /// Default: true. /// @@ -26,9 +26,32 @@ namespace Volo.Abp.BackgroundJobs /// public int MaxJobFetchCount { get; set; } = 1000; + /// + /// Default duration (as seconds) for the first wait on a failure. + /// Default value: 60 (1 minutes). + /// + public int DefaultFirstWaitDuration { get; set; } + + /// + /// Default timeout value (as seconds) for a job before it's abandoned (). + /// Default value: 172,800 (2 days). + /// + public int DefaultTimeout { get; set; } + + /// + /// Default wait factor for execution failures. + /// This amount is multiplated by last wait time to calculate next wait time. + /// Default value: 2.0. + /// + public double DefaultWaitFactor { get; set; } + public BackgroundJobOptions() { JobTypes = new Dictionary(); + + DefaultFirstWaitDuration = 60; + DefaultTimeout = 172800; + DefaultWaitFactor = 2.0; } internal Type GetJobType(string jobName) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs index 1d4e355892..42afbb5183 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -137,10 +137,6 @@ namespace Volo.Abp.EntityFrameworkCore { entityChangeList = EntityHistoryHelper.CreateChangeList(ChangeTracker.Entries().ToList()); } - else - { - Logger.LogWarning("AuditingManager?.Current is null!"); - } var changeReport = ApplyAbpConcepts(); diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs index 3dfc61f607..1a80ab9887 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs @@ -38,7 +38,10 @@ namespace Volo.Abp.BackgroundJobs.DemoApp context.Services.Configure(options => { + //Configure for fast running options.JobPollPeriod = 1000; + options.DefaultFirstWaitDuration = 1; + options.DefaultWaitFactor = 1; }); context.Services.AddAssemblyOf(); diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleGreenJob.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleGreenJob.cs index 7d2ad86372..9930ce7d9e 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleGreenJob.cs +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleGreenJob.cs @@ -7,14 +7,18 @@ namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs { public override void Execute(WriteToConsoleGreenJobArgs args) { - if (RandomHelper.GetRandomOf(1, 2) == 2) + if (RandomHelper.GetRandom(0,100) < 70) { throw new ApplicationException("A sample exception from the WriteToConsoleGreenJob!"); } var oldColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Green; + + Console.WriteLine(); Console.WriteLine($"############### WriteToConsoleGreenJob: {args.Value} ###############"); + Console.WriteLine(); + Console.ForegroundColor = oldColor; } } diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleYellowJob.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleYellowJob.cs index 514b5b3892..3532e1a3c7 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleYellowJob.cs +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleYellowJob.cs @@ -7,14 +7,18 @@ namespace Volo.Abp.BackgroundJobs.DemoApp.Jobs { public override void Execute(WriteToConsoleYellowJobArgs args) { - if (RandomHelper.GetRandomOf(1, 2) == 2) + if (RandomHelper.GetRandom(0, 100) < 70) { throw new ApplicationException("A sample exception from the WriteToConsoleYellowJob!"); } var oldColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Yellow; + + Console.WriteLine(); Console.WriteLine($"############### WriteToConsoleYellowJob: {args.Value} ###############"); + Console.WriteLine(); + Console.ForegroundColor = oldColor; } }