Skip to content

Commit

Permalink
Ensure that a set of bridge modules is ready before letting server st…
Browse files Browse the repository at this point in the history
…artup continue.
  • Loading branch information
alexrp committed Aug 24, 2023
1 parent 2c09d8b commit 84070e0
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/server/world/Bridge/BridgeModuleGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ public BridgeModuleGenerator(IOptions<WorldOptions> options, ILogger<BridgeModul
_logger = logger;
}

Task IHostedService.StartAsync(CancellationToken cancellationToken)
async Task IHostedService.StartAsync(CancellationToken cancellationToken)
{
var ready = new TaskCompletionSource();
var ct = _cts.Token;

_ = Task.Run(() => GenerateModulesAsync(ct), ct);
_ = Task.Run(() => GenerateModulesAsync(ready, ct), ct);

return Task.CompletedTask;
await ready.Task;
}

async Task IHostedService.StopAsync(CancellationToken cancellationToken)
Expand All @@ -58,7 +59,7 @@ async Task IHostedService.StopAsync(CancellationToken cancellationToken)
_cts.Dispose();
}

private async Task GenerateModulesAsync(CancellationToken cancellationToken)
private async Task GenerateModulesAsync(TaskCompletionSource ready, CancellationToken cancellationToken)
{
ReadOnlyMemory<byte> CreateModule(BridgeModuleKind kind, int seed)
{
Expand Down Expand Up @@ -98,8 +99,9 @@ ReadOnlyMemory<byte> CreateModule(BridgeModuleKind kind, int seed)
Log.GeneratedBridgeModules(
_logger, _options.Value.ConcurrentModules, stopwatch.Elapsed.TotalMilliseconds);

// Note that running this method synchronously until this await ensures that we have a collection of valid
// modules before the host is allowed to finish the startup sequence.
// Signal that we have an initial set of modules so startup can continue.
_ = ready.TrySetResult();

await Task.Delay(_options.Value.ModuleRotationTime.ToTimeSpan(), cancellationToken);
}
}
Expand Down

0 comments on commit 84070e0

Please sign in to comment.