Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 8a31efe

Browse files
committed
badge for buid status
1 parent 2c5f58f commit 8a31efe

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# eShopOnContainers - Microservices Architecture and Containers based Reference Application (**BETA state** - Visual Studio 2017 and CLI environments compatible)
22
Sample .NET Core reference application, powered by Microsoft, based on a simplified microservices architecture and Docker containers.
33

4+
[![Build status (Linux images)](https://msftdevtools.visualstudio.com/eShopOnContainers/_apis/build/status/All%20Microservices%20Linux)](https://msftdevtools.visualstudio.com/eShopOnContainers/_build/latest?definitionId=184)
5+
6+
47
## IMPORTANT NOTES!
58
**You can use either the latest version of Visual Studio or simply Docker CLI and .NET CLI for Windows, Mac and Linux**.
69

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Configuration;
23
using Microsoft.Extensions.DependencyInjection;
34
using Microsoft.Extensions.Logging;
45
using Polly;
@@ -10,8 +11,17 @@ namespace Microsoft.AspNetCore.Hosting
1011
{
1112
public static class IWebHostExtensions
1213
{
14+
public static bool IsInKubernetes(this IWebHost webHost)
15+
{
16+
var cfg = webHost.Services.GetService<IConfiguration>();
17+
var orchestratorType = cfg.GetValue<string>("OrchestratorType");
18+
return orchestratorType?.ToUpper() == "K8S";
19+
}
20+
1321
public static IWebHost MigrateDbContext<TContext>(this IWebHost webHost, Action<TContext,IServiceProvider> seeder) where TContext : DbContext
1422
{
23+
var underK8s = webHost.IsInKubernetes();
24+
1525
using (var scope = webHost.Services.CreateScope())
1626
{
1727
var services = scope.ServiceProvider;
@@ -24,36 +34,47 @@ public static IWebHost MigrateDbContext<TContext>(this IWebHost webHost, Action<
2434
{
2535
logger.LogInformation($"Migrating database associated with context {typeof(TContext).Name}");
2636

27-
var retry = Policy.Handle<SqlException>()
28-
.WaitAndRetry(new TimeSpan[]
29-
{
37+
if (underK8s)
38+
{
39+
InvokeSeeder(seeder, context, services);
40+
}
41+
else
42+
{
43+
var retry = Policy.Handle<SqlException>()
44+
.WaitAndRetry(new TimeSpan[]
45+
{
3046
TimeSpan.FromSeconds(3),
3147
TimeSpan.FromSeconds(5),
3248
TimeSpan.FromSeconds(8),
33-
});
49+
});
3450

35-
retry.Execute(() =>
36-
{
3751
//if the sql server container is not created on run docker compose this
3852
//migration can't fail for network related exception. The retry options for DbContext only
39-
//apply to transient exceptions.
40-
41-
context.Database
42-
.Migrate();
43-
44-
seeder(context, services);
45-
});
46-
53+
//apply to transient exceptions
54+
// Note that this is NOT applied when running some orchestrators (let the orchestrator to recreate the failing service)
55+
retry.Execute(() => InvokeSeeder(seeder, context, services));
56+
}
4757

4858
logger.LogInformation($"Migrated database associated with context {typeof(TContext).Name}");
4959
}
5060
catch (Exception ex)
5161
{
5262
logger.LogError(ex, $"An error occurred while migrating the database used on context {typeof(TContext).Name}");
63+
if (underK8s)
64+
{
65+
throw; // Rethrow under k8s because we rely on k8s to re-run the pod
66+
}
5367
}
5468
}
5569

5670
return webHost;
5771
}
72+
73+
private static void InvokeSeeder<TContext>(Action<TContext, IServiceProvider> seeder, TContext context, IServiceProvider services)
74+
where TContext : DbContext
75+
{
76+
context.Database.Migrate();
77+
seeder(context, services);
78+
}
5879
}
5980
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
3-
43
<!--
54
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
65
-->
7-
86
<system.webServer>
97
<handlers>
10-
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
8+
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
119
</handlers>
12-
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
10+
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
11+
<environmentVariables />
12+
</aspNetCore>
1313
</system.webServer>
14-
</configuration>
14+
</configuration>

0 commit comments

Comments
 (0)