166 lines
3.4 KiB
C#
166 lines
3.4 KiB
C#
using BeWo.Data.Access;
|
|
using BeWo.Service.Core;
|
|
using BS.Shared.DataContracts;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BeWo.Service.Status
|
|
{
|
|
public class BaseStatusService
|
|
{
|
|
protected string Name { get; set; }
|
|
|
|
protected List<Func<string>> Checkups { get; set; }
|
|
|
|
private List<WebConfigSetting> RequiredWebConfigs { get; set; }
|
|
private List<WebSecretConfigSetting> RequiredWebSecretConfigs { get; set; }
|
|
private List<string> RequiredDBScripts { get; set; }
|
|
private List<SpecialConfig> RequiredConfigs { get; set; }
|
|
|
|
public BaseStatusService(string name)
|
|
{
|
|
Name = name;
|
|
|
|
registerCheckup(checkWebConfig,
|
|
checkWebSecretConfig,
|
|
checkDBScripts,
|
|
checkConfig);
|
|
}
|
|
|
|
protected void addWebConfigCheck(params WebConfigSetting[] args)
|
|
{
|
|
if(RequiredWebConfigs is null)
|
|
RequiredWebConfigs = new List<WebConfigSetting>();
|
|
|
|
RequiredWebConfigs.AddRange(args);
|
|
}
|
|
|
|
protected void addWebSecretConfigCheck(params WebSecretConfigSetting[] args)
|
|
{
|
|
if(RequiredWebSecretConfigs is null)
|
|
RequiredWebSecretConfigs = new List<WebSecretConfigSetting>();
|
|
|
|
RequiredWebSecretConfigs.AddRange(args);
|
|
}
|
|
|
|
protected void addDBScript(params string[] args)
|
|
{
|
|
if(RequiredDBScripts is null)
|
|
RequiredDBScripts = new List<string>();
|
|
|
|
RequiredDBScripts.AddRange(args);
|
|
}
|
|
|
|
protected void addCustomConfig(params SpecialConfig[] args)
|
|
{
|
|
if(RequiredConfigs is null)
|
|
RequiredConfigs = new List<SpecialConfig>();
|
|
|
|
RequiredConfigs.AddRange(args);
|
|
}
|
|
|
|
protected void registerCheckup(params Func<string>[] args)
|
|
{
|
|
if (Checkups is null)
|
|
Checkups = new List<Func<string>>();
|
|
|
|
Checkups.AddRange(args);
|
|
}
|
|
|
|
public ServiceStatusDC DoHealthCheckup()
|
|
{
|
|
string error = string.Empty;
|
|
bool ok = true;
|
|
|
|
foreach (var func in Checkups)
|
|
{
|
|
try
|
|
{
|
|
error = func();
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
error = ex.Message;
|
|
}
|
|
|
|
ok = string.IsNullOrEmpty(error);
|
|
|
|
if (!ok)
|
|
break;
|
|
}
|
|
|
|
return new ServiceStatusDC
|
|
{
|
|
Name = Name,
|
|
IsOk = ok,
|
|
Message = ok ? "OK" : error
|
|
};
|
|
}
|
|
|
|
private string checkWebConfig()
|
|
{
|
|
if (RequiredWebConfigs is null)
|
|
return null;
|
|
|
|
foreach (var webConfig in RequiredWebConfigs)
|
|
{
|
|
var url = MergedConfig.GetSetting(webConfig);
|
|
if (string.IsNullOrWhiteSpace(url))
|
|
return $"'{webConfig}' wurde nicht korrekt konfiguriert";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private string checkWebSecretConfig()
|
|
{
|
|
if (RequiredWebSecretConfigs is null)
|
|
return null;
|
|
|
|
foreach (var webSecretConfig in RequiredWebSecretConfigs)
|
|
{
|
|
var key = MergedConfig.GetSecretSetting(webSecretConfig);
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
return $"'{webSecretConfig}' wurde nicht korrekt konfiguriert";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private string checkDBScripts()
|
|
{
|
|
if (RequiredDBScripts is null)
|
|
return null;
|
|
|
|
foreach (var script in RequiredDBScripts)
|
|
{
|
|
var executed = DAOFactory.ScriptDAO.IsScriptExecuted(script);
|
|
|
|
return executed ? null : $"Script '{script}' wurde nicht ausgeführt";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private string checkConfig()
|
|
{
|
|
if (RequiredConfigs is null)
|
|
return null;
|
|
|
|
foreach (var config in RequiredConfigs)
|
|
{
|
|
var configPath = ConfigReader.GetConfigPath(config);
|
|
|
|
if (!File.Exists(configPath))
|
|
return $"SpecialConfig.{config} wurde nicht korrekt konfiguriert";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|