forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckRepository.cs
More file actions
146 lines (112 loc) · 4.65 KB
/
Copy pathCheckRepository.cs
File metadata and controls
146 lines (112 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System.Configuration;
using System.Data;
using System.Linq;
using Dapper;
using L2dotNET.Logging.Abstraction;
using L2dotNET.Repositories.Contracts;
using L2dotNET.Repositories.Utils;
using MySql.Data.MySqlClient;
namespace L2dotNET.Repositories
{
public class CheckRepository : ICheckRepository
{
private static readonly ILog Log = LogProvider.GetCurrentClassLogger();
internal IDbConnection Db;
private const int PingTimeoutMs = 3000;
private const int PingRetryAttempts = 5;
private const int MysqlServiceStartTimeoutMs = 3000;
private const int MysqlServiceRetryAttempts = 5;
private const string MysqlServiceName = "MySQL";
private readonly string _host;
private readonly string _database;
public CheckRepository()
{
Db = new MySqlConnection("Server=127.0.0.1;Database=l2dotnet;Uid=l2dotnet;Pwd=l2dotnet;SslMode=none;");
MySqlConnectionStringBuilder connStrBuilder = new MySqlConnectionStringBuilder(Db.ConnectionString);
_host = connStrBuilder.Server;
_database = connStrBuilder.Database;
}
public bool PreCheckRepository()
{
if (!CheckDatabaseHostPing())
return false;
if (!CheckMySqlService())
return false;
if (!CheckDatabaseQuery())
return false;
return true;
}
private bool CheckDatabaseHostPing()
{
Log.Info("Checking ping to database host...");
bool isHostPinging = HostCheck.IsPingSuccessful(_host, PingTimeoutMs);
for (int i = 1; !isHostPinging && (i <= PingRetryAttempts); i++)
{
Log.Error($"Ping to database host '{_host}' has FAILED!");
Log.Warn($"Retrying to ping...Retry attempt: {i}.");
isHostPinging = HostCheck.IsPingSuccessful(_host, PingTimeoutMs);
if (isHostPinging)
break;
}
if (isHostPinging)
Log.Info($"Ping to database host '{_host}' was SUCCESSFUL!");
else
Log.Error($"Ping to database host '{_host}' has FAILED!");
return isHostPinging;
}
private bool CheckMySqlService()
{
if (HostCheck.IsLocalIpAddress(_host))
{
Log.Info("Database host running at localhost.");
Log.Info("Checking if MySQL Service is running at localhost...");
if (!HostCheck.ServiceExists(MysqlServiceName))
{
Log.Error("MySQL Service does not exist at localhost Windows Services!");
return false;
}
bool isMySqlServiceRunning = HostCheck.IsServiceRunning(MysqlServiceName);
for (int i = 1; !isMySqlServiceRunning && (i <= MysqlServiceRetryAttempts); i++)
{
Log.Error("MySQL Service was not found running at localhost!");
Log.Warn($"Trying to start MySQL service...Retry attempt: {i}.");
HostCheck.StartService(MysqlServiceName, MysqlServiceStartTimeoutMs);
isMySqlServiceRunning = HostCheck.IsServiceRunning(MysqlServiceName);
if (!isMySqlServiceRunning)
continue;
Log.Info("MySQL Service started!");
break;
}
if (isMySqlServiceRunning)
Log.Info("MySQL Service running at localhost.");
else
Log.Error("MySQL Service was not found running at localhost!");
return isMySqlServiceRunning;
}
Log.Info("Database host NOT running at localhost. MySQL Service check skipped.");
return true;
}
private bool CheckDatabaseQuery()
{
Log.Info("Checking if query to database works...");
bool isQuerySuccessful = TryQueryDatabase();
if (isQuerySuccessful)
Log.Info($"Query to database '{_database}' was SUCCESSFUL!");
else
Log.Error($"Query to database '{_database}' has FAILED!");
return isQuerySuccessful;
}
private bool TryQueryDatabase()
{
try
{
return Db.Query("SELECT 1").Any();
}
catch (MySqlException ex)
{
Log.Error($"Method: {nameof(TryQueryDatabase)}. Message: '{ex.Message}' (Error Number: '{ex.Number}')");
}
return false;
}
}
}