Skip to content

Commit dd5f9e4

Browse files
committed
save
1 parent 88e6f77 commit dd5f9e4

File tree

4 files changed

+80
-51
lines changed

4 files changed

+80
-51
lines changed

samples/ScheduleDemo/Program.cs

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,60 +11,61 @@
1111

1212
// Create the host builder
1313
IHost host = Host.CreateDefaultBuilder(args)
14-
.ConfigureServices(services =>
14+
.ConfigureServices(services =>
15+
{
16+
string connectionString = Environment.GetEnvironmentVariable("DURABLE_TASK_SCHEDULER_CONNECTION_STRING")
17+
?? throw new InvalidOperationException("Missing required environment variable 'DURABLE_TASK_SCHEDULER_CONNECTION_STRING'");
18+
19+
// Configure the worker
20+
_ = services.AddDurableTaskWorker(builder =>
21+
{
22+
// Add the Schedule entity and demo orchestration
23+
builder.AddTasks(r =>
1524
{
16-
string connectionString = Environment.GetEnvironmentVariable("DURABLE_TASK_SCHEDULER_CONNECTION_STRING")
17-
?? throw new InvalidOperationException("Missing required environment variable 'DURABLE_TASK_SCHEDULER_CONNECTION_STRING'");
18-
19-
// Configure the worker
20-
services.AddDurableTaskWorker(builder =>
25+
// Add a demo orchestration that will be triggered by the schedule
26+
r.AddOrchestratorFunc("DemoOrchestration", async context =>
2127
{
22-
// Add the Schedule entity and demo orchestration
23-
builder.AddTasks(r =>
24-
{
25-
// Add a demo orchestration that will be triggered by the schedule
26-
r.AddOrchestratorFunc("DemoOrchestration", async context =>
27-
{
28-
string input = context.GetInput<string>();
29-
await context.CallActivityAsync("ProcessMessage", input);
30-
return $"Completed processing at {DateTime.UtcNow}";
31-
});
32-
// Add a demo activity
33-
r.AddActivityFunc<string, string>("ProcessMessage", (context, message) => $"Processing message: {message}");
34-
});
35-
36-
// Enable scheduled tasks support
37-
builder.EnableScheduledTasksSupport();
38-
builder.UseDurableTaskScheduler(connectionString);
28+
string? input = context.GetInput<string>();
29+
await context.CallActivityAsync("ProcessMessage", input);
30+
return $"Completed processing at {DateTime.UtcNow}";
3931
});
40-
41-
// Configure the client
42-
services.AddDurableTaskClient(builder =>
43-
{
44-
builder.EnableScheduledTasksSupport();
45-
builder.UseDurableTaskScheduler(connectionString);
46-
});
47-
48-
// Configure console logging
49-
services.AddLogging(logging =>
50-
{
51-
logging.AddSimpleConsole(options =>
52-
{
53-
options.SingleLine = true;
54-
options.UseUtcTimestamp = true;
55-
options.TimestampFormat = "yyyy-MM-ddTHH:mm:ss.fffZ ";
56-
});
57-
});
58-
})
59-
.Build();
32+
// Add a demo activity
33+
r.AddActivityFunc<string, string>("ProcessMessage", (context, message) => $"Processing message: {message}");
34+
});
35+
36+
// Enable scheduled tasks support
37+
builder.EnableScheduledTasksSupport();
38+
builder.UseDurableTaskScheduler(connectionString);
39+
});
40+
41+
// Configure the client
42+
services.AddDurableTaskClient(builder =>
43+
{
44+
builder.UseDurableTaskScheduler(connectionString);
45+
builder.EnableScheduledTasksSupport();
46+
});
47+
48+
// Configure console logging
49+
services.AddLogging(logging =>
50+
{
51+
logging.AddSimpleConsole(options =>
52+
{
53+
options.SingleLine = true;
54+
options.UseUtcTimestamp = true;
55+
options.TimestampFormat = "yyyy-MM-ddTHH:mm:ss.fffZ ";
56+
});
57+
});
58+
})
59+
.Build();
6060

6161
await host.StartAsync();
62+
6263
IScheduledTaskClient scheduledTaskClient = host.Services.GetRequiredService<IScheduledTaskClient>();
6364

6465
try
6566
{
6667
// Create schedule options that runs every 30 seconds
67-
var scheduleOptions = new ScheduleCreationOptions("DemoOrchestration")
68+
ScheduleCreationOptions scheduleOptions = new ScheduleCreationOptions("DemoOrchestration")
6869
{
6970
ScheduleId = "demo-schedule",
7071
Interval = TimeSpan.FromSeconds(30),
@@ -106,6 +107,21 @@
106107
Console.WriteLine("\nPress any key to delete the schedule and exit...");
107108
Console.ReadKey();
108109

110+
// intentionally call schedule to trigger exceptions
111+
await scheduleHandle.ResumeAsync();
112+
await scheduleHandle.ResumeAsync();
113+
114+
// Get schedule instance details
115+
var scheduleInstanceDetails = await scheduleHandle.GetScheduleInstanceDetailsAsync(true);
116+
Console.WriteLine($"\nSchedule instance details:");
117+
Console.WriteLine($"{scheduleInstanceDetails}");
118+
Console.WriteLine($"Instance ID: {scheduleInstanceDetails!.InstanceId}");
119+
Console.WriteLine($"Created time: {scheduleInstanceDetails.CreatedAt}");
120+
Console.WriteLine($"Name: {scheduleInstanceDetails.Name}");
121+
Console.WriteLine($"RuntimeStatus: {scheduleInstanceDetails.RuntimeStatus}");
122+
Console.WriteLine($"LastUpdatedAt: {scheduleInstanceDetails.LastUpdatedAt}");
123+
Console.WriteLine($"FailureDetails: {scheduleInstanceDetails.FailureDetails}");
124+
Console.WriteLine(); // Add blank line between instances
109125
// Delete the schedule
110126
await scheduleHandle.DeleteAsync();
111127
Console.WriteLine("Schedule deleted.");

src/ScheduledTasks/Client/IScheduleHandle.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
14
using Microsoft.DurableTask.Client;
25

36
namespace Microsoft.DurableTask.ScheduledTasks;

src/ScheduledTasks/Logging/Entity/Logs.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.Logging;
55

66
namespace Microsoft.DurableTask.ScheduledTasks;
7+
78
/// <summary>
89
/// Log messages.
910
/// </summary>
@@ -41,7 +42,7 @@ static partial class Logs
4142

4243
[LoggerMessage(EventId = 10, Level = LogLevel.Information, Message = "Schedule '{scheduleId}' is executed")]
4344
public static partial void CompletedScheduleRun(this ILogger logger, string scheduleId);
44-
45+
4546
[LoggerMessage(EventId = 11, Level = LogLevel.Information, Message = "Schedule '{scheduleId}' is being deleted")]
4647
public static partial void DeletingSchedule(this ILogger logger, string scheduleId);
4748

@@ -59,4 +60,4 @@ static partial class Logs
5960

6061
[LoggerMessage(EventId = 16, Level = LogLevel.Information, Message = "Schedule '{scheduleId}' run cancelled with execution token '{executionToken}'")]
6162
public static partial void ScheduleRunCancelled(this ILogger logger, string scheduleId, string executionToken);
62-
}
63+
}

src/ScheduledTasks/Models/ScheduleCreationOptions.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ namespace Microsoft.DurableTask.ScheduledTasks;
88
/// </summary>
99
public record ScheduleCreationOptions
1010
{
11+
/// <summary>
12+
/// The interval of the schedule.
13+
/// </summary>
14+
TimeSpan? interval;
15+
1116
/// <summary>
1217
/// Gets the name of the orchestration function to schedule.
1318
/// </summary>
@@ -49,11 +54,6 @@ public ScheduleCreationOptions(string orchestrationName)
4954
/// </summary>
5055
public DateTimeOffset? EndAt { get; init; }
5156

52-
/// <summary>
53-
/// The interval of the schedule.
54-
/// </summary>
55-
TimeSpan? interval;
56-
5757
/// <summary>
5858
/// Gets the interval of the schedule.
5959
/// </summary>
@@ -79,9 +79,18 @@ public TimeSpan? Interval
7979
}
8080
}
8181

82+
/// <summary>
83+
/// Gets the cron expression for the schedule.
84+
/// </summary>
8285
public string? CronExpression { get; init; }
8386

87+
/// <summary>
88+
/// Gets the maximum number of occurrences for the schedule.
89+
/// </summary>
8490
public int MaxOccurrence { get; init; }
8591

92+
/// <summary>
93+
/// Gets a value indicating whether to start the schedule immediately if it is late.
94+
/// </summary>
8695
public bool? StartImmediatelyIfLate { get; init; }
8796
}

0 commit comments

Comments
 (0)