Skip to content

fix issue: Manual Job not be added into Scheduler #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sample/Jobs/HelloJobAuto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace SilkierQuartz.Example.Jobs
{

[SilkierQuartz(5, Group = "example", Desciption = "this e sq test", TriggerDescription = "_hellojobauto", TriggerGroup = "SilkierQuartz")]
[SilkierQuartz(5, Group = "example", Description = "this e sq test", TriggerDescription = "_hellojobauto", TriggerGroup = "SilkierQuartz")]
public class HelloJobAuto : IJob
{
public Task Execute(IJobExecutionContext context)
Expand Down
10 changes: 5 additions & 5 deletions sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public void ConfigureServices(IServiceCollection services)
services.AddOptions();
services.Configure<AppSettings>(Configuration);
services.Configure<InjectProperty>(options => { options.WriteText = "This is inject string"; });
services.AddQuartzJob<HelloJob>(nameof(HelloJob), "example")
.AddQuartzJob<InjectSampleJob>(nameof(InjectSampleJob), "example")
.AddQuartzJob<HelloJobSingle>(nameof(HelloJobSingle), "example")
.AddQuartzJob<InjectSampleJobSingle>(nameof(InjectSampleJobSingle), "example")
.AddQuartzJob<LongRunningJob>(nameof(LongRunningJob), "example");
services.AddQuartzJob<HelloJob>(nameof(HelloJob))
.AddQuartzJob<InjectSampleJob>(nameof(InjectSampleJob))
.AddQuartzJob<HelloJobSingle>(nameof(HelloJobSingle))
.AddQuartzJob<InjectSampleJobSingle>(nameof(InjectSampleJobSingle))
.AddQuartzJob<LongRunningJob>(nameof(LongRunningJob));
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
4 changes: 2 additions & 2 deletions sample2/Jobs/HelloJobAuto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace SilkierQuartz.Example.Jobs
{
[SilkierQuartz(5, Group = "sample", Desciption = "this e sq test", TriggerDescription = "_hellojobauto")]
[SilkierQuartz(5, Group = "sample", Description = "this e sq test", TriggerDescription = "_hellojobauto")]
public class HelloJobAuto : IJob
{
public Task Execute(IJobExecutionContext context)
Expand All @@ -18,7 +18,7 @@ public Task Execute(IJobExecutionContext context)
}

[DisallowConcurrentExecution]
[SilkierQuartz(5, 0, 0, Group = "sample", Desciption = "自动下载欢迎信息")]
[SilkierQuartz(5, 0, 0, Group = "sample", Description = "自动下载欢迎信息")]
public class HelloJobAuto1 : IJob
{
public Task Execute(IJobExecutionContext context)
Expand Down
10 changes: 5 additions & 5 deletions sample2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@
services.AddOptions();
services.Configure<AppSettings>(configuration);
services.Configure<InjectProperty>(options => { options.WriteText = "This is inject string"; });
services.AddQuartzJob<HelloJob>(nameof(HelloJob), "sample")
.AddQuartzJob<InjectSampleJob>(nameof(InjectSampleJob), "sample")
.AddQuartzJob<HelloJobSingle>(nameof(HelloJobSingle), "sample")
.AddQuartzJob<InjectSampleJobSingle>(nameof(InjectSampleJobSingle), "sample")
.AddQuartzJob<LongRunningJob>(nameof(LongRunningJob), "sample");
services.AddQuartzJob<HelloJob>(nameof(HelloJob))
.AddQuartzJob<InjectSampleJob>(nameof(InjectSampleJob))
.AddQuartzJob<HelloJobSingle>(nameof(HelloJobSingle))
.AddQuartzJob<InjectSampleJobSingle>(nameof(InjectSampleJobSingle))
.AddQuartzJob<LongRunningJob>(nameof(LongRunningJob));


var app = builder.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static IServiceCollection AddSilkierQuartz(
types.ForEach(t =>
{
var so = t.GetCustomAttribute<SilkierQuartzAttribute>();
services.AddQuartzJob(t, so.Identity ?? t.Name, so.Group, so.Desciption ?? t.FullName);
services.AddQuartzJob(t, so.Identity ?? t.Name, so.Manual ? true : false, so.Group, so.Description ?? t.FullName);
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider simplifying the boolean expression 'so.Manual ? true : false' to 'so.Manual' for improved readability.

Suggested change
services.AddQuartzJob(t, so.Identity ?? t.Name, so.Manual ? true : false, so.Group, so.Description ?? t.FullName);
services.AddQuartzJob(t, so.Identity ?? t.Name, so.Manual, so.Group, so.Description ?? t.FullName);

Copilot uses AI. Check for mistakes.

});
return services;
}
Expand Down
13 changes: 4 additions & 9 deletions src/SilkierQuartz/HostedService/IJobRegistratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,18 @@
return services;
}

public static IServiceCollection AddQuartzJob<TJob>(this IServiceCollection services, string identity, string? group) where TJob : class
public static IServiceCollection AddQuartzJob<TJob>(this IServiceCollection services, string identity, bool durability = false, string? group = null, string? description = null) where TJob : class

Check warning on line 38 in src/SilkierQuartz/HostedService/IJobRegistratorExtensions.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 38 in src/SilkierQuartz/HostedService/IJobRegistratorExtensions.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 38 in src/SilkierQuartz/HostedService/IJobRegistratorExtensions.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
return services.AddQuartzJob<TJob>(identity, group, null);
return services.AddQuartzJob(typeof(TJob), identity, durability, group, description);
}

public static IServiceCollection AddQuartzJob<TJob>(this IServiceCollection services, string identity, string? group, string description) where TJob : class
{
return services.AddQuartzJob(typeof(TJob), identity, group, description);
}

public static IServiceCollection AddQuartzJob(this IServiceCollection services, Type t, string identity, string? group, string description)
public static IServiceCollection AddQuartzJob(this IServiceCollection services, Type t, string identity, bool durability = false, string ? group = null, string? description = null)

Check warning on line 43 in src/SilkierQuartz/HostedService/IJobRegistratorExtensions.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 43 in src/SilkierQuartz/HostedService/IJobRegistratorExtensions.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (!services.Any(sd => sd.ServiceType == t))
{
services.AddTransient(t);
}
var jobDetail = JobBuilder.Create(t).WithIdentity(identity, group).WithDescription(description).Build();
var jobDetail = JobBuilder.Create(t).StoreDurably(durability).WithIdentity(identity, group).WithDescription(description).Build();
services.AddSingleton<IScheduleJob>(provider => new ScheduleJob(jobDetail, new List<ITrigger>()));
return services;
}
Expand Down
31 changes: 19 additions & 12 deletions src/SilkierQuartz/HostedService/QuartzHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,32 @@ public async Task StartAsync(CancellationToken cancellationToken)

foreach (var scheduleJob in _scheduleJobs)
{
var isNewJob = true;
foreach (var trigger in scheduleJob.Triggers)
if (scheduleJob.Triggers != null && scheduleJob.Triggers.Any())
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] If possible, ensure that 'scheduleJob.Triggers' is always initialized to avoid needing a null check and simplify the control flow.

Suggested change
if (scheduleJob.Triggers != null && scheduleJob.Triggers.Any())
if (scheduleJob.Triggers.Any())

Copilot uses AI. Check for mistakes.

{

if (isNewJob)
var isNewJob = true;
foreach (var trigger in scheduleJob.Triggers)
{
if (!(await _scheduler.CheckExists(scheduleJob.JobDetail.Key, cancellationToken) ))

if (isNewJob)
{
await _scheduler.ScheduleJob(scheduleJob.JobDetail, trigger, cancellationToken);
if (!(await _scheduler.CheckExists(scheduleJob.JobDetail.Key, cancellationToken)))
{
await _scheduler.ScheduleJob(scheduleJob.JobDetail, trigger, cancellationToken);
}
}
}
else
{
if (!( await _scheduler.CheckExists(trigger.Key, cancellationToken)))
else
{
await _scheduler.ScheduleJob(trigger, cancellationToken);
if (!(await _scheduler.CheckExists(trigger.Key, cancellationToken)))
{
await _scheduler.ScheduleJob(trigger, cancellationToken);
}
}
isNewJob = false;
}
isNewJob = false;
}
else
{
await _scheduler.AddJob(scheduleJob.JobDetail, true, cancellationToken);
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/SilkierQuartz/SilkierQuartzAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
{
}

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)
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)

