Skip to content

Instantly share code, notes, and snippets.

@Aaronontheweb
Last active May 14, 2018 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aaronontheweb/e0622409e95fcf88a4004f56f9311e2b to your computer and use it in GitHub Desktop.
Save Aaronontheweb/e0622409e95fcf88a4004f56f9311e2b to your computer and use it in GitHub Desktop.
Akka.Cluster HTTP HealthCheck
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
};
}
}
@Aaronontheweb
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment