128 lines
3.3 KiB
C#
128 lines
3.3 KiB
C#
using BeWo.Data.Access;
|
|
using BeWo.ServerUtils.Core;
|
|
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);
|
|
|
|
private readonly string joinChar = Environment.NewLine;
|
|
private readonly string errorChar = "•";
|
|
|
|
protected string Name { get; set; }
|
|
|
|
protected List<Func<string>> Checkups { get; } = new List<Func<string>>();
|
|
|
|
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>();
|
|
|
|
public BaseStatusService(string name)
|
|
{
|
|
Name = name;
|
|
|
|
RegisterCheckup(CheckWebConfig,
|
|
CheckWebSecretConfig,
|
|
CheckDBScripts,
|
|
CheckConfig);
|
|
}
|
|
|
|
protected void AddWebConfigCheck(params WebConfigSetting[] args)
|
|
{
|
|
RequiredWebConfigs.AddRange(args);
|
|
}
|
|
|
|
protected void AddWebSecretConfigCheck(params WebSecretConfigSetting[] args)
|
|
{
|
|
RequiredWebSecretConfigs.AddRange(args);
|
|
}
|
|
|
|
protected void AddDBScript(params string[] args)
|
|
{
|
|
RequiredDBScripts.AddRange(args);
|
|
}
|
|
|
|
protected void AddCustomConfig(params SpecialConfig[] args)
|
|
{
|
|
RequiredConfigs.AddRange(args);
|
|
}
|
|
|
|
protected void RegisterCheckup(params Func<string>[] args)
|
|
{
|
|
Checkups.AddRange(args);
|
|
}
|
|
|
|
public ServiceStatusDC DoHealthCheckup()
|
|
{
|
|
var errors = new List<string>();
|
|
|
|
foreach (var func in Checkups)
|
|
{
|
|
try
|
|
{
|
|
var error = func();
|
|
|
|
if (!string.IsNullOrEmpty(error))
|
|
errors.Add(error);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error($"Health-Checkup '{Name}' fehlgeschlagen", ex);
|
|
errors.Add(ex.Message);
|
|
}
|
|
}
|
|
|
|
return new ServiceStatusDC
|
|
{
|
|
Name = Name,
|
|
IsOk = errors.Count == 0,
|
|
Message = errors.Count == 0 ? "OK" : string.Join(joinChar, errors.Select(x => x.StartsWith(errorChar) ? x : $"{errorChar} {x}"))
|
|
};
|
|
}
|
|
|
|
private string CheckWebConfig()
|
|
{
|
|
var missing = RequiredWebConfigs
|
|
.Where(x => string.IsNullOrWhiteSpace(MergedConfig.GetSetting(x)))
|
|
.Select(x => $"{errorChar} '{x}' wurde nicht korrekt konfiguriert");
|
|
|
|
return string.Join(joinChar, missing);
|
|
}
|
|
|
|
private string CheckWebSecretConfig()
|
|
{
|
|
var missing = RequiredWebSecretConfigs
|
|
.Where(x => string.IsNullOrWhiteSpace(MergedConfig.GetSecretSetting(x)))
|
|
.Select(x => $"{errorChar} '{x}' wurde nicht korrekt konfiguriert");
|
|
|
|
return string.Join(joinChar, missing);
|
|
}
|
|
|
|
private string CheckDBScripts()
|
|
{
|
|
var missing = RequiredDBScripts
|
|
.Where(x => !DAOFactory.ScriptDAO.IsScriptExecuted(x))
|
|
.Select(x => $"{errorChar} Script '{x}' wurde nicht ausgeführt");
|
|
|
|
return string.Join(joinChar, missing);
|
|
}
|
|
|
|
private string CheckConfig()
|
|
{
|
|
var missing = RequiredConfigs
|
|
.Where(x => !File.Exists(ConfigReader.GetConfigPath(x)))
|
|
.Select(x => $"{errorChar} SpecialConfig.{x} wurde nicht korrekt konfiguriert");
|
|
|
|
return string.Join(joinChar, missing);
|
|
}
|
|
}
|
|
}
|