Move static values to configurable options.

pull/395/head
Halil ibrahim Kalkan 7 years ago
parent b4212f2861
commit 14e55acd15

@ -36,8 +36,10 @@ namespace Volo.Abp.BackgroundJobs
Logger = NullLogger<BackgroundJobExecuter>.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;
}
}
}

@ -9,25 +9,6 @@ namespace Volo.Abp.BackgroundJobs
/// </summary>
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; }
/// <summary>
@ -72,13 +53,6 @@ namespace Volo.Abp.BackgroundJobs
/// </summary>
public virtual BackgroundJobPriority Priority { get; set; }
static BackgroundJobInfo()
{
DefaultFirstWaitDuration = 60;
DefaultTimeout = 172800;
DefaultWaitFactor = 2.0;
}
/// <summary>
/// Initializes a new instance of the <see cref="BackgroundJobInfo"/> class.
/// </summary>
@ -86,25 +60,5 @@ namespace Volo.Abp.BackgroundJobs
{
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;
}
}
}

@ -9,7 +9,7 @@ namespace Volo.Abp.BackgroundJobs
public class BackgroundJobOptions
{
public Dictionary<string, Type> JobTypes { get; }
/// <summary>
/// Default: true.
/// </summary>
@ -26,9 +26,32 @@ namespace Volo.Abp.BackgroundJobs
/// </summary>
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()
{
JobTypes = new Dictionary<string, Type>();
DefaultFirstWaitDuration = 60;
DefaultTimeout = 172800;
DefaultWaitFactor = 2.0;
}
internal Type GetJobType(string jobName)

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

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

@ -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;
}
}

@ -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;
}
}

Loading…
Cancel
Save