Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CheckHealth async #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/DrHouse.Directory/DirectoryHealthDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Threading.Tasks;

namespace DrHouse.Directory
{
Expand Down Expand Up @@ -41,19 +42,25 @@ public void AddDirectoryDependency(string directoryPath, FileSystemRights[] fils
}
}

public HealthData CheckHealth()
public async Task<HealthData> CheckHealthAsync()
{
HealthData directoryHealthData = new HealthData(_windowsPrincipal.Identity.Name);
directoryHealthData.Type = "DirectoryPermission";

try
{
List<Task<HealthData>> taskList = new List<Task<HealthData>>();
foreach (string directoryPath in _fileSystemRigths.Keys)
{
HealthData directoryHealth = CheckFileRigthPermissions(directoryPath, _fileSystemRigths[directoryPath]);
directoryHealthData.DependenciesStatus.Add(directoryHealth);
Task<HealthData> checkTask = new Task<HealthData>(() =>
CheckFileRigthPermissions(directoryPath, _fileSystemRigths[directoryPath]));
checkTask.Start();
taskList.Add(checkTask);
}

HealthData[] taskResults = await Task.WhenAll(taskList);
directoryHealthData.DependenciesStatus.AddRange(taskResults);

directoryHealthData.IsOK = true;
}
catch (Exception ex)
Expand Down
32 changes: 18 additions & 14 deletions src/DrHouse.SqlServer/SqlServerHealthDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading.Tasks;
using DrHouse.Events;

namespace DrHouse.SqlServer
Expand Down Expand Up @@ -50,7 +51,7 @@ public void AddIndexDependency(string tableName, string indexName)
_indexes.Add(new Index { TableName = tableName, IndexName = indexName });
}

public HealthData CheckHealth()
public async Task<HealthData> CheckHealthAsync()
{
HealthData sqlHealthData = new HealthData(_databaseName);
sqlHealthData.Type = "SqlServer";
Expand All @@ -61,24 +62,27 @@ public HealthData CheckHealth()
{
if (sqlConnection.State != System.Data.ConnectionState.Open)
{
sqlConnection.Open();
await sqlConnection.OpenAsync();
}

sqlHealthData = new HealthData(sqlConnection.Database);
sqlHealthData.Type = "SqlServer";

List<Task<HealthData>> taskList = new List<Task<HealthData>>();
foreach (string tableName in _permissions.Keys)
{
HealthData tableHealth = CheckTablePermissions(tableName, _permissions[tableName], sqlConnection);
sqlHealthData.DependenciesStatus.Add(tableHealth);
taskList.Add(CheckTablePermissionsAsync(tableName, _permissions[tableName], sqlConnection));
}

foreach (Index ix in _indexes)
{
HealthData indexHealth = CheckIndex(ix, sqlConnection);
sqlHealthData.DependenciesStatus.Add(indexHealth);
taskList.Add(CheckIndexAsync(ix, sqlConnection));
}

HealthData[] taskResults = await Task.WhenAll(taskList);

sqlHealthData.DependenciesStatus.AddRange(taskResults);

sqlHealthData.IsOK = true;
}
}
Expand All @@ -93,7 +97,7 @@ public HealthData CheckHealth()
return sqlHealthData;
}

