Skip to content

Commit e0af2d3

Browse files
committed
feat!: Move second precision to job
1 parent 82b617c commit e0af2d3

File tree

6 files changed

+52
-18
lines changed

6 files changed

+52
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ All notable changes to **NCronJob** will be documented in this file. The project
66

77
## [Unreleased]
88

9+
### Changed
10+
11+
- Moved `EnableSecondPrecision` from `AddNCronJob` to `AddCronJob` to allow for more granular control over the precision of the cron expression
12+
913
## [0.12.0] - 2024-03-22
1014

1115
### Changed

src/LinkDotNet.NCronJob/JobOption.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ public sealed class JobOption
2424
/// Gets or sets the <see cref="IsolationLevel"/> of the executed job.
2525
/// </summary>
2626
public IsolationLevel IsolationLevel { get; set; }
27+
28+
/// <summary>
29+
/// Determines whether the cron expression can specify second-level precision.
30+
/// </summary>
31+
/// <remarks>
32+
/// When enabled, cron expressions must include a seconds field, allowing for more precise scheduling.
33+
/// By default, this is disabled, and cron expressions are expected to start with the minute field.
34+
/// Enabling this affects scheduling granularity and may influence performance, especially for jobs
35+
/// that are scheduled to run very frequently.
36+
/// </remarks>
37+
public bool EnableSecondPrecision { get; set; }
2738
}
2839

2940
/// <summary>

src/LinkDotNet.NCronJob/NCronJobExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@ private static CrontabSchedule GetCronExpression(IServiceCollection services, Jo
8181
{
8282
using var serviceProvider = services.BuildServiceProvider();
8383

84-
var nCronJobOptions = serviceProvider.GetRequiredService<NCronJobOptions>();
85-
8684
var cronParseOptions = new CrontabSchedule.ParseOptions
8785
{
88-
IncludingSeconds = nCronJobOptions.EnableSecondPrecision
86+
IncludingSeconds = option.EnableSecondPrecision
8987
};
9088

9189
return CrontabSchedule.TryParse(option.CronExpression, cronParseOptions)

src/LinkDotNet.NCronJob/NCronJobOptions.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,4 @@ public sealed class NCronJobOptions
1212
/// The higher the value, the longer it will take to run instant jobs.
1313
/// </remarks>
1414
public TimeSpan TimerInterval { get; set; } = TimeSpan.FromSeconds(1);
15-
16-
/// <summary>
17-
/// Determines whether cron expressions can specify second-level precision.
18-
/// </summary>
19-
/// <remarks>
20-
/// When enabled, cron expressions must include a seconds field, allowing for more precise scheduling.
21-
/// By default, this is disabled, and cron expressions are expected to start with the minute field.
22-
/// Enabling this affects scheduling granularity and may influence performance, especially for jobs
23-
/// that are scheduled to run very frequently.
24-
/// </remarks>
25-
public bool EnableSecondPrecision { get; set; }
2615
}

tests/NCronJob.Tests/ExtensionsTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ public void AddingCronJobWithSecondPrecisionExpressionNotThrowException()
2121
{
2222
var everySecond = "* * * * * *";
2323
var collection = new ServiceCollection();
24-
collection.AddNCronJob(options => options.EnableSecondPrecision = true);
24+
collection.AddNCronJob();
2525

26-
Action act = () => collection.AddCronJob<FakeJob>(o => o.CronExpression = everySecond);
26+
Action act = () => collection.AddCronJob<FakeJob>(o =>
27+
{
28+
o.EnableSecondPrecision = true;
29+
o.CronExpression = everySecond;
30+
});
2731

2832
act.ShouldNotThrow();
2933
}

tests/NCronJob.Tests/NCronJobIntegrationTests.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,12 @@ public async Task CronJobThatIsScheduledEverySecondShouldBeExecuted()
121121
{
122122
var fakeTimer = TimeProviderFactory.GetTimeProvider();
123123
ServiceCollection.AddSingleton<TimeProvider>(fakeTimer);
124-
ServiceCollection.AddNCronJob(p => p.EnableSecondPrecision = true);
125-
ServiceCollection.AddCronJob<SimpleJob>(p => p.CronExpression = "* * * * * *");
124+
ServiceCollection.AddNCronJob();
125+
ServiceCollection.AddCronJob<SimpleJob>(p =>
126+
{
127+
p.EnableSecondPrecision = true;
128+
p.CronExpression = "* * * * * *";
129+
});
126130
await using var provider = ServiceCollection.BuildServiceProvider();
127131

128132
await provider.GetRequiredService<IHostedService>().StartAsync(CancellationToken);
@@ -132,6 +136,30 @@ public async Task CronJobThatIsScheduledEverySecondShouldBeExecuted()
132136
jobFinished.ShouldBeTrue();
133137
}
134138

139+
[Fact]
140+
public async Task CanRunSecondPrecisionAndMinutePrecisionJobs()
141+
{
142+
var fakeTimer = TimeProviderFactory.GetTimeProvider();
143+
ServiceCollection.AddSingleton<TimeProvider>(fakeTimer);
144+
ServiceCollection.AddNCronJob();
145+
ServiceCollection.AddCronJob<SimpleJob>(p =>
146+
{
147+
p.EnableSecondPrecision = true;
148+
p.CronExpression = "* * * * * *";
149+
});
150+
ServiceCollection.AddCronJob<SimpleJob>(p =>
151+
{
152+
p.CronExpression = "* * * * *";
153+
});
154+
await using var provider = ServiceCollection.BuildServiceProvider();
155+
156+
await provider.GetRequiredService<IHostedService>().StartAsync(CancellationToken);
157+
158+
fakeTimer.Advance(TimeSpan.FromSeconds(61));
159+
var jobFinished = await WaitForJobsOrTimeout(61);
160+
jobFinished.ShouldBeTrue();
161+
}
162+
135163
[Fact]
136164
public async Task LongRunningJobShouldNotBlockSchedulerWithIsolationLevelTask()
137165
{

0 commit comments

Comments
 (0)