Skip to content

Commit

Permalink
Merge pull request #280 from ONLYOFFICE/feature/healthcheck
Browse files Browse the repository at this point in the history
added health checks
  • Loading branch information
alexeybannov authored Jun 23, 2021
2 parents dc15252 + 20d1f9a commit c8e617d
Show file tree
Hide file tree
Showing 33 changed files with 461 additions and 87 deletions.
9 changes: 7 additions & 2 deletions ASC.Web.sln
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.202
MinimumVisualStudioVersion = 10.0.40219.1
Expand Down Expand Up @@ -87,6 +86,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ASC.Calendar", "products\AS
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ASC.SsoAuth.Svc", "common\services\ASC.SsoAuth.Svc\ASC.SsoAuth.Svc.csproj", "{6AD828EA-FBA2-4D30-B969-756B3BE78E4E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASC.Web.HealthChecks.UI", "web\ASC.Web.HealthChecks.UI\ASC.Web.HealthChecks.UI.csproj", "{0C1A387E-0CD0-4BE8-82FC-9FCAD05BF289}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ASC.CRM.BackgroundTasks", "products\ASC.CRM\BackgroundTasks\ASC.CRM.BackgroundTasks.csproj", "{E52C0E35-A05C-437F-8FF2-3E0AC9F81433}"
EndProject
Global
Expand Down Expand Up @@ -247,6 +248,10 @@ Global
{6AD828EA-FBA2-4D30-B969-756B3BE78E4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6AD828EA-FBA2-4D30-B969-756B3BE78E4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6AD828EA-FBA2-4D30-B969-756B3BE78E4E}.Release|Any CPU.Build.0 = Release|Any CPU
{0C1A387E-0CD0-4BE8-82FC-9FCAD05BF289}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C1A387E-0CD0-4BE8-82FC-9FCAD05BF289}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C1A387E-0CD0-4BE8-82FC-9FCAD05BF289}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C1A387E-0CD0-4BE8-82FC-9FCAD05BF289}.Release|Any CPU.Build.0 = Release|Any CPU
{E52C0E35-A05C-437F-8FF2-3E0AC9F81433}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E52C0E35-A05C-437F-8FF2-3E0AC9F81433}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E52C0E35-A05C-437F-8FF2-3E0AC9F81433}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
14 changes: 7 additions & 7 deletions build/build.backend.bat
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
PUSHD %~dp0..
dotnet build ASC.Web.sln /fl1 /flp1:LogFile=build/ASC.Web.log;Verbosity=Normal

echo "ASC.UrlShortener"
call build\scripts\urlshortener.sh
@echo off

echo "ASC.Thumbnails"
call build\scripts\thumbnails.sh

echo "ASC.Socket.IO"
call build\scripts\socket.sh
if %errorlevel% == 0 (
for /R "build\scripts\" %%f in (*.sh) do (
echo "%%~nxf"
call build\scripts\%%~nxf
)
)
2 changes: 2 additions & 0 deletions build/run/WebHealthChecksUI.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo "RUN ASC.Web.HealthChecks.UI"
call dotnet run --project ..\..\web\ASC.Web.HealthChecks.UI\ASC.Web.HealthChecks.UI.csproj --no-build
1 change: 1 addition & 0 deletions build/scripts/ssoauth.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarn install --cwd common/ASC.SsoAuth --frozen-lockfile
5 changes: 5 additions & 0 deletions common/ASC.Api.Core/ASC.Api.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.Elasticsearch" Version="5.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.Kafka" Version="5.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.MySql" Version="5.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="5.0.2" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="5.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
</ItemGroup>

Expand Down
52 changes: 31 additions & 21 deletions common/ASC.Api.Core/Core/BaseStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

using Autofac;

using HealthChecks.UI.Client;

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -33,7 +36,6 @@ public abstract class BaseStartup
public IHostEnvironment HostEnvironment { get; }
public virtual string[] LogParams { get; }
public virtual JsonConverter[] Converters { get; }
public virtual bool AddControllers { get; } = true;
public virtual bool ConfirmAddScheme { get; } = false;
protected DIHelper DIHelper { get; }

Expand All @@ -45,31 +47,29 @@ public BaseStartup(IConfiguration configuration, IHostEnvironment hostEnvironmen
}

public virtual void ConfigureServices(IServiceCollection services)
{
{
services.AddCustomHealthCheck(Configuration);
services.AddHttpContextAccessor();
services.AddMemoryCache();

DIHelper.Configure(services);

if (AddControllers)
{
services.AddControllers()
.AddXmlSerializerFormatters()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.WriteIndented = false;
options.JsonSerializerOptions.IgnoreNullValues = true;
options.JsonSerializerOptions.Converters.Add(new ApiDateTimeConverter());

if (Converters != null)
services.AddControllers()
.AddXmlSerializerFormatters()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.WriteIndented = false;
options.JsonSerializerOptions.IgnoreNullValues = true;
options.JsonSerializerOptions.Converters.Add(new ApiDateTimeConverter());

if (Converters != null)
{
foreach (var c in Converters)
{
foreach (var c in Converters)
{
options.JsonSerializerOptions.Converters.Add(c);
}
}
});
}
options.JsonSerializerOptions.Converters.Add(c);
}
}
});

DIHelper.TryAdd<DisposeMiddleware>();
DIHelper.TryAdd<CultureMiddleware>();
Expand Down Expand Up @@ -139,7 +139,17 @@ public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapCustom();
endpoints.MapCustom();

endpoints.MapHealthChecks("/health", new HealthCheckOptions()
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
endpoints.MapHealthChecks("/liveness", new HealthCheckOptions
{
Predicate = r => r.Name.Contains("self")
});
});
}

Expand Down
44 changes: 44 additions & 0 deletions common/ASC.Api.Core/Core/BaseWorkerStartup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using ASC.Api.Core.Core;

using HealthChecks.UI.Client;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace ASC.Api.Core
{
public class BaseWorkerStartup
{
public IConfiguration Configuration { get; }

public BaseWorkerStartup(IConfiguration configuration)
{
Configuration = configuration;
}

public virtual void ConfigureServices(IServiceCollection services)
{
services.AddCustomHealthCheck(Configuration);
}

public virtual void Configure(IApplicationBuilder app)
{
app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/health", new HealthCheckOptions()
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
endpoints.MapHealthChecks("/liveness", new HealthCheckOptions
{
Predicate = r => r.Name.Contains("self")
});
});
}
}
}
76 changes: 76 additions & 0 deletions common/ASC.Api.Core/Core/CustomHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Linq;

using ASC.Common.Caching;
using ASC.Common.Utils;

using Confluent.Kafka;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace ASC.Api.Core.Core
{
public static class CustomHealthCheck
{
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration)
{
var hcBuilder = services.AddHealthChecks();

hcBuilder.AddCheck("self", () => HealthCheckResult.Healthy());

var configurationExtension = new ConfigurationExtension(configuration);

var connectionString = configurationExtension.GetConnectionStrings("default");

if (String.Compare(connectionString.ProviderName, "MySql.Data.MySqlClient") == 0)
{
hcBuilder.AddMySql(connectionString.ConnectionString,
name: "mysqldb",
tags: new string[] { "mysqldb" }
);
}

if (String.Compare(connectionString.ProviderName, "Npgsql") == 0)
{
hcBuilder.AddNpgSql(connectionString.ConnectionString,
name: "postgredb",
tags: new string[] { "postgredb" }
);
}

var kafkaSettings = configurationExtension.GetSetting<KafkaSettings>("kafka");

if (kafkaSettings != null && !string.IsNullOrEmpty(kafkaSettings.BootstrapServers))
{
var clientConfig = new ClientConfig { BootstrapServers = kafkaSettings.BootstrapServers };

hcBuilder.AddKafka(new ProducerConfig(clientConfig),
name: "kafka",
tags: new string[] { "kafka" });

}


var elasticSettings = configuration.GetSection("elastic");

if (elasticSettings != null && elasticSettings.GetChildren().Any())
{
var host = elasticSettings.GetSection("Host").Value ?? "localhost";
var scheme = elasticSettings.GetSection("Scheme").Value ?? "http";
var port = elasticSettings.GetSection("Port").Value ?? "9200";
var elasticSearchUri = $"{scheme}://{host}:{port}";

if (Uri.IsWellFormedUriString(elasticSearchUri, UriKind.Absolute))
{
hcBuilder.AddElasticsearch(elasticSearchUri,
name: "elasticsearch",
tags: new string[] { "elasticsearch" });
}
}

return services;
}
}
}
1 change: 1 addition & 0 deletions common/services/ASC.ApiSystem/ASC.ApiSystem.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

<ItemGroup>
<ProjectReference Include="..\..\..\web\ASC.Web.Core\ASC.Web.Core.csproj" />
<ProjectReference Include="..\..\ASC.Api.Core\ASC.Api.Core.csproj" />
<ProjectReference Include="..\..\ASC.Common\ASC.Common.csproj" />
<ProjectReference Include="..\..\ASC.Core.Common\ASC.Core.Common.csproj" />
</ItemGroup>
Expand Down
21 changes: 9 additions & 12 deletions common/services/ASC.ApiSystem/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/


using ASC.Api.Core;
using ASC.ApiSystem.Classes;
using ASC.ApiSystem.Controllers;
using ASC.Common;
Expand All @@ -35,7 +36,6 @@

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -44,20 +44,20 @@

namespace ASC.ApiSystem
{
public class Startup
public class Startup : BaseWorkerStartup
{
public IConfiguration Configuration { get; }
public IHostEnvironment HostEnvironment { get; }

public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment)
public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment) : base(configuration)
{
Configuration = configuration;
HostEnvironment = hostEnvironment;
}

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
public override void ConfigureServices(IServiceCollection services)
{
base.ConfigureServices(services);

var diHelper = new DIHelper(services);

services.AddHttpContextAccessor();
Expand Down Expand Up @@ -87,12 +87,9 @@ public void ConfigureServices(IServiceCollection services)
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
public override void Configure(IApplicationBuilder app)
{
base.Configure(app);

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\ASC.Api.Core\ASC.Api.Core.csproj" />
<ProjectReference Include="..\..\ASC.Data.Storage\ASC.Data.Storage.csproj" />
</ItemGroup>

Expand Down
Loading

0 comments on commit c8e617d

Please sign in to comment.