package com.hubspot.singularity;

import javax.inject.Inject;

import com.codahale.metrics.health.HealthCheck;
import com.hubspot.singularity.mesos.SingularityMesosSchedulerDelegator;

public class SingularityDropwizardHealthcheck extends HealthCheck {

  private final SingularityMesosSchedulerDelegator mesosScheduler;

  @Inject
  public SingularityDropwizardHealthcheck(final SingularityMesosSchedulerDelegator mesosScheduler) {
    this.mesosScheduler = mesosScheduler;
  }

  @Override
  protected Result check() throws Exception {
    return mesosScheduler.isRunning() ? Result.healthy() : Result.unhealthy("scheduler not running");
  }

}

@@ -38,27 +38,158 @@
 ort 
-javax.inject.Inject
+static com.google.common.base.Preconditions.checkNotNull;%0A%0Aimport javax.inject.Inject;%0A%0Aimport org.apache.curator.framework.recipes.leader.LeaderLatch
 ;%0A%0Ai
@@ -444,16 +444,57 @@
 heduler;
+%0A  private final LeaderLatch leaderLatch;
 %0A%0A  @Inj
@@ -594,16 +594,47 @@
 cheduler
+, final LeaderLatch leaderLatch
 ) %7B%0A    
@@ -655,16 +655,29 @@
 duler = 
+checkNotNull(
 mesosSch
@@ -678,24 +678,124 @@
 sosScheduler
+, %22mesosScheduler is null%22);%0A    this.leaderLatch = checkNotNull(leaderLatch, %22leaderLatch is null%22)
 ;%0A  %7D%0A%0A  @Ov
@@ -847,16 +847,57 @@
 ption %7B%0A
+    if (leaderLatch.hasLeadership()) %7B%0A  
     retu
@@ -943,16 +943,51 @@
 healthy(
+%22Leading and scheduler is running.%22
 ) : Resu
@@ -1000,16 +1000,29 @@
 ealthy(%22
+Leading, but 
 schedule
@@ -1023,16 +1023,19 @@
 heduler 
+is 
 not runn
@@ -1037,19 +1037,173 @@
  running
+!
 %22);%0A  
+  %7D else %7B%0A      return mesosScheduler.isRunning() ? Result.unhealthy(%22Not leading, but scheduler is running!%22) : Result.healthy(%22Not leading.%22);%0A    %7D%0A  
 %7D
-%0A
 %0A%7D%0A

Fix Dropwizard healthcheck.