Check warning on line 14 in src/SilkierQuartz/SilkierQuartzAttribute.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
}

public SilkierQuartzAttribute(double hours, double minutes, double seconds, string _identity, string? _group, string _desciption) : this(0, hours, minutes, seconds, 0, 0, _identity, _group, _desciption)
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)

Check warning on line 18 in src/SilkierQuartz/SilkierQuartzAttribute.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
}

public SilkierQuartzAttribute(double minutes, double seconds, string _identity, string? _group, string _desciption) : this(0, 0, minutes, seconds, 0, 0, _identity, _group, _desciption)
public SilkierQuartzAttribute(double minutes, double seconds, string _identity, string? _group = null, string _description = null) : this(0, 0, minutes, seconds, 0, 0, _identity, _group, _description)

Check warning on line 22 in src/SilkierQuartz/SilkierQuartzAttribute.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
}

public SilkierQuartzAttribute(double seconds, string _identity, string? _group, string _desciption) : this(0, 0, 0, seconds, 0, 0, _identity, _group, _desciption)
public SilkierQuartzAttribute(double seconds, string _identity, string? _group = null, string _description = null) : this(0, 0, 0, seconds, 0, 0, _identity, _group, _description)

Check warning on line 26 in src/SilkierQuartz/SilkierQuartzAttribute.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
}

Expand All @@ -48,8 +48,11 @@
this.Manual = true;
}

public SilkierQuartzAttribute(double days, double hours, double minutes, double seconds, double milliseconds, long ticks, string _identity, string? _group, string _desciption)
public SilkierQuartzAttribute(double days, double hours, double minutes, double seconds, double milliseconds, long ticks, string _identity, string? _group, string _description)

Check warning on line 51 in src/SilkierQuartz/SilkierQuartzAttribute.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
Identity = _identity;
Group = _group;
Description = _description;

WithInterval = TimeSpan.FromTicks(ticks + (long)(days * TimeSpan.TicksPerDay
+ hours * TimeSpan.TicksPerHour
Expand All @@ -58,7 +61,7 @@
+ milliseconds + TimeSpan.TicksPerMillisecond));
}

public string Desciption { get; set; } = null;
public string Description { get; set; } = null;
public string Identity { get; set; } = null;
public string Group { get; set; } = null;
public TimeSpan WithInterval { get; set; }
Expand Down