private HealthData CheckTablePermissions(string tableName, ICollection<TablePermission> permissions, SqlConnection sqlConnection)
private async Task<HealthData> CheckTablePermissionsAsync(string tableName, ICollection<TablePermission> permissions, SqlConnection sqlConnection)
{
HealthData tableHealth = new HealthData(tableName);

Expand All @@ -103,7 +107,7 @@ private HealthData CheckTablePermissions(string tableName, ICollection<TablePerm
{
HealthData tablePermissionHealth = new HealthData(permission.Permission.ToString());

tablePermissionHealth.IsOK = CheckPermission(permission, sqlConnection);
tablePermissionHealth.IsOK = await CheckPermissionAsync(permission, sqlConnection);
if(tablePermissionHealth.IsOK == false)
{
tablePermissionHealth.ErrorMessage = "Does not have permission.";
Expand All @@ -125,7 +129,7 @@ private HealthData CheckTablePermissions(string tableName, ICollection<TablePerm
return tableHealth;
}

private bool CheckPermission(TablePermission permission, SqlConnection sqlConnection)
private async Task<bool> CheckPermissionAsync(TablePermission permission, SqlConnection sqlConnection)
{
string query = @"SELECT HAS_PERMS_BY_NAME (@tableName, 'OBJECT', @permission)";
var permissionCmd = new SqlCommand(query);
Expand All @@ -134,16 +138,16 @@ private bool CheckPermission(TablePermission permission, SqlConnection sqlConnec

permissionCmd.Connection = sqlConnection;

var reader = permissionCmd.ExecuteReader();
reader.Read();
SqlDataReader reader = await permissionCmd.ExecuteReaderAsync();
await reader.ReadAsync();

bool result = (int)reader[0] == 1;
reader.Close();

return result;
}

private HealthData CheckIndex(Index index, SqlConnection sqlConnection)
private async Task<HealthData> CheckIndexAsync(Index index, SqlConnection sqlConnection)
{
HealthData tableHealth = new HealthData(index.IndexName);

Expand All @@ -158,9 +162,9 @@ private HealthData CheckIndex(Index index, SqlConnection sqlConnection)
permissionCmd.Connection = sqlConnection;

bool result = false;
using (var reader = permissionCmd.ExecuteReader())
using (var reader = await permissionCmd.ExecuteReaderAsync())
{
reader.Read();
await reader.ReadAsync();

// If there is at lease one, return success
result = (int)reader[0] > 0;
Expand Down
7 changes: 5 additions & 2 deletions src/DrHouse.Telnet/TelnetHealthDependency.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using DrHouse.Core;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using DrHouse.Events;

namespace DrHouse.Telnet
Expand Down Expand Up @@ -30,7 +32,7 @@ public TelnetHealthDependency(string hostname, int port, string serviceName = nu

public event EventHandler<DependencyExceptionEvent> OnDependencyException;

public HealthData CheckHealth()
public async Task<HealthData> CheckHealthAsync()
{
HealthData healthData = new HealthData($"{_hostname}:{_port} [{_serviceName ?? ""}]");
healthData.Type = "Telnet";
Expand All @@ -39,7 +41,8 @@ public HealthData CheckHealth()
{
using (Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp))
{
socket.Connect(_hostname, _port);
await Task.Factory.FromAsync(
socket.BeginConnect, socket.EndConnect, _hostname, _port, null);

if (socket.Connected)
{
Expand Down
23 changes: 15 additions & 8 deletions src/DrHouse/HealthChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DrHouse.Events;

namespace DrHouse.Core
Expand All @@ -25,19 +26,25 @@ public void AddDependency(IHealthDependency dependency)
}

public HealthData CheckHealth()
{
return CheckHealthAsync().Result;
}

public async Task<HealthData> CheckHealthAsync()
{
HealthData healthData = new HealthData(_appName);

try
{
ConcurrentBag<HealthData> healthDataCollection = new ConcurrentBag<HealthData>();

_healthDependencyCollection.AsParallel().ForAll(dep =>
List<Task<HealthData>> checkTasks = new List<Task<HealthData>>();
foreach (IHealthDependency dependency in _healthDependencyCollection)
{
healthDataCollection.Add(CheckDependency(dep));
});
checkTasks.Add(CheckDependency(dependency));
}

HealthData[] results = await Task.WhenAll(checkTasks.ToList());

healthData.DependenciesStatus.AddRange(healthDataCollection);
healthData.DependenciesStatus.AddRange(results);
healthData.IsOK = true;
}
catch (Exception ex)
Expand All @@ -51,7 +58,7 @@ public HealthData CheckHealth()
return healthData;
}

private HealthData CheckDependency(IHealthDependency dependency)
private async Task<HealthData> CheckDependency(IHealthDependency dependency)
{
try
{
Expand All @@ -60,7 +67,7 @@ private HealthData CheckDependency(IHealthDependency dependency)
OnDependencyException?.Invoke(this, e);
};

return dependency.CheckHealth();
return await dependency.CheckHealthAsync();
}
catch (Exception ex)
{
Expand Down
3 changes: 2 additions & 1 deletion src/DrHouse/IHealthDependency.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Threading.Tasks;
using DrHouse.Events;

namespace DrHouse.Core
{
public interface IHealthDependency
{
HealthData CheckHealth();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not remove this method from the contract, just add the new one.

Task<HealthData> CheckHealthAsync();

HealthData CheckHealth(Action check);

Expand Down