diff --git a/api/health/health.go b/api/health/health.go index e0b047bbe..f7d0b0c73 100644 --- a/api/health/health.go +++ b/api/health/health.go @@ -23,11 +23,17 @@ func New(healthStatus *health.Health) *Health { } } -func (h *Health) handleGetHealth(w http.ResponseWriter, req *http.Request) error { +func (h *Health) handleGetHealth(w http.ResponseWriter, _ *http.Request) error { acc, err := h.healthStatus.Status() if err != nil { return err } + + if !acc.Healthy { + w.WriteHeader(http.StatusServiceUnavailable) // Set the status to 503 + } else { + w.WriteHeader(http.StatusOK) // Set the status to 200 + } return utils.WriteJSON(w, acc) } diff --git a/comm/communicator.go b/comm/communicator.go index 8a2656363..7fbef2e22 100644 --- a/comm/communicator.go +++ b/comm/communicator.go @@ -119,9 +119,10 @@ func (c *Communicator) Sync(ctx context.Context, handler HandleBlockStream) { syncCount++ if shouldSynced() { + c.health.ChainSyncStatus(false) delay = syncInterval c.onceSynced.Do(func() { - c.health.ChainSynced() + c.health.ChainSyncStatus(true) close(c.syncedCh) }) } diff --git a/health/health.go b/health/health.go index 56252ae01..7d39a94ce 100644 --- a/health/health.go +++ b/health/health.go @@ -13,8 +13,8 @@ import ( ) type BlockIngestion struct { - BestBlock *thor.Bytes32 `json:"bestBlock"` - BestBlockTimestamp *time.Time `json:"bestBlockTimestamp"` + BestBlock *thor.Bytes32 `json:"bestBlock"` + BestBlockIngestionTimestamp *time.Time `json:"bestBlockIngestionTimestamp"` } type Status struct { @@ -43,8 +43,8 @@ func (h *Health) Status() (*Status, error) { defer h.lock.RUnlock() blockIngest := &BlockIngestion{ - BestBlock: h.bestBlockID, - BestBlockTimestamp: &h.newBestBlock, + BestBlock: h.bestBlockID, + BestBlockIngestionTimestamp: &h.newBestBlock, } // todo review time slots @@ -58,9 +58,9 @@ func (h *Health) Status() (*Status, error) { }, nil } -func (h *Health) ChainSynced() { +func (h *Health) ChainSyncStatus(syncStatus bool) { h.lock.Lock() defer h.lock.Unlock() - h.chainSynced = true + h.chainSynced = syncStatus }