Skip to content

Commit 5d098b1

Browse files
committed
Autorefresh on healthchecks with config timeout
1 parent e42e00f commit 5d098b1

9 files changed

Lines changed: 64 additions & 25 deletions

File tree

src/Services/Catalog/Catalog.API/Startup.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ public void ConfigureServices(IServiceCollection services)
5050

5151
services.AddHealthChecks(checks =>
5252
{
53-
checks.AddSqlCheck("CatalogDb", Configuration["ConnectionString"]);
53+
var minutes = 1;
54+
if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed))
55+
{
56+
minutes = minutesParsed;
57+
}
58+
checks.AddSqlCheck("CatalogDb", Configuration["ConnectionString"], TimeSpan.FromMinutes(minutes));
5459
});
5560

5661
services.AddMvc(options =>

src/Services/Identity/Identity.API/Startup.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ public void ConfigureServices(IServiceCollection services)
6666

6767
services.AddHealthChecks(checks =>
6868
{
69-
checks.AddSqlCheck("Identity_Db", Configuration.GetConnectionString("DefaultConnection"));
69+
var minutes = 1;
70+
if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed))
71+
{
72+
minutes = minutesParsed;
73+
}
74+
checks.AddSqlCheck("Identity_Db", Configuration.GetConnectionString("DefaultConnection"), TimeSpan.FromMinutes(minutes));
7075
});
7176

7277
services.AddTransient<IEmailSender, AuthMessageSender>();

src/Services/Ordering/Ordering.API/Startup.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
6161

6262
services.AddHealthChecks(checks =>
6363
{
64-
checks.AddSqlCheck("OrderingDb", Configuration["ConnectionString"]);
64+
var minutes = 1;
65+
if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed))
66+
{
67+
minutes = minutesParsed;
68+
}
69+
checks.AddSqlCheck("OrderingDb", Configuration["ConnectionString"], TimeSpan.FromMinutes(minutes));
6570
});
6671

6772
services.AddEntityFrameworkSqlServer()

src/Web/WebMVC/Startup.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,21 @@ public void ConfigureServices(IServiceCollection services)
5454

5555
services.AddHealthChecks(checks =>
5656
{
57-
checks.AddUrlCheck(Configuration["CatalogUrl"]);
58-
checks.AddUrlCheck(Configuration["OrderingUrl"]);
59-
checks.AddUrlCheck(Configuration["BasketUrl"]);
60-
checks.AddUrlCheck(Configuration["IdentityUrl"]);
57+
var minutes = 1;
58+
if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed))
59+
{
60+
minutes = minutesParsed;
61+
}
62+
checks.AddUrlCheck(Configuration["CatalogUrl"], TimeSpan.FromMinutes(minutes));
63+
checks.AddUrlCheck(Configuration["OrderingUrl"], TimeSpan.FromMinutes(minutes));
64+
checks.AddUrlCheck(Configuration["BasketUrl"], TimeSpan.FromMinutes(minutes));
65+
checks.AddUrlCheck(Configuration["IdentityUrl"], TimeSpan.FromMinutes(minutes));
6166
});
6267

6368
// Add application services.
64-
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
65-
services.AddTransient<ICatalogService, CatalogService>();
66-
services.AddTransient<IOrderingService, OrderingService>();
69+
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
70+
services.AddTransient<ICatalogService, CatalogService>();
71+
services.AddTransient<IOrderingService, OrderingService>();
6772
services.AddTransient<IBasketService, BasketService>();
6873
services.AddTransient<IIdentityParser<ApplicationUser>, IdentityParser>();
6974

@@ -76,7 +81,7 @@ public void ConfigureServices(IServiceCollection services)
7681
{
7782
services.AddSingleton<IHttpClient, StandardHttpClient>();
7883
}
79-
}
84+
}
8085

8186
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
8287
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
@@ -113,10 +118,10 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
113118
AuthenticationScheme = "oidc",
114119
SignInScheme = "Cookies",
115120
Authority = identityUrl.ToString(),
116-
PostLogoutRedirectUri = callBackUrl.ToString(),
121+
PostLogoutRedirectUri = callBackUrl.ToString(),
117122
ClientId = "mvc",
118123
ClientSecret = "secret",
119-
ResponseType = "code id_token",
124+
ResponseType = "code id_token",
120125
SaveTokens = true,
121126
GetClaimsFromUserInfoEndpoint = true,
122127
RequireHttpsMetadata = false,

