-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathProgram.cs
More file actions
175 lines (136 loc) · 6.07 KB
/
Program.cs
File metadata and controls
175 lines (136 loc) · 6.07 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using eShopDashboard.Infrastructure.Data.Catalog;
using eShopDashboard.Infrastructure.Data.Ordering;
using eShopDashboard.Infrastructure.Setup;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Events;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace eShopDashboard
{
public class Program
{
private static int _seedingProgress = 100;
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseSerilog()
.ConfigureAppConfiguration((builderContext, config) =>
{
config.AddEnvironmentVariables();
})
.Build();
public static int GetSeedingProgress()
{
return _seedingProgress;
}
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.Seq("http://localhost:5341/")
.CreateLogger();
Log.Information("----- Starting web host");
try
{
var host = BuildWebHost(args);
Log.Information("----- Seeding Database");
Task seeding = Task.Run(async () => { await ConfigureDatabaseAsync(host); });
Log.Information("----- Running Host");
host.Run();
Log.Information("----- Web host stopped");
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "----- Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
private static async Task ConfigureDatabaseAsync(IWebHost host)
{
_seedingProgress = 0;
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
var catalogContext = services.GetService<CatalogContext>();
await catalogContext.Database.MigrateAsync();
var orderingContext = services.GetService<OrderingContext>();
await orderingContext.Database.MigrateAsync();
}
await SeedDatabaseAsync(host);
_seedingProgress = 100;
}
private static async Task SeedDatabaseAsync(IWebHost host)
{
try
{
using (var scope = host.Services.CreateScope())
{
IServiceProvider services = scope.ServiceProvider;
Log.Information("----- Checking seeding status");
var catalogContextSetup = services.GetService<CatalogContextSetup>();
var orderingContextSetup = services.GetService<OrderingContextSetup>();
var catalogSeedingStatus = await catalogContextSetup.GetSeedingStatusAsync();
Log.Information("----- SeedingStatus ({Context}): {@SeedingStatus}", "Catalog", catalogSeedingStatus);
var orderingSeedingStatus = await orderingContextSetup.GetSeedingStatusAsync();
Log.Information("----- SeedingStatus ({Context}): {@SeedingStatus}", "Ordering", orderingSeedingStatus);
var seedingStatus = new SeedingStatus(catalogSeedingStatus, orderingSeedingStatus);
Log.Information("----- SeedingStatus ({Context}): {@SeedingStatus}", "Aggregated", seedingStatus);
if (!seedingStatus.NeedsSeeding)
{
Log.Information("----- No seeding needed");
return;
}
Log.Information("----- Seeding database");
var sw = new Stopwatch();
sw.Start();
void ProgressAggregator()
{
seedingStatus.RecordsLoaded = catalogSeedingStatus.RecordsLoaded + orderingSeedingStatus.RecordsLoaded;
Log.Debug("----- Seeding {SeedingPercentComplete}% complete", seedingStatus.PercentComplete);
_seedingProgress = seedingStatus.PercentComplete;
}
var catalogProgressHandler = new Progress<int>(value =>
{
catalogSeedingStatus.RecordsLoaded = value;
ProgressAggregator();
});
var orderingProgressHandler = new Progress<int>(value =>
{
orderingSeedingStatus.RecordsLoaded = value;
ProgressAggregator();
});
Log.Information("----- Seeding CatalogContext");
Task catalogSeedingTask = Task.Run(async () => await catalogContextSetup.SeedAsync(catalogProgressHandler));
Log.Information("----- Seeding OrderingContext");
Task orderingSeedingTask = Task.Run(async () => await orderingContextSetup.SeedAsync(orderingProgressHandler));
await Task.WhenAll(catalogSeedingTask, orderingSeedingTask);
seedingStatus.SetAsComplete();
_seedingProgress = seedingStatus.PercentComplete;
Log.Information("----- Database Seeded ({ElapsedTime:n3}s)", sw.Elapsed.TotalSeconds);
}
}
catch (Exception ex)
{
Log.Error(ex, "----- Exception seeding database");
}
}
}
}