-
Notifications
You must be signed in to change notification settings - Fork 24
/
Program.cs
84 lines (68 loc) · 3.47 KB
/
Program.cs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------
using Dapr.Client;
using Dapr.Tests.Common;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace PubsubWorkflow
{
class PubsubWorkflow
{
private static string rapidPubsubName = "longhaul-sb-rapid";
private static string mediumPubsubName = "longhaul-sb-medium";
private static string slowPubsubName = "longhaul-sb-slow";
private static string glacialPubsubName = "longhaul-sb-glacial";
static void Main(string[] args)
{
ObservabilityUtils.StartMetricsServer();
var host = CreateHostBuilder(args).Build();
var logger = host.Services.GetRequiredService<ILogger<PubsubWorkflow>>();
// When ran in k8s, this app might start before Dapr sidecar is ready
// and this will lead to errors. Let's wait a bit for the sidecar to
// be ready before we start publishing.
logger.LogInformation("Waiting for Dapr sidecar to be ready...");
CancellationToken timeout = new CancellationTokenSource(TimeSpan.FromMinutes(1)).Token;
var client = new DaprClientBuilder().Build();
client.WaitForSidecarAsync(timeout).Wait();
logger.LogInformation("Starting Pubsub Workflow");
var rapidTimer = StartPublishingMessages(10, rapidPubsubName, "rapidtopic");
var mediumTimer = StartPublishingMessages(300, mediumPubsubName, "mediumtopic");
var slowTimer = StartPublishingMessages(3600, slowPubsubName, "slowtopic");
var glacialTimer = StartPublishingMessages(3600*12, glacialPubsubName, "glacialtopic");
host.Run();
logger.LogInformation("Exiting Pubsub Workflow");
rapidTimer.Dispose();
mediumTimer.Dispose();
slowTimer.Dispose();
glacialTimer.Dispose();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureTestInfraLogging()
.ConfigureWebHostDefaults(webBuilder =>
{
var appSettings = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true)
.AddCommandLine(args)
.Build();
webBuilder.UseStartup<Startup>()
.UseUrls(urls: $"http://*:{appSettings["DaprHTTPAppPort"]}");
});
static internal Timer StartPublishingMessages(int periodInSeconds, string pubsubName, string topic)
{
var client = new DaprClientBuilder().Build();
var messagePublisher = new MessagePublisher(client, pubsubName, topic);
return new Timer(messagePublisher.Publish, null, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(periodInSeconds));
}
}
}