Browse Source

Move static values to configurable options.

pull/395/head
Halil ibrahim Kalkan 8 years ago
parent
commit
14e55acd15
  1. 25
      framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs
  2. 46
      framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobInfo.cs
  3. 25
      framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobOptions.cs
  4. 4
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
  5. 3
      modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs
  6. 6
      modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleGreenJob.cs
  7. 6
      modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Jobs/WriteToConsoleYellowJob.cs

25
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobExecuter.cs

@ -36,8 +36,10 @@ namespace Volo.Abp.BackgroundJobs
Logger = NullLogger<BackgroundJobExecuter>.Instance; Logger = NullLogger<BackgroundJobExecuter>.Instance;
} }
public void Execute(BackgroundJobInfo jobInfo) public virtual void Execute(BackgroundJobInfo jobInfo)
{ {
//TODO: Refactor (split to multiple methods).
try try
{ {
jobInfo.TryCount++; jobInfo.TryCount++;
@ -53,8 +55,6 @@ namespace Volo.Abp.BackgroundJobs
throw new AbpException("The job type is not registered to DI: " + jobType); 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"); var jobExecuteMethod = job.GetType().GetMethod("Execute");
Debug.Assert(jobExecuteMethod != null, nameof(jobExecuteMethod) + " != null"); Debug.Assert(jobExecuteMethod != null, nameof(jobExecuteMethod) + " != null");
var argsType = jobExecuteMethod.GetParameters()[0].ParameterType; var argsType = jobExecuteMethod.GetParameters()[0].ParameterType;
@ -69,7 +69,7 @@ namespace Volo.Abp.BackgroundJobs
{ {
Logger.LogException(ex); Logger.LogException(ex);
var nextTryTime = jobInfo.CalculateNextTryTime(Clock); var nextTryTime = CalculateNextTryTime(jobInfo);
if (nextTryTime.HasValue) if (nextTryTime.HasValue)
{ {
jobInfo.NextTryTime = nextTryTime.Value; jobInfo.NextTryTime = nextTryTime.Value;
@ -104,7 +104,7 @@ namespace Volo.Abp.BackgroundJobs
} }
} }
private void TryUpdate(BackgroundJobInfo jobInfo) protected virtual void TryUpdate(BackgroundJobInfo jobInfo)
{ {
try try
{ {
@ -115,5 +115,20 @@ namespace Volo.Abp.BackgroundJobs
Logger.LogException(updateEx); 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;
}
} }
} }

46
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobInfo.cs

@ -9,25 +9,6 @@ namespace Volo.Abp.BackgroundJobs
/// </summary> /// </summary>
public class BackgroundJobInfo public class BackgroundJobInfo
{ {
/// <summary>
/// Default duration (as seconds) for the first wait on a failure.
/// Default value: 60 (1 minutes).
/// </summary>
public static int DefaultFirstWaitDuration { get; set; } //TODO: Move to configuration
/// <summary>
/// Default timeout value (as seconds) for a job before it's abandoned (<see cref="IsAbandoned"/>).
/// Default value: 172,800 (2 days).
/// </summary>
public static int DefaultTimeout { get; set; } //TODO: Move to configuration
/// <summary>
/// Default wait factor for execution failures.
/// This amount is multiplated by last wait time to calculate next wait time.
/// Default value: 2.0.
/// </summary>
public static double DefaultWaitFactor { get; set; } //TODO: Move to configuration
public Guid Id { get; set; } public Guid Id { get; set; }
/// <summary> /// <summary>
@ -72,13 +53,6 @@ namespace Volo.Abp.BackgroundJobs
/// </summary> /// </summary>
public virtual BackgroundJobPriority Priority { get; set; } public virtual BackgroundJobPriority Priority { get; set; }
static BackgroundJobInfo()
{
DefaultFirstWaitDuration = 60;
DefaultTimeout = 172800;
DefaultWaitFactor = 2.0;
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="BackgroundJobInfo"/> class. /// Initializes a new instance of the <see cref="BackgroundJobInfo"/> class.
/// </summary> /// </summary>
@ -86,25 +60,5 @@ namespace Volo.Abp.BackgroundJobs
{ {
Priority = BackgroundJobPriority.Normal; Priority = BackgroundJobPriority.Normal;
} }
/// <summary>
/// Calculates next try time if a job fails.
/// Returns null if it will not wait anymore and job should be abandoned.
/// </summary>
/// <returns></returns>
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;
}
} }
} }

25
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobOptions.cs

@ -9,7 +9,7 @@ namespace Volo.Abp.BackgroundJobs
public class BackgroundJobOptions public class BackgroundJobOptions
{ {
public Dictionary<string, Type> JobTypes { get; } public Dictionary<string, Type> JobTypes { get; }
/// <summary> /// <summary>
/// Default: true. /// Default: true.
/// </summary> /// </summary>
@ -26,9 +26,32 @@ namespace Volo.Abp.BackgroundJobs
/// </summary> /// </summary>
public int MaxJobFetchCount { get; set; } = 1000; public int MaxJobFetchCount { get; set; } = 1000;
/// <summary>
/// Default duration (as seconds) for the first wait on a failure.
/// Default value: 60 (1 minutes).
/// </summary>
public int DefaultFirstWaitDuration { get; set; }
/// <summary>
/// Default timeout value (as seconds) for a job before it's abandoned (<see cref="BackgroundJobInfo.IsAbandoned"/>).
/// Default value: 172,800 (2 days).
/// </summary>
public int DefaultTimeout { get; set; }
/// <summary>
/// Default wait factor for execution failures.
/// This amount is multiplated by last wait time to calculate next wait time.
/// Default value: 2.0.
/// </summary>
public double DefaultWaitFactor { get; set; }
public BackgroundJobOptions() public BackgroundJobOptions()
{ {
JobTypes = new Dictionary<string, Type>(); JobTypes = new Dictionary<string, Type>();
DefaultFirstWaitDuration = 60;
DefaultTimeout = 172800;
DefaultWaitFactor = 2.0;
} }
internal Type GetJobType(string jobName) internal Type GetJobType(string jobName)

4
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs

@ -137,10 +137,6 @@ namespace Volo.Abp.EntityFrameworkCore
{ {
entityChangeList = EntityHistoryHelper.CreateChangeList(ChangeTracker.Entries().ToList()); entityChangeList = EntityHistoryHelper.CreateChangeList(ChangeTracker.Entries().ToList());
} }
else
{
Logger.LogWarning("AuditingManager?.Current is null!");
}
var changeReport = ApplyAbpConcepts(); var changeReport = ApplyAbpConcepts();

3
modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs

@ -38,7 +38,10 @@ namespace Volo.Abp.BackgroundJobs.DemoApp
context.Services.Configure<BackgroundJobOptions>(options => context.Services.Configure<BackgroundJobOptions>(options =>
{ {
//Configure for fast running
options.JobPollPeriod = 1000; options.JobPollPeriod = 1000;
options.DefaultFirstWaitDuration = 1;
options.DefaultWaitFactor = 1;
}); });
context.Services.AddAssemblyOf<DemoAppModule>(); context.Services.AddAssemblyOf<DemoAppModule>();

6
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) 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!"); throw new ApplicationException("A sample exception from the WriteToConsoleGreenJob!");
} }
var oldColor = Console.ForegroundColor; var oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine($"############### WriteToConsoleGreenJob: {args.Value} ###############"); Console.WriteLine($"############### WriteToConsoleGreenJob: {args.Value} ###############");
Console.WriteLine();
Console.ForegroundColor = oldColor; Console.ForegroundColor = oldColor;
} }
} }

6
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) 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!"); throw new ApplicationException("A sample exception from the WriteToConsoleYellowJob!");
} }
var oldColor = Console.ForegroundColor; var oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine();
Console.WriteLine($"############### WriteToConsoleYellowJob: {args.Value} ###############"); Console.WriteLine($"############### WriteToConsoleYellowJob: {args.Value} ###############");
Console.WriteLine();
Console.ForegroundColor = oldColor; Console.ForegroundColor = oldColor;
} }
} }

Loading…
Cancel
Save