From 6642f109fae22bcb2aa7211458b694d448d56ff9 Mon Sep 17 00:00:00 2001 From: Tiago Alves Macambira Date: Fri, 22 Sep 2023 09:59:12 -0700 Subject: [PATCH] Wait for sidecar readyness before starting apps up. Closes #182 Signed-off-by: Tiago Alves Macambira --- feed-generator/Program.cs | 9 +++++++++ pubsub-workflow/Program.cs | 11 ++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/feed-generator/Program.cs b/feed-generator/Program.cs index c801fb37..86a26ee1 100644 --- a/feed-generator/Program.cs +++ b/feed-generator/Program.cs @@ -14,6 +14,7 @@ namespace FeedGenerator using Microsoft.Extensions.Logging; using Prometheus; using System; + using System.Threading; using System.Threading.Tasks; /// @@ -80,6 +81,14 @@ static internal async void StartMessageGeneratorAsync(int delayInMilliseconds, I DaprClientBuilder daprClientBuilder = new DaprClientBuilder(); DaprClient client = daprClientBuilder.Build(); + + // 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; + await client.WaitForSidecarAsync(timeout); + while (true) { SocialMediaMessage message = GeneratePost(); diff --git a/pubsub-workflow/Program.cs b/pubsub-workflow/Program.cs index 5c75443e..77f94586 100644 --- a/pubsub-workflow/Program.cs +++ b/pubsub-workflow/Program.cs @@ -30,9 +30,18 @@ static void Main(string[] args) ObservabilityUtils.StartMetricsServer(); var host = CreateHostBuilder(args).Build(); - + var logger = host.Services.GetRequiredService>(); + // 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");