Skip to content

Commit 69cb2f8

Browse files
committed
add createasync in scheduleclient
1 parent 2e0917c commit 69cb2f8

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/ScheduledTasks/Client/ScheduleClient.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ protected ScheduleClient(string scheduleId)
2222
/// </summary>
2323
public string ScheduleId { get; }
2424

25+
/// <summary>
26+
/// Creates or update a schedule with the specified configuration.
27+
/// </summary>
28+
/// <param name="creationOptions">The options for creating the schedule.</param>
29+
/// <param name="cancellation">The cancellation token that can be used to cancel the operation.</param>
30+
/// <returns>A task that completes when the schedule has been created.</returns>
31+
public abstract Task CreateAsync(ScheduleCreationOptions creationOptions, CancellationToken cancellation = default);
32+
2533
/// <summary>
2634
/// Retrieves the current details of this schedule.
2735
/// </summary>

src/ScheduledTasks/Client/ScheduleClientImpl.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,43 @@ public ScheduleClientImpl(DurableTaskClient client, string scheduleId, ILogger l
3636
/// </summary>
3737
EntityInstanceId EntityId { get; }
3838

39+
/// <inheritdoc/>
40+
public override async Task CreateAsync(ScheduleCreationOptions creationOptions, CancellationToken cancellation = default)
41+
{
42+
try
43+
{
44+
Check.NotNull(creationOptions, nameof(creationOptions));
45+
46+
ScheduleOperationRequest request = new ScheduleOperationRequest(this.EntityId, nameof(Schedule.CreateSchedule), creationOptions);
47+
string instanceId = await this.durableTaskClient.ScheduleNewOrchestrationInstanceAsync(
48+
new TaskName(nameof(ExecuteScheduleOperationOrchestrator)),
49+
request,
50+
cancellation);
51+
52+
// Wait for the orchestration to complete
53+
OrchestrationMetadata state = await this.durableTaskClient.WaitForInstanceCompletionAsync(instanceId, true, cancellation);
54+
55+
if (state.RuntimeStatus != OrchestrationRuntimeStatus.Completed)
56+
{
57+
throw new InvalidOperationException($"Failed to create schedule '{this.ScheduleId}': {state.FailureDetails?.ErrorMessage ?? string.Empty}");
58+
}
59+
}
60+
catch (OperationCanceledException) when (cancellation.IsCancellationRequested)
61+
{
62+
// the operation was cancelled as requested. No need to log this.
63+
throw;
64+
}
65+
catch (Exception ex)
66+
{
67+
this.logger.ClientError(
68+
nameof(this.CreateAsync),
69+
this.ScheduleId,
70+
ex);
71+
72+
throw;
73+
}
74+
}
75+
3976
/// <inheritdoc/>
4077
public override async Task<ScheduleDescription> DescribeAsync(CancellationToken cancellation = default)
4178
{

0 commit comments

Comments
 (0)