-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathAbpBackgroundJobOptions.cs
79 lines (63 loc) · 2.35 KB
/
AbpBackgroundJobOptions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
namespace Volo.Abp.BackgroundJobs;
public class AbpBackgroundJobOptions
{
private readonly Dictionary<Type, BackgroundJobConfiguration> _jobConfigurationsByArgsType;
private readonly Dictionary<string, BackgroundJobConfiguration> _jobConfigurationsByName;
/// <summary>
/// Default: true.
/// </summary>
public bool IsJobExecutionEnabled { get; set; } = true;
/// <summary>
/// The delegate to get the name of a background job.
/// Default: <see cref="BackgroundJobNameAttribute.GetName"/>.
/// </summary>
public Func<Type, string> GetBackgroundJobName { get; set; }
public AbpBackgroundJobOptions()
{
_jobConfigurationsByArgsType = new Dictionary<Type, BackgroundJobConfiguration>();
_jobConfigurationsByName = new Dictionary<string, BackgroundJobConfiguration>();
GetBackgroundJobName = BackgroundJobNameAttribute.GetName;
}
public BackgroundJobConfiguration GetJob<TArgs>()
{
return GetJob(typeof(TArgs));
}
public BackgroundJobConfiguration GetJob(Type argsType)
{
var jobConfiguration = _jobConfigurationsByArgsType.GetOrDefault(argsType);
if (jobConfiguration == null)
{
throw new AbpException("Undefined background job for the job args type: " + argsType.AssemblyQualifiedName);
}
return jobConfiguration;
}
public BackgroundJobConfiguration GetJob(string name)
{
var jobConfiguration = _jobConfigurationsByName.GetOrDefault(name);
if (jobConfiguration == null)
{
throw new AbpException("Undefined background job for the job name: " + name);
}
return jobConfiguration;
}
public IReadOnlyList<BackgroundJobConfiguration> GetJobs()
{
return _jobConfigurationsByArgsType.Values.ToImmutableList();
}
public void AddJob<TJob>()
{
AddJob(typeof(TJob));
}
public void AddJob(Type jobType)
{
AddJob(new BackgroundJobConfiguration(jobType, GetBackgroundJobName(jobType)));
}
public void AddJob(BackgroundJobConfiguration jobConfiguration)
{
_jobConfigurationsByArgsType[jobConfiguration.ArgsType] = jobConfiguration;
_jobConfigurationsByName[jobConfiguration.JobName] = jobConfiguration;
}
}