Skip to content

Commit 9dab6dc

Browse files
committed
Add health API
1 parent 3e54590 commit 9dab6dc

File tree

15 files changed

+168
-12
lines changed

15 files changed

+168
-12
lines changed

IntegrationEngine.Core/Mail/IMailClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public interface IMailClient
88
MailConfiguration MailConfiguration { get; set; }
99
SmtpClient SmtpClient { get; set; }
1010
void Send(MailMessage mailMessage);
11+
bool IsServerAvailable();
1112
}
1213
}
1314

IntegrationEngine.Core/Mail/MailClient.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using IntegrationEngine.Core.Configuration;
33
using System;
44
using System.Net.Mail;
5+
using System.Net.Sockets;
6+
using System.IO;
57

68
namespace IntegrationEngine.Core.Mail
79
{
@@ -22,11 +24,11 @@ public MailClient (ILog log) : this()
2224
public void Send(MailMessage mailMessage)
2325
{
2426
ConfigureSmtpClient();
25-
try
27+
try
2628
{
2729
SmtpClient.Send(mailMessage);
2830
}
29-
catch (Exception exception)
31+
catch (Exception exception)
3032
{
3133
Log.Error(exception);
3234
}
@@ -39,6 +41,36 @@ void ConfigureSmtpClient()
3941
SmtpClient.Host = MailConfiguration.HostName;
4042
SmtpClient.Port = MailConfiguration.Port;
4143
}
44+
45+
public bool IsServerAvailable()
46+
{
47+
try
48+
{
49+
using (var client = new TcpClient())
50+
{
51+
client.Connect(MailConfiguration.HostName, MailConfiguration.Port);
52+
using (var stream = client.GetStream())
53+
{
54+
using (var writer = new StreamWriter(stream))
55+
using (var reader = new StreamReader(stream))
56+
{
57+
writer.WriteLine("EHLO " + MailConfiguration.HostName);
58+
writer.Flush();
59+
var response = reader.ReadLine();
60+
Log.Debug(x => x("Mail server status: {0}", response));
61+
if (response != null)
62+
return true;
63+
}
64+
}
65+
}
66+
}
67+
catch(SocketException exception)
68+
{
69+
Log.Error(exception);
70+
return false;
71+
}
72+
return false;
73+
}
4274
}
4375
}
4476

IntegrationEngine.Core/Storage/ESRepository.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using IntegrationEngine.Model;
1+
using Common.Logging;
2+
using IntegrationEngine.Model;
23
using Nest;
34
using System;
45
using System.Collections.Generic;
@@ -8,7 +9,8 @@ namespace IntegrationEngine.Core.Storage
89
{
910
public class ESRepository<T> : IRepository<T> where T : class, IHasStringId
1011
{
11-
public ElasticClient ElasticClient{ get; set; }
12+
public IElasticClient ElasticClient { get; set; }
13+
public ILog Log { get; set; }
1214

1315
public ESRepository()
1416
{
@@ -73,6 +75,19 @@ public void Dispose()
7375
{
7476
throw new NotImplementedException();
7577
}
78+
79+
public bool IsServerAvailable()
80+
{
81+
try
82+
{
83+
return ElasticClient.Ping(new PingRequest()).ConnectionStatus.Success;
84+
}
85+
catch(Exception exception)
86+
{
87+
Log.Error(exception);
88+
return false;
89+
}
90+
}
7691
}
7792
}
7893

IntegrationEngine.Core/Storage/IRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ public interface IRepository<T> where T : class
1818
bool Exists(object id);
1919
void SetState(T value, EntityState entityState);
2020
void Dispose();
21+
bool IsServerAvailable();
2122
}
2223
}

IntegrationEngine.Core/Storage/Repository.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,10 @@ public void Dispose()
6969
{
7070
db.Dispose();
7171
}
72+
73+
public bool IsServerAvailable()
74+
{
75+
throw new NotImplementedException();
76+
}
7277
}
7378
}

IntegrationEngine.Model.net40/IntegrationEngine.Model.net40.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
<Compile Include="..\IntegrationEngine.Model\TimeZone.cs">
6969
<Link>TimeZone.cs</Link>
7070
</Compile>
71+
<Compile Include="..\IntegrationEngine.Model\HealthStatus.cs">
72+
<Link>HealthStatus.cs</Link>
73+
</Compile>
74+
<Compile Include="..\IntegrationEngine.Model\IHealthStatus.cs">
75+
<Link>IHealthStatus.cs</Link>
76+
</Compile>
7177
</ItemGroup>
7278
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7379
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace IntegrationEngine.Model
4+
{
5+
public class HealthStatus : IHealthStatus
6+
{
7+
public bool IsMailServerAvailable { get; set; }
8+
public bool IsMessageQueueServerAvailable { get; set; }
9+
public bool IsElasticsearchServerAvailable { get; set; }
10+
11+
public HealthStatus()
12+
{}
13+
}
14+
}
15+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace IntegrationEngine.Model
4+
{
5+
public interface IHealthStatus
6+
{
7+
bool IsMailServerAvailable { get; set; }
8+
bool IsMessageQueueServerAvailable { get; set; }
9+
bool IsElasticsearchServerAvailable { get; set; }
10+
}
11+
}
12+

IntegrationEngine.Model/IntegrationEngine.Model.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
<Compile Include="IIntegrationJobTrigger.cs" />
5555
<Compile Include="TimeZone.cs" />
5656
<Compile Include="ITimeZone.cs" />
57+
<Compile Include="HealthStatus.cs" />
58+
<Compile Include="IHealthStatus.cs" />
5759
</ItemGroup>
5860
<ItemGroup>
5961
<None Include="package.nuspec">
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using IntegrationEngine.Scheduler;
2+
using IntegrationEngine.Core.Mail;
3+
using IntegrationEngine.Core.Storage;
4+
using IntegrationEngine.MessageQueue;
5+
using InEngineHealthStatus = IntegrationEngine.Model.HealthStatus;
6+
using Nest;
7+
using System;
8+
using System.Linq;
9+
using System.Collections.Generic;
10+
using System.Web.Http;
11+
using System.Web.Http.Description;
12+
13+
14+
namespace IntegrationEngine.Api.Controllers
15+
{
16+
public class HealthStatusController : ApiController
17+
{
18+
public IMailClient MailClient { get; set; }
19+
public IMessageQueueClient MessageQueueClient { get; set; }
20+
public ESRepository<CronTrigger> ESRepository { get; set; }
21+
22+
public HealthStatusController()
23+
{}
24+
25+
public HealthStatusController(IMailClient mailClient, IMessageQueueClient messageQueueClient, ESRepository<CronTrigger> esRepository)
26+
: this()
27+
{
28+
MailClient = mailClient;
29+
MessageQueueClient = messageQueueClient;
30+
ESRepository = esRepository;
31+
}
32+
33+
[ResponseType(typeof(InEngineHealthStatus))]
34+
public IHttpActionResult GetHealthStatus()
35+
{
36+
return Ok(new InEngineHealthStatus() {
37+
IsMailServerAvailable = MailClient.IsServerAvailable(),
38+
IsMessageQueueServerAvailable = MessageQueueClient.IsServerAvailable(),
39+
IsElasticsearchServerAvailable = ESRepository.IsServerAvailable(),
40+
});
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)