Skip to content

Commit 47d9af0

Browse files
committed
update scheduleconfig
1 parent 8086607 commit 47d9af0

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

src/ScheduledTasks/Entity/Schedule.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public void ResumeSchedule(TaskEntityContext context)
305305
public void RunSchedule(TaskEntityContext context, string executionToken)
306306
{
307307
ScheduleConfiguration scheduleConfig = Verify.NotNull(this.State.ScheduleConfiguration, nameof(this.State.ScheduleConfiguration));
308-
TimeSpan interval = scheduleConfig.Interval ?? throw new InvalidOperationException("Schedule interval must be specified.");
308+
TimeSpan interval = scheduleConfig.Interval;
309309

310310
if (executionToken != this.State.ExecutionToken)
311311
{
@@ -364,17 +364,12 @@ public void RunSchedule(TaskEntityContext context, string executionToken)
364364

365365
static DateTimeOffset ComputeNextRunTime(ScheduleConfiguration scheduleConfig, DateTimeOffset lastRunAt)
366366
{
367-
if (!scheduleConfig.Interval.HasValue)
368-
{
369-
throw new InvalidOperationException("Interval must be set to compute next run time.");
370-
}
371-
372367
// Calculate number of intervals between last run and now
373368
TimeSpan timeSinceLastRun = DateTimeOffset.UtcNow - lastRunAt;
374-
int intervalsElapsed = (int)(timeSinceLastRun.Ticks / scheduleConfig.Interval.Value.Ticks);
369+
int intervalsElapsed = (int)(timeSinceLastRun.Ticks / scheduleConfig.Interval.Ticks);
375370

376371
// Compute and return the next run time
377-
return lastRunAt + TimeSpan.FromTicks(scheduleConfig.Interval.Value.Ticks * (intervalsElapsed + 1));
372+
return lastRunAt + TimeSpan.FromTicks(scheduleConfig.Interval.Ticks * (intervalsElapsed + 1));
378373
}
379374

380375
void StartOrchestrationIfNotRunning(TaskEntityContext context)

src/ScheduledTasks/Models/ScheduleConfiguration.cs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,46 @@ namespace Microsoft.DurableTask.ScheduledTasks;
99
class ScheduleConfiguration
1010
{
1111
string orchestrationName;
12-
TimeSpan? interval;
12+
TimeSpan interval;
1313

1414
/// <summary>
1515
/// Initializes a new instance of the <see cref="ScheduleConfiguration"/> class.
1616
/// </summary>
17+
/// <param name="scheduleId">The ID of the schedule.</param>
1718
/// <param name="orchestrationName">The name of the orchestration to schedule.</param>
18-
/// <param name="scheduleId">The ID of the schedule, or null to generate one.</param>
19-
public ScheduleConfiguration(string orchestrationName, string scheduleId)
19+
/// <param name="interval">The interval between schedule executions. Must be positive and at least 1 second.</param>
20+
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
21+
public ScheduleConfiguration(string scheduleId, string orchestrationName, TimeSpan interval)
22+
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
2023
{
21-
this.orchestrationName = Check.NotNullOrEmpty(orchestrationName, nameof(orchestrationName));
22-
this.ScheduleId = scheduleId ?? Guid.NewGuid().ToString("N");
24+
this.ScheduleId = Check.NotNullOrEmpty(scheduleId, nameof(scheduleId));
25+
this.OrchestrationName = Check.NotNullOrEmpty(orchestrationName, nameof(orchestrationName));
26+
if (interval <= TimeSpan.Zero)
27+
{
28+
throw new ArgumentException("Interval must be positive", nameof(interval));
29+
}
30+
31+
if (interval.TotalSeconds < 1)
32+
{
33+
throw new ArgumentException("Interval must be at least 1 second", nameof(interval));
34+
}
35+
36+
this.Interval = interval;
2337
}
2438

2539
/// <summary>
26-
/// Gets or sets the name of the orchestration function to schedule.
40+
/// Gets or Sets the name of the orchestration function to schedule.
2741
/// </summary>
2842
public string OrchestrationName
2943
{
3044
get => this.orchestrationName;
31-
set
32-
{
33-
this.orchestrationName = Check.NotNullOrEmpty(value, nameof(value));
34-
}
45+
set => this.orchestrationName = Check.NotNullOrEmpty(value, nameof(this.OrchestrationName));
3546
}
3647

3748
/// <summary>
3849
/// Gets the ID of the schedule.
3950
/// </summary>
40-
public string ScheduleId { get; init; }
51+
public string ScheduleId { get; }
4152

4253
/// <summary>
4354
/// Gets or sets the input to the orchestration function.
@@ -62,22 +73,17 @@ public string OrchestrationName
6273
/// <summary>
6374
/// Gets or sets the interval between schedule executions.
6475
/// </summary>
65-
public TimeSpan? Interval
76+
public TimeSpan Interval
6677
{
6778
get => this.interval;
6879
set
6980
{
70-
if (!value.HasValue)
71-
{
72-
return;
73-
}
74-
75-
if (value.Value <= TimeSpan.Zero)
81+
if (value <= TimeSpan.Zero)
7682
{
7783
throw new ArgumentException("Interval must be positive", nameof(value));
7884
}
7985

80-
if (value.Value.TotalSeconds < 1)
86+
if (value.TotalSeconds < 1)
8187
{
8288
throw new ArgumentException("Interval must be at least 1 second", nameof(value));
8389
}
@@ -98,13 +104,14 @@ public TimeSpan? Interval
98104
/// <returns>A new schedule configuration.</returns>
99105
public static ScheduleConfiguration FromCreateOptions(ScheduleCreationOptions createOptions)
100106
{
101-
return new ScheduleConfiguration(createOptions.OrchestrationName, createOptions.ScheduleId)
107+
Check.NotNull(createOptions, nameof(createOptions));
108+
109+
return new ScheduleConfiguration(createOptions.ScheduleId, createOptions.OrchestrationName, createOptions.Interval)
102110
{
103111
OrchestrationInput = createOptions.OrchestrationInput,
104112
OrchestrationInstanceId = createOptions.OrchestrationInstanceId,
105113
StartAt = createOptions.StartAt,
106114
EndAt = createOptions.EndAt,
107-
Interval = createOptions.Interval,
108115
StartImmediatelyIfLate = createOptions.StartImmediatelyIfLate,
109116
};
110117
}
@@ -151,7 +158,7 @@ public HashSet<string> Update(ScheduleUpdateOptions updateOptions)
151158

152159
if (updateOptions.Interval.HasValue)
153160
{
154-
this.Interval = updateOptions.Interval;
161+
this.Interval = updateOptions.Interval.Value;
155162
updatedFields.Add(nameof(this.Interval));
156163
}
157164

0 commit comments

Comments
 (0)