Skip to content

Commit 484f1eb

Browse files
authored
Merge pull request #167 from Bonnie2980/master
fix issue: Manual Job not be added into Scheduler
2 parents c1e89ca + 642a440 commit 484f1eb

File tree

8 files changed

+46
-41
lines changed

8 files changed

+46
-41
lines changed

sample/Jobs/HelloJobAuto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace SilkierQuartz.Example.Jobs
99
{
1010

11-
[SilkierQuartz(5, Group = "example", Desciption = "this e sq test", TriggerDescription = "_hellojobauto", TriggerGroup = "SilkierQuartz")]
11+
[SilkierQuartz(5, Group = "example", Description = "this e sq test", TriggerDescription = "_hellojobauto", TriggerGroup = "SilkierQuartz")]
1212
public class HelloJobAuto : IJob
1313
{
1414
public Task Execute(IJobExecutionContext context)

sample/Startup.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ public void ConfigureServices(IServiceCollection services)
6161
services.AddOptions();
6262
services.Configure<AppSettings>(Configuration);
6363
services.Configure<InjectProperty>(options => { options.WriteText = "This is inject string"; });
64-
services.AddQuartzJob<HelloJob>(nameof(HelloJob), "example")
65-
.AddQuartzJob<InjectSampleJob>(nameof(InjectSampleJob), "example")
66-
.AddQuartzJob<HelloJobSingle>(nameof(HelloJobSingle), "example")
67-
.AddQuartzJob<InjectSampleJobSingle>(nameof(InjectSampleJobSingle), "example")
68-
.AddQuartzJob<LongRunningJob>(nameof(LongRunningJob), "example");
64+
services.AddQuartzJob<HelloJob>(nameof(HelloJob))
65+
.AddQuartzJob<InjectSampleJob>(nameof(InjectSampleJob))
66+
.AddQuartzJob<HelloJobSingle>(nameof(HelloJobSingle))
67+
.AddQuartzJob<InjectSampleJobSingle>(nameof(InjectSampleJobSingle))
68+
.AddQuartzJob<LongRunningJob>(nameof(LongRunningJob));
6969
}
7070

7171
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

sample2/Jobs/HelloJobAuto.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace SilkierQuartz.Example.Jobs
99
{
10-
[SilkierQuartz(5, Group = "sample", Desciption = "this e sq test", TriggerDescription = "_hellojobauto")]
10+
[SilkierQuartz(5, Group = "sample", Description = "this e sq test", TriggerDescription = "_hellojobauto")]
1111
public class HelloJobAuto : IJob
1212
{
1313
public Task Execute(IJobExecutionContext context)
@@ -18,7 +18,7 @@ public Task Execute(IJobExecutionContext context)
1818
}
1919

2020
[DisallowConcurrentExecution]
21-
[SilkierQuartz(5, 0, 0, Group = "sample", Desciption = "自动下载欢迎信息")]
21+
[SilkierQuartz(5, 0, 0, Group = "sample", Description = "自动下载欢迎信息")]
2222
public class HelloJobAuto1 : IJob
2323
{
2424
public Task Execute(IJobExecutionContext context)

sample2/Program.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@
5757
services.AddOptions();
5858
services.Configure<AppSettings>(configuration);
5959
services.Configure<InjectProperty>(options => { options.WriteText = "This is inject string"; });
60-
services.AddQuartzJob<HelloJob>(nameof(HelloJob), "sample")
61-
.AddQuartzJob<InjectSampleJob>(nameof(InjectSampleJob), "sample")
62-
.AddQuartzJob<HelloJobSingle>(nameof(HelloJobSingle), "sample")
63-
.AddQuartzJob<InjectSampleJobSingle>(nameof(InjectSampleJobSingle), "sample")
64-
.AddQuartzJob<LongRunningJob>(nameof(LongRunningJob), "sample");
60+
services.AddQuartzJob<HelloJob>(nameof(HelloJob))
61+
.AddQuartzJob<InjectSampleJob>(nameof(InjectSampleJob))
62+
.AddQuartzJob<HelloJobSingle>(nameof(HelloJobSingle))
63+
.AddQuartzJob<InjectSampleJobSingle>(nameof(InjectSampleJobSingle))
64+
.AddQuartzJob<LongRunningJob>(nameof(LongRunningJob));
6565

6666

6767
var app = builder.Build();

src/SilkierQuartz/Configuration/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static IServiceCollection AddSilkierQuartz(
5959
types.ForEach(t =>
6060
{
6161
var so = t.GetCustomAttribute<SilkierQuartzAttribute>();
62-
services.AddQuartzJob(t, so.Identity ?? t.Name, so.Group, so.Desciption ?? t.FullName);
62+
services.AddQuartzJob(t, so.Identity ?? t.Name, so.Manual ? true : false, so.Group, so.Description ?? t.FullName);
6363
});
6464
return services;
6565
}

src/SilkierQuartz/HostedService/IJobRegistratorExtensions.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,18 @@ public static IServiceCollection AddQuartzJobDetail(this IServiceCollection serv
3535
return services;
3636
}
3737

38-
public static IServiceCollection AddQuartzJob<TJob>(this IServiceCollection services, string identity, string? group) where TJob : class
38+
public static IServiceCollection AddQuartzJob<TJob>(this IServiceCollection services, string identity, bool durability = false, string? group = null, string? description = null) where TJob : class
3939
{
40-
return services.AddQuartzJob<TJob>(identity, group, null);
40+
return services.AddQuartzJob(typeof(TJob), identity, durability, group, description);
4141
}
4242

43-
public static IServiceCollection AddQuartzJob<TJob>(this IServiceCollection services, string identity, string? group, string description) where TJob : class
44-
{
45-
return services.AddQuartzJob(typeof(TJob), identity, group, description);
46-
}
47-
48-
public static IServiceCollection AddQuartzJob(this IServiceCollection services, Type t, string identity, string? group, string description)
43+
public static IServiceCollection AddQuartzJob(this IServiceCollection services, Type t, string identity, bool durability = false, string ? group = null, string? description = null)
4944
{
5045
if (!services.Any(sd => sd.ServiceType == t))
5146
{
5247
services.AddTransient(t);
5348
}
54-
var jobDetail = JobBuilder.Create(t).WithIdentity(identity, group).WithDescription(description).Build();
49+
var jobDetail = JobBuilder.Create(t).StoreDurably(durability).WithIdentity(identity, group).WithDescription(description).Build();
5550
services.AddSingleton<IScheduleJob>(provider => new ScheduleJob(jobDetail, new List<ITrigger>()));
5651
return services;
5752
}

src/SilkierQuartz/HostedService/QuartzHostedService.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,32 @@ public async Task StartAsync(CancellationToken cancellationToken)
4343

4444
foreach (var scheduleJob in _scheduleJobs)
4545
{
46-
var isNewJob = true;
47-
foreach (var trigger in scheduleJob.Triggers)
46+
if (scheduleJob.Triggers != null && scheduleJob.Triggers.Any())
4847
{
49-
50-
if (isNewJob)
48+
var isNewJob = true;
49+
foreach (var trigger in scheduleJob.Triggers)
5150
{
52-
if (!(await _scheduler.CheckExists(scheduleJob.JobDetail.Key, cancellationToken) ))
51+
52+
if (isNewJob)
5353
{
54-
await _scheduler.ScheduleJob(scheduleJob.JobDetail, trigger, cancellationToken);
54+
if (!(await _scheduler.CheckExists(scheduleJob.JobDetail.Key, cancellationToken)))
55+
{
56+
await _scheduler.ScheduleJob(scheduleJob.JobDetail, trigger, cancellationToken);
57+
}
5558
}
56-
}
57-
else
58-
{
59-
if (!( await _scheduler.CheckExists(trigger.Key, cancellationToken)))
59+
else
6060
{
61-
await _scheduler.ScheduleJob(trigger, cancellationToken);
61+
if (!(await _scheduler.CheckExists(trigger.Key, cancellationToken)))
62+
{
63+
await _scheduler.ScheduleJob(trigger, cancellationToken);
64+
}
6265
}
66+
isNewJob = false;
6367
}
64-
isNewJob = false;
68+
}
69+
else
70+
{
71+
await _scheduler.AddJob(scheduleJob.JobDetail, true, cancellationToken);
6572
}
6673
}
6774
}

src/SilkierQuartz/SilkierQuartzAttribute.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ public SilkierQuartzAttribute()
1111
{
1212
}
1313

14-
public SilkierQuartzAttribute(double days, double hours, double minutes, double seconds, double milliseconds, string _identity, string? _group, string _desciption) : this(days, hours, minutes, seconds, milliseconds, 0, _identity, _group, _desciption)
14+
public SilkierQuartzAttribute(double days, double hours, double minutes, double seconds, double milliseconds, string _identity, string? _group = null, string _description = null) : this(days, hours, minutes, seconds, milliseconds, 0, _identity, _group, _description)
1515
{
1616
}
1717

18-
public SilkierQuartzAttribute(double hours, double minutes, double seconds, string _identity, string? _group, string _desciption) : this(0, hours, minutes, seconds, 0, 0, _identity, _group, _desciption)
18+
public SilkierQuartzAttribute(double hours, double minutes, double seconds, string _identity, string? _group = null, string _description = null) : this(0, hours, minutes, seconds, 0, 0, _identity, _group, _description)
1919
{
2020
}
2121

22-
public SilkierQuartzAttribute(double minutes, double seconds, string _identity, string? _group, string _desciption) : this(0, 0, minutes, seconds, 0, 0, _identity, _group, _desciption)
22+
public SilkierQuartzAttribute(double minutes, double seconds, string _identity, string? _group = null, string _description = null) : this(0, 0, minutes, seconds, 0, 0, _identity, _group, _description)
2323
{
2424
}
2525

26-
public SilkierQuartzAttribute(double seconds, string _identity, string? _group, string _desciption) : this(0, 0, 0, seconds, 0, 0, _identity, _group, _desciption)
26+
public SilkierQuartzAttribute(double seconds, string _identity, string? _group = null, string _description = null) : this(0, 0, 0, seconds, 0, 0, _identity, _group, _description)
2727
{
2828
}
2929

@@ -48,8 +48,11 @@ public SilkierQuartzAttribute(bool Manual) : this(0, 0, 0, 0, 0, 0, null, null,
4848
this.Manual = true;
4949
}
5050

51-
public SilkierQuartzAttribute(double days, double hours, double minutes, double seconds, double milliseconds, long ticks, string _identity, string? _group, string _desciption)
51+
public SilkierQuartzAttribute(double days, double hours, double minutes, double seconds, double milliseconds, long ticks, string _identity, string? _group, string _description)
5252
{
53+
Identity = _identity;
54+
Group = _group;
55+
Description = _description;
5356

5457
WithInterval = TimeSpan.FromTicks(ticks + (long)(days * TimeSpan.TicksPerDay
5558
+ hours * TimeSpan.TicksPerHour
@@ -58,7 +61,7 @@ public SilkierQuartzAttribute(double days, double hours, double minutes, double
5861
+ milliseconds + TimeSpan.TicksPerMillisecond));
5962
}
6063

61-
public string Desciption { get; set; } = null;
64+
public string Description { get; set; } = null;
6265
public string Identity { get; set; } = null;
6366
public string Group { get; set; } = null;
6467
public TimeSpan WithInterval { get; set; }

0 commit comments

Comments
 (0)