Files
BeWoPlaner/Service/Status/BaseStatusService.cs

127 lines
3.1 KiB
C#
Raw Normal View History

using BeWo.Data.Access;
using BeWo.ServerUtils.Core;
2026-07-10 13:28:04 +02:00
using BeWo.Service.Core;
using BS.Shared.DataContracts;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace BeWo.Service.Status
{
public class BaseStatusService
{
private static readonly log4net.ILog log = LoggerUtils.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
2026-07-13 11:51:02 +02:00
private readonly string joinChar = Environment.NewLine;
2026-07-10 13:28:04 +02:00
protected string Name { get; set; }
protected List<Func<string>> Checkups { get; } = new List<Func<string>>();
2026-07-10 13:28:04 +02:00
private List<WebConfigSetting> RequiredWebConfigs { get; } = new List<WebConfigSetting>();
private List<WebSecretConfigSetting> RequiredWebSecretConfigs { get; } = new List<WebSecretConfigSetting>();
private List<string> RequiredDBScripts { get; } = new List<string>();
private List<SpecialConfig> RequiredConfigs { get; } = new List<SpecialConfig>();
2026-07-10 13:28:04 +02:00
public BaseStatusService(string name)
{
Name = name;
RegisterCheckup(CheckWebConfig,
CheckWebSecretConfig,
CheckDBScripts,
CheckConfig);
2026-07-10 13:28:04 +02:00
}
protected void AddWebConfigCheck(params WebConfigSetting[] args)
2026-07-10 13:28:04 +02:00
{
RequiredWebConfigs.AddRange(args);
}
protected void AddWebSecretConfigCheck(params WebSecretConfigSetting[] args)
{
2026-07-10 13:28:04 +02:00
RequiredWebSecretConfigs.AddRange(args);
}
protected void AddDBScript(params string[] args)
2026-07-10 13:28:04 +02:00
{
RequiredDBScripts.AddRange(args);
}
protected void AddCustomConfig(params SpecialConfig[] args)
{
2026-07-10 13:28:04 +02:00
RequiredConfigs.AddRange(args);
}
protected void RegisterCheckup(params Func<string>[] args)
2026-07-10 13:28:04 +02:00
{
Checkups.AddRange(args);
}
public ServiceStatusDC DoHealthCheckup()
{
var errors = new List<string>();
2026-07-10 13:28:04 +02:00
foreach (var func in Checkups)
{
try
{
var error = func();
if (!string.IsNullOrEmpty(error))
errors.Add(error);
2026-07-10 13:28:04 +02:00
}
catch (Exception ex)
2026-07-10 13:28:04 +02:00
{
log.Error($"Health-Checkup '{Name}' fehlgeschlagen", ex);
errors.Add(ex.Message);
2026-07-10 13:28:04 +02:00
}
}
return new ServiceStatusDC
{
Name = Name,
IsOk = errors.Count == 0,
2026-07-13 11:51:02 +02:00
Message = errors.Count == 0 ? "OK" : string.Join(joinChar, errors)
2026-07-10 13:28:04 +02:00
};
}
private string CheckWebConfig()
2026-07-10 13:28:04 +02:00
{
var missing = RequiredWebConfigs
.Where(x => string.IsNullOrWhiteSpace(MergedConfig.GetSetting(x)))
.Select(x => $"'{x}' wurde nicht korrekt konfiguriert");
2026-07-10 13:28:04 +02:00
2026-07-13 11:51:02 +02:00
return string.Join(joinChar, missing);
2026-07-10 13:28:04 +02:00
}
private string CheckWebSecretConfig()
2026-07-10 13:28:04 +02:00
{
var missing = RequiredWebSecretConfigs
.Where(x => string.IsNullOrWhiteSpace(MergedConfig.GetSecretSetting(x)))
.Select(x => $"'{x}' wurde nicht korrekt konfiguriert");
2026-07-10 13:28:04 +02:00
2026-07-13 11:51:02 +02:00
return string.Join(joinChar, missing);
2026-07-10 13:28:04 +02:00
}
private string CheckDBScripts()
{
var missing = RequiredDBScripts
.Where(x => !DAOFactory.ScriptDAO.IsScriptExecuted(x))
.Select(x => $"Script '{x}' wurde nicht ausgeführt");
2026-07-10 13:28:04 +02:00
2026-07-13 11:51:02 +02:00
return string.Join(joinChar, missing);
2026-07-10 13:28:04 +02:00
}
private string CheckConfig()
2026-07-10 13:28:04 +02:00
{
var missing = RequiredConfigs
.Where(x => !File.Exists(ConfigReader.GetConfigPath(x)))
.Select(x => $"SpecialConfig.{x} wurde nicht korrekt konfiguriert");
2026-07-10 13:28:04 +02:00
2026-07-13 11:51:02 +02:00
return string.Join(joinChar, missing);
2026-07-10 13:28:04 +02:00
}
}
}