Skip to content

Commit 5e29246

Browse files
committed
sample fb
1 parent 78e4966 commit 5e29246

File tree

3 files changed

+52
-27
lines changed

3 files changed

+52
-27
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.DurableTask;
5+
6+
namespace ScheduleDemo.Activities;
7+
8+
[DurableTask]
9+
public class GetStockPrice : TaskActivity<string, decimal>
10+
{
11+
public override Task<decimal> RunAsync(TaskActivityContext context, string symbol)
12+
{
13+
// Mock implementation - would normally call stock API
14+
return Task.FromResult(100.00m);
15+
}
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.DurableTask;
5+
using Microsoft.Extensions.Logging;
6+
7+
[DurableTask]
8+
public class StockPriceOrchestrator : TaskOrchestrator<string, string>
9+
{
10+
public override async Task<string> RunAsync(TaskOrchestrationContext context, string symbol)
11+
{
12+
var logger = context.CreateReplaySafeLogger("DemoOrchestration");
13+
logger.LogInformation("Getting stock price for: {symbol}", symbol);
14+
try
15+
{
16+
// Get current stock price
17+
decimal currentPrice = await context.CallActivityAsync<decimal>("GetStockPrice", symbol);
18+
19+
logger.LogInformation("Current price for {symbol} is ${price:F2}", symbol, currentPrice);
20+
21+
return $"Stock {symbol} price: ${currentPrice:F2} at {DateTime.UtcNow}";
22+
}
23+
catch (Exception ex)
24+
{
25+
logger.LogError(ex, "Error processing stock price for {symbol}", symbol);
26+
throw;
27+
}
28+
}
29+
}

samples/ScheduleDemo/Program.cs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Hosting;
1010
using Microsoft.Extensions.Logging;
11+
using ScheduleDemo.Activities;
12+
1113

1214
// Create the host builder
1315
IHost host = Host.CreateDefaultBuilder(args)
@@ -22,33 +24,11 @@
2224
// Add the Schedule entity and demo orchestration
2325
builder.AddTasks(r =>
2426
{
25-
// Add a demo orchestration that will be triggered by the schedule
26-
r.AddOrchestratorFunc<string, string>("DemoOrchestration", async (context, symbol) =>
27-
{
28-
var logger = context.CreateReplaySafeLogger("DemoOrchestration");
29-
logger.LogInformation("Getting stock price for: {symbol}", symbol);
30-
try
31-
{
32-
// Get current stock price
33-
decimal currentPrice = await context.CallActivityAsync<decimal>("GetStockPrice", symbol);
34-
35-
logger.LogInformation("Current price for {symbol} is ${price:F2}", symbol, currentPrice);
36-
37-
return $"Stock {symbol} price: ${currentPrice:F2} at {DateTime.UtcNow}";
38-
}
39-
catch (Exception ex)
40-
{
41-
logger.LogError(ex, "Error processing stock price for {symbol}", symbol);
42-
throw;
43-
}
44-
});
27+
// Add the orchestrator class
28+
r.AddOrchestrator<StockPriceOrchestrator>();
4529

4630
// Add required activities
47-
r.AddActivityFunc<string, decimal>("GetStockPrice", (context, symbol) =>
48-
{
49-
// Mock implementation - would normally call stock API
50-
return 100.00m;
51-
});
31+
r.AddActivity<GetStockPrice>();
5232
});
5333

5434
// Enable scheduled tasks support
@@ -94,7 +74,7 @@
9474
// Create schedule options that runs every 30 seconds
9575
ScheduleCreationOptions scheduleOptions = new ScheduleCreationOptions
9676
{
97-
OrchestrationName = "DemoOrchestration",
77+
OrchestrationName = nameof(StockPriceOrchestrator),
9878
ScheduleId = "demo-schedule101",
9979
Interval = TimeSpan.FromSeconds(4),
10080
StartAt = DateTimeOffset.UtcNow,
@@ -136,7 +116,7 @@
136116
Console.WriteLine("");
137117
Console.WriteLine("");
138118

139-
await Task.Delay(2000000);
119+
await Task.Delay(TimeSpan.FromMinutes(30));
140120
//Console.WriteLine("\nPress any key to delete the schedule and exit...");
141121
//Console.ReadKey();
142122

0 commit comments

Comments
 (0)