Last active
May 14, 2018 18:59
-
-
Save Aaronontheweb/e0622409e95fcf88a4004f56f9311e2b to your computer and use it in GitHub Desktop.
Akka.Cluster HTTP HealthCheck
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
public class HomeController : Controller | |
{ | |
private readonly Cluster _cluster; | |
public HomeController() | |
{ | |
_cluster = Cluster.Get(SystemActors.ActorSystem); | |
} | |
public IActionResult Index() | |
{ | |
return View(); | |
} | |
public IActionResult HealthCheck() | |
{ | |
var state = _cluster.State; | |
var atLeastOneAvailableLighthouse = state.Members | |
.Any(x => x.HasRole("lighthouse") | |
&& x.Status == MemberStatus.Up | |
&& !state.Unreachable.Contains(x)); | |
var statusCode = atLeastOneAvailableLighthouse | |
? HttpStatusCode.OK | |
: HttpStatusCode.InternalServerError; | |
var sb = new StringBuilder(); | |
sb.AppendLine("Members: "); | |
foreach (var member in state.Members) | |
{ | |
sb.AppendFormat("{0}[{1}] Roles=[{2}], Reachable={3}", member.Address, member.Status, | |
string.Join(",", member.Roles), !state.Unreachable.Contains(member)); | |
sb.AppendLine(); | |
} | |
return new ContentResult() | |
{ | |
Content = sb.ToString(), | |
StatusCode = (int) statusCode | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Basic internal HTTP healthcheck for determining if we're in a joinable cluster, via the "at least one reachable lighthouse node is a current member of the cluster" rule