-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathScenariosController.cs
More file actions
50 lines (43 loc) · 1.78 KB
/
ScenariosController.cs
File metadata and controls
50 lines (43 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.DurableTask;
using Microsoft.DurableTask.Client;
namespace AspNetWebApp;
[Route("scenarios")]
[ApiController]
public partial class ScenariosController(
DurableTaskClient durableTaskClient,
ILogger<ScenariosController> logger) : ControllerBase
{
readonly DurableTaskClient durableTaskClient = durableTaskClient;
readonly ILogger<ScenariosController> logger = logger;
[HttpPost("hellocities")]
public async Task<ActionResult> RunHelloCities([FromQuery] int? count, [FromQuery] string? prefix)
{
if (count is null || count < 1)
{
return this.BadRequest(new { error = "A 'count' query string parameter is required and it must contain a positive number." });
}
// Generate a semi-unique prefix for the instance IDs to simplify tracking
prefix ??= $"hellocities-{count}-";
prefix += Guid.NewGuid().ToString();
this.logger.LogInformation("Scheduling {count} orchestrations with a prefix of '{prefix}'...", count, prefix);
Stopwatch sw = Stopwatch.StartNew();
await Enumerable.Range(0, count.Value).ParallelForEachAsync(1000, i =>
{
string instanceId = $"{prefix}-{i:X16}";
return this.durableTaskClient.ScheduleNewHelloCitiesInstanceAsync(
input: null!,
new StartOrchestrationOptions(instanceId));
});
sw.Stop();
this.logger.LogInformation(
"All {count} orchestrations were scheduled successfully in {time}ms!",
count,
sw.ElapsedMilliseconds);
return this.Ok(new
{
message = $"Scheduled {count} orchestrations prefixed with '{prefix}' in {sw.ElapsedMilliseconds}."
});
}
}