src/Web/WebSPA/Startup.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,16 @@ public void ConfigureServices(IServiceCollection services)
4545
{
4646
services.AddHealthChecks(checks =>
4747
{
48-
checks.AddUrlCheck(Configuration["CatalogUrl"]);
49-
checks.AddUrlCheck(Configuration["OrderingUrl"]);
50-
checks.AddUrlCheck(Configuration["BasketUrl"]);
51-
checks.AddUrlCheck(Configuration["IdentityUrl"]);
48+
var minutes = 1;
49+
if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed))
50+
{
51+
minutes = minutesParsed;
52+
}
53+
54+
checks.AddUrlCheck(Configuration["CatalogUrl"], TimeSpan.FromMinutes(minutes));
55+
checks.AddUrlCheck(Configuration["OrderingUrl"], TimeSpan.FromMinutes(minutes));
56+
checks.AddUrlCheck(Configuration["BasketUrl"], TimeSpan.FromMinutes(minutes));
57+
checks.AddUrlCheck(Configuration["IdentityUrl"], TimeSpan.FromMinutes(minutes));
5258
});
5359

5460
services.Configure<AppSettings>(Configuration);

src/Web/WebStatus/Controllers/HomeController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public async Task<IActionResult> Index()
2828
data.AddResult(checkResult.Key, checkResult.Value);
2929
}
3030

31+
ViewBag.RefreshSeconds = 60;
3132
return View(data);
3233
}
3334

src/Web/WebStatus/Extensions/HealthCheckBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ namespace WebStatus.Extensions
88
{
99
public static class HealthCheckBuilderExtensions
1010
{
11-
public static HealthCheckBuilder AddUrlCheckIfNotNull(this HealthCheckBuilder builder, string url)
11+
public static HealthCheckBuilder AddUrlCheckIfNotNull(this HealthCheckBuilder builder, string url, TimeSpan cacheDuration)
1212
{
1313
if (!string.IsNullOrEmpty(url))
1414
{
15-
builder.AddUrlCheck(url);
15+
builder.AddUrlCheck(url, cacheDuration);
1616
}
1717

1818
return builder;

src/Web/WebStatus/Startup.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@ public void ConfigureServices(IServiceCollection services)
3232
// Add framework services.
3333
services.AddHealthChecks(checks =>
3434
{
35-
checks.AddUrlCheckIfNotNull(Configuration["OrderingUrl"]);
36-
checks.AddUrlCheckIfNotNull(Configuration["BasketUrl"]);
37-
checks.AddUrlCheckIfNotNull(Configuration["CatalogUrl"]);
38-
checks.AddUrlCheckIfNotNull(Configuration["IdentityUrl"]);
39-
checks.AddUrlCheckIfNotNull(Configuration["mvc"]);
40-
checks.AddUrlCheckIfNotNull(Configuration["spa"]);
35+
var minutes = 1;
36+
if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed))
37+
{
38+
minutes = minutesParsed;
39+
}
40+
41+
checks.AddUrlCheckIfNotNull(Configuration["OrderingUrl"], TimeSpan.FromMinutes(minutes));
42+
checks.AddUrlCheckIfNotNull(Configuration["BasketUrl"], TimeSpan.FromMinutes(minutes));
43+
checks.AddUrlCheckIfNotNull(Configuration["CatalogUrl"], TimeSpan.FromMinutes(minutes));
44+
checks.AddUrlCheckIfNotNull(Configuration["IdentityUrl"], TimeSpan.FromMinutes(minutes));
45+
checks.AddUrlCheckIfNotNull(Configuration["mvc"], TimeSpan.FromMinutes(minutes));
46+
checks.AddUrlCheckIfNotNull(Configuration["spa"], TimeSpan.FromMinutes(minutes));
4147
});
4248
services.AddMvc();
4349
}

src/Web/WebStatus/Views/Shared/_Layout.cshtml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
1717
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
1818
</environment>
19+
20+
@if (ViewBag.RefreshSeconds != null && ViewBag.RefreshSeconds > 0)
21+
{
22+
<meta http-equiv="refresh" content="@ViewBag.RefreshSeconds">
23+
}
24+
1925
@Html.Raw(JavaScriptSnippet.FullScript)
2026
</head>
2127
<body>

0 commit comments

Comments
 (0)