|
1 | 1 | using BookStore.ApiService.Infrastructure; |
2 | 2 | using BookStore.ApiService.Infrastructure.Extensions; |
| 3 | +using BookStore.ApiService.Projections; |
3 | 4 | using Marten; |
4 | 5 | using Scalar.AspNetCore; |
5 | 6 |
|
|
24 | 25 | { |
25 | 26 | using var scope = app.Services.CreateScope(); |
26 | 27 | var store = scope.ServiceProvider.GetRequiredService<IDocumentStore>(); |
| 28 | + var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>(); |
27 | 29 |
|
28 | 30 | // Apply schema to create PostgreSQL extensions (pg_trgm, unaccent) |
29 | 31 | await store.Storage.ApplyAllConfiguredChangesToDatabaseAsync(); |
30 | 32 |
|
31 | 33 | var seeder = new DatabaseSeeder(store); |
32 | 34 | await seeder.SeedAsync(); |
33 | 35 |
|
34 | | - // Give async projections time to process the seeded events |
| 36 | + // Wait for async projections to process the seeded events |
35 | 37 | // In production, projections run continuously in the background |
36 | | - await Task.Delay(TimeSpan.FromSeconds(2)); |
| 38 | + await WaitForProjectionsAsync(store, logger); |
| 39 | +} |
| 40 | + |
| 41 | +static async Task WaitForProjectionsAsync(IDocumentStore store, ILogger logger) |
| 42 | +{ |
| 43 | + logger.LogInformation("Waiting for async projections to complete..."); |
| 44 | + |
| 45 | + var timeout = TimeSpan.FromSeconds(30); |
| 46 | + var checkInterval = TimeSpan.FromMilliseconds(100); |
| 47 | + var stopwatch = System.Diagnostics.Stopwatch.StartNew(); |
| 48 | + |
| 49 | + while (stopwatch.Elapsed < timeout) |
| 50 | + { |
| 51 | + await using var session = store.QuerySession(); |
| 52 | + |
| 53 | + // Check if projections have data by querying the projection tables |
| 54 | + var bookCount = await session.Query<BookSearchProjection>().CountAsync(); |
| 55 | + var authorCount = await session.Query<AuthorProjection>().CountAsync(); |
| 56 | + var categoryCount = await session.Query<CategoryProjection>().CountAsync(); |
| 57 | + var publisherCount = await session.Query<PublisherProjection>().CountAsync(); |
| 58 | + |
| 59 | + // If all projections have data, we're ready |
| 60 | + if (bookCount > 0 && authorCount > 0 && categoryCount > 0 && publisherCount > 0) |
| 61 | + { |
| 62 | + logger.LogInformation( |
| 63 | + "All projections are ready: {BookCount} books, {AuthorCount} authors, {CategoryCount} categories, {PublisherCount} publishers", |
| 64 | + bookCount, authorCount, categoryCount, publisherCount); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + await Task.Delay(checkInterval); |
| 69 | + } |
| 70 | + |
| 71 | + logger.LogWarning("Projection initialization timed out after {Timeout}s. Some projections may not be ready.", timeout.TotalSeconds); |
37 | 72 | } |
38 | 73 |
|
39 | 74 | // Configure the HTTP request pipeline |
|
0 commit comments