Skip to content

Commit

Permalink
Wait for sidecar readyness before starting apps up.
Browse files Browse the repository at this point in the history
Closes #182

Signed-off-by: Tiago Alves Macambira <[email protected]>
  • Loading branch information
tmacam committed Sep 25, 2023
1 parent 9c2f9ed commit 6642f10
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 9 additions & 0 deletions feed-generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace FeedGenerator
using Microsoft.Extensions.Logging;
using Prometheus;
using System;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
Expand Down Expand Up @@ -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();
Expand Down
11 changes: 10 additions & 1 deletion pubsub-workflow/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ 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");
Expand Down

0 comments on commit 6642f10

Please sign in to comment.