This repository has been archived by the owner on Jul 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from rogeralsing/dev
Dev
- Loading branch information
Showing
29 changed files
with
190 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Consul; | ||
|
||
namespace Microphone.Core | ||
{ | ||
public static class Cluster | ||
{ | ||
private static string ServiceName; | ||
private static string ServiceId; | ||
|
||
public static string GetConfig() | ||
{ | ||
var client = new Client(); | ||
var key = "ServiceConfig:" + ServiceName; | ||
var response = client.KV.Get(key); | ||
var res = Encoding.UTF8.GetString(response.Response.Value); | ||
return res; | ||
} | ||
|
||
public static ServiceInformation[] FindService(string name) | ||
{ | ||
Logger.Information("{ServiceName} lookup {OtherServiceName}", ServiceName, name); | ||
var client = new Client(); | ||
var others = client.Health.Service(name, null, true); | ||
|
||
return | ||
others.Response.Select(other => new ServiceInformation(other.Service.Address, other.Service.Port)) | ||
.ToArray(); | ||
} | ||
|
||
public static void RegisterService(string serviceName, string serviceId, string version, Uri uri) | ||
{ | ||
ServiceName = serviceName; | ||
ServiceId = serviceId; | ||
var client = new Client(); | ||
client.Agent.ServiceRegister(new AgentServiceRegistration | ||
{ | ||
Address = uri.Host, | ||
ID = serviceId, | ||
Name = serviceName, | ||
Port = uri.Port, | ||
Tags = new[] {version}, | ||
Check = new AgentServiceCheck | ||
{ | ||
HTTP = uri + "status", | ||
Interval = TimeSpan.FromSeconds(1), | ||
TTL = TimeSpan.Zero, | ||
Timeout = TimeSpan.Zero | ||
} | ||
}); | ||
StartReaper(); | ||
} | ||
|
||
private static void StartReaper() | ||
{ | ||
Task.Factory.StartNew(async () => | ||
{ | ||
await Task.Delay(1000).ConfigureAwait(false); | ||
Logger.Information("Reaper: started.."); | ||
var client = new Client(); | ||
var lookup = new HashSet<string>(); | ||
while (true) | ||
{ | ||
try | ||
{ | ||
var checks = client.Agent.Checks(); | ||
foreach (var check in checks.Response) | ||
{ | ||
if (Equals(check.Value.Status, CheckStatus.Critical)) | ||
{ | ||
//dont delete new services | ||
if (lookup.Contains(check.Value.ServiceID)) | ||
{ | ||
client.Agent.ServiceDeregister(check.Value.ServiceID); | ||
Logger.Information("Reaper: Removing {ServiceId}", check.Value.ServiceID); | ||
} | ||
else | ||
{ | ||
Logger.Information("Reaper: Marking {ServiceId}", check.Value.ServiceID); | ||
lookup.Add(check.Value.ServiceID); | ||
} | ||
|
||
} | ||
else | ||
{ | ||
//if service is ok, remove it from reaper set | ||
lookup.Remove(check.Value.ServiceID); | ||
} | ||
} | ||
} | ||
catch(Exception x) | ||
{ | ||
Logger.Error(x,"Crashed"); | ||
} | ||
|
||
await Task.Delay(5000).ConfigureAwait(false); | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
|
||
namespace Microphone.Core | ||
{ | ||
public static class Configuration | ||
{ | ||
|
||
|
||
public static Uri GetUri(int port = 0) | ||
{ | ||
port = port == 0 ? FreeTcpPort() : port; | ||
var uri = new Uri("http://localhost:" + port); | ||
return uri; | ||
} | ||
|
||
private static int FreeTcpPort() | ||
{ | ||
var l = new TcpListener(IPAddress.Loopback, 0); | ||
l.Start(); | ||
var port = ((IPEndPoint)l.LocalEndpoint).Port; | ||
l.Stop(); | ||
return port; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
del *.nupkg | ||
nuget pack Microphone.Core.csproj -IncludeReferencedProjects -Prop Configuration=Release | ||
nuget push Microphone.Core.0.1.0.0.nupkg | ||
nuget push Microphone.Core.0.1.3.0.nupkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="Consul" version="0.5.2.65" targetFramework="net46" /> | ||
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net46" /> | ||
<package id="Serilog" version="1.5.11" targetFramework="net46" /> | ||
</packages> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,17 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Consul; | ||
using Microphone.Core; | ||
using Microphone.Core; | ||
using Nancy; | ||
|
||
namespace Microphone.Nancy | ||
{ | ||
public abstract class AutoRegisterModule : NancyModule | ||
public class StatusModule : NancyModule | ||
{ | ||
private readonly Logger _logger; | ||
|
||
protected Logger Logger => _logger; | ||
|
||
protected AutoRegisterModule() | ||
public StatusModule() | ||
{ | ||
_logger = new Logger(); | ||
//Before += ctx => | ||
//{ | ||
// _logger.Information("{Url} - {Method}", ctx.Request.Url , ctx.Request.Method ); | ||
// return null; | ||
//}; | ||
|
||
Get["/status"] = _ => | ||
{ | ||
Logger.Information("OK"); | ||
return "ok"; | ||
}; | ||
} | ||
|
||
protected string GetConfig() | ||
{ | ||
var client = new Client(); | ||
var key = "ServiceConfig:" + Bootstrap.ServiceName; | ||
var response = client.KV.Get(key); | ||
var res = System.Text.Encoding.UTF8.GetString(response.Response.Value); | ||
return res; | ||
} | ||
|
||
protected ServiceInformation[] FindService(string name) | ||
{ | ||
Logger.Information("{ServiceName} lookup {OtherServiceName}",Bootstrap.ServiceName,name); | ||
var client = new Client(); | ||
var others = client.Catalog.Service(name); | ||
|
||
return | ||
others.Response.Select(other => new ServiceInformation(other.ServiceAddress, other.ServicePort)) | ||
.ToArray(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.