Skip to content

Commit 9ed36bd

Browse files
committed
schedule webapp sample
1 parent 4785899 commit 9ed36bd

File tree

12 files changed

+490
-2
lines changed

12 files changed

+490
-2
lines changed

Microsoft.DurableTask.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScheduledTasks", "src\Sched
8989
EndProject
9090
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScheduleConsoleApp", "samples\ScheduleConsoleApp\ScheduleConsoleApp.csproj", "{A89B766C-987F-4C9F-8937-D0AB9FE640C8}"
9191
EndProject
92+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScheduleWebApp", "samples\ScheduleWebApp\ScheduleWebApp.csproj", "{100348B5-4D97-4A3F-B777-AB14F276F8FE}"
93+
EndProject
9294
Global
9395
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9496
Debug|Any CPU = Debug|Any CPU
@@ -235,6 +237,10 @@ Global
235237
{A89B766C-987F-4C9F-8937-D0AB9FE640C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
236238
{A89B766C-987F-4C9F-8937-D0AB9FE640C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
237239
{A89B766C-987F-4C9F-8937-D0AB9FE640C8}.Release|Any CPU.Build.0 = Release|Any CPU
240+
{100348B5-4D97-4A3F-B777-AB14F276F8FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
241+
{100348B5-4D97-4A3F-B777-AB14F276F8FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
242+
{100348B5-4D97-4A3F-B777-AB14F276F8FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
243+
{100348B5-4D97-4A3F-B777-AB14F276F8FE}.Release|Any CPU.Build.0 = Release|Any CPU
238244
EndGlobalSection
239245
GlobalSection(SolutionProperties) = preSolution
240246
HideSolutionNode = FALSE
@@ -279,6 +285,7 @@ Global
279285
{B48FACA9-A328-452A-BFAE-C4F60F9C7024} = {EFF7632B-821E-4CFC-B4A0-ED4B24296B17}
280286
{69ED743C-D616-4530-87E2-391D249D7368} = {8AFC9781-F6F1-4696-BB4A-9ED7CA9D612B}
281287
{A89B766C-987F-4C9F-8937-D0AB9FE640C8} = {EFF7632B-821E-4CFC-B4A0-ED4B24296B17}
288+
{100348B5-4D97-4A3F-B777-AB14F276F8FE} = {EFF7632B-821E-4CFC-B4A0-ED4B24296B17}
282289
EndGlobalSection
283290
GlobalSection(ExtensibilityGlobals) = postSolution
284291
SolutionGuid = {AB41CB55-35EA-4986-A522-387AB3402E71}

samples/ScheduleConsoleApp/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
using Microsoft.Extensions.DependencyInjection;
1010
using Microsoft.Extensions.Hosting;
1111
using Microsoft.Extensions.Logging;
12-
using ScheduleConsoleApp.Activities;
13-
1412

1513
// Create the host builder
1614
IHost host = Host.CreateDefaultBuilder(args)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace ScheduleWebApp.Models;
5+
6+
/// <summary>
7+
/// Represents a request to create a new schedule.
8+
/// </summary>
9+
public class CreateScheduleRequest
10+
{
11+
/// <summary>
12+
/// Gets or sets the unique identifier for the schedule.
13+
/// </summary>
14+
public string Id { get; set; } = default!;
15+
16+
/// <summary>
17+
/// Gets or sets the name of the orchestration to be scheduled.
18+
/// </summary>
19+
public string OrchestrationName { get; set; } = default!;
20+
21+
/// <summary>
22+
/// Gets or sets the input data for the orchestration.
23+
/// </summary>
24+
public string? Input { get; set; }
25+
26+
/// <summary>
27+
/// Gets or sets the time interval between schedule executions.
28+
/// </summary>
29+
public TimeSpan Interval { get; set; }
30+
31+
/// <summary>
32+
/// Gets or sets the time when the schedule should start.
33+
/// </summary>
34+
public DateTimeOffset? StartAt { get; set; }
35+
36+
/// <summary>
37+
/// Gets or sets the time when the schedule should end.
38+
/// </summary>
39+
public DateTimeOffset? EndAt { get; set; }
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace ScheduleWebApp.Models;
5+
6+
/// <summary>
7+
/// Represents a request to update an existing schedule.
8+
/// </summary>
9+
public class UpdateScheduleRequest
10+
{
11+
/// <summary>
12+
/// Gets or sets the name of the orchestration to be scheduled.
13+
/// </summary>
14+
public string OrchestrationName { get; set; } = default!;
15+
16+
/// <summary>
17+
/// Gets or sets the input data for the orchestration.
18+
/// </summary>
19+
public string? Input { get; set; }
20+
21+
/// <summary>
22+
/// Gets or sets the time interval between schedule executions.
23+
/// </summary>
24+
public TimeSpan Interval { get; set; }
25+
26+
/// <summary>
27+
/// Gets or sets the time when the schedule should start.
28+
/// </summary>
29+
public DateTimeOffset? StartAt { get; set; }
30+
31+
/// <summary>
32+
/// Gets or sets the time when the schedule should end.
33+
/// </summary>
34+
public DateTimeOffset? EndAt { get; set; }
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.DurableTask;
5+
6+
namespace ScheduleWebApp.Orchestrations;
7+
8+
public class CacheClearingOrchestrator : TaskOrchestrator<string, string>
9+
{
10+
public override async Task<string> RunAsync(TaskOrchestrationContext context, string scheduleId)
11+
{
12+
ILogger logger = context.CreateReplaySafeLogger(nameof(CacheClearingOrchestrator));
13+
try
14+
{
15+
logger.LogInformation("Starting CacheClearingOrchestration for schedule ID: {ScheduleId}", scheduleId);
16+
17+
// Simulate cache clearing
18+
await Task.Delay(TimeSpan.FromSeconds(5));
19+
20+
logger.LogInformation("CacheClearingOrchestration completed for schedule ID: {ScheduleId}", scheduleId);
21+
22+
return "ok";
23+
}
24+
catch (Exception ex)
25+
{
26+
logger.LogError(ex, "Error in CacheClearingOrchestration for schedule ID: {ScheduleId}", scheduleId);
27+
throw;
28+
}
29+
}
30+
}

samples/ScheduleWebApp/Program.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System.Text.Json.Serialization;
5+
using Microsoft.DurableTask.Client;
6+
using Microsoft.DurableTask.Client.AzureManaged;
7+
using Microsoft.DurableTask.ScheduledTasks;
8+
using Microsoft.DurableTask.Worker;
9+
using Microsoft.DurableTask.Worker.AzureManaged;
10+
using ScheduleWebApp.Orchestrations;
11+
12+
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
13+
14+
string connectionString = builder.Configuration["DURABLE_TASK_SCHEDULER_CONNECTION_STRING"]
15+
?? throw new InvalidOperationException("Missing required configuration 'DURABLE_TASK_SCHEDULER_CONNECTION_STRING'");
16+
17+
// Add all the generated orchestrations and activities automatically
18+
builder.Services.AddDurableTaskWorker(builder =>
19+
{
20+
builder.AddTasks(r =>
21+
{
22+
// Add your orchestrators and activities here
23+
r.AddOrchestrator<CacheClearingOrchestrator>();
24+
});
25+
builder.UseDurableTaskScheduler(connectionString);
26+
builder.EnableScheduledTasksSupport();
27+
});
28+
29+
// Register the client, which can be used to start orchestrations
30+
builder.Services.AddDurableTaskClient(builder =>
31+
{
32+
builder.UseDurableTaskScheduler(connectionString);
33+
builder.EnableScheduledTasksSupport();
34+
});
35+
36+
// Configure console logging using the simpler, more compact format
37+
builder.Services.AddLogging(logging =>
38+
{
39+
logging.AddSimpleConsole(options =>
40+
{
41+
options.SingleLine = true;
42+
options.UseUtcTimestamp = true;
43+
options.TimestampFormat = "yyyy-MM-ddTHH:mm:ss.fffZ ";
44+
});
45+
});
46+
47+
// Configure the HTTP request pipeline
48+
builder.Services.AddControllers().AddJsonOptions(options =>
49+
{
50+
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
51+
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
52+
});
53+
54+
// The actual listen URL can be configured in environment variables named "ASPNETCORE_URLS" or "ASPNETCORE_URLS_HTTPS"
55+
WebApplication app = builder.Build();
56+
app.MapControllers();
57+
app.Run();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:47697",
8+
"sslPort": 44371
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"applicationUrl": "http://localhost:5008",
15+
"dotnetRunMessages": true,
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)