|
11 | 11 |
|
12 | 12 | // Create the host builder |
13 | 13 | 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 => |
15 | 24 | { |
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 => |
21 | 27 | { |
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}"; |
39 | 31 | }); |
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(); |
60 | 60 |
|
61 | 61 | await host.StartAsync(); |
| 62 | + |
62 | 63 | IScheduledTaskClient scheduledTaskClient = host.Services.GetRequiredService<IScheduledTaskClient>(); |
63 | 64 |
|
64 | 65 | try |
65 | 66 | { |
66 | 67 | // Create schedule options that runs every 30 seconds |
67 | | - var scheduleOptions = new ScheduleCreationOptions("DemoOrchestration") |
| 68 | + ScheduleCreationOptions scheduleOptions = new ScheduleCreationOptions("DemoOrchestration") |
68 | 69 | { |
69 | 70 | ScheduleId = "demo-schedule", |
70 | 71 | Interval = TimeSpan.FromSeconds(30), |
|
106 | 107 | Console.WriteLine("\nPress any key to delete the schedule and exit..."); |
107 | 108 | Console.ReadKey(); |
108 | 109 |
|
| 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 |
109 | 125 | // Delete the schedule |
110 | 126 | await scheduleHandle.DeleteAsync(); |
111 | 127 | Console.WriteLine("Schedule deleted."); |
|
0 commit comments