module Handler.Health where import Import import qualified Data.Aeson.Encode.Pretty as Aeson import qualified Data.Text.Lazy.Builder as Builder import Utils.Lens import qualified Data.UUID as UUID getHealthR :: Handler TypedContent getHealthR = do healthReport' <- liftIO . readTVarIO =<< getsYesod appHealthReport let handleMissing = do interval <- getsYesod $ round . (* 1e6) . toRational . view _appHealthCheckInterval reportStore <- getsYesod appHealthReport waitResult <- threadDelay interval `race` atomically (readTVar reportStore >>= guard . is _Just) case waitResult of Left () -> fail "System is not generating HealthReports" Right _ -> redirect HealthR (lastUpdated, healthReport) <- maybe handleMissing return healthReport' interval <- getsYesod $ view _appHealthCheckInterval instanceId <- getsYesod appInstanceID setWeakEtagHashable (instanceId, lastUpdated) expiresAt $ interval `addUTCTime` lastUpdated setLastModified lastUpdated let status | HealthSuccess <- classifyHealthReport healthReport = ok200 | otherwise = internalServerError500 sendResponseStatus status <=< selectRep $ do provideRep . siteLayoutMsg MsgHealthReport $ do setTitleI MsgHealthReport let HealthReport{..} = healthReport [whamlet| $newline never