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
_{MsgHealthMatchingClusterConfig}
#{boolSymbol healthMatchingClusterConfig} $maybe httpReachable <- healthHTTPReachable
_{MsgHealthHTTPReachable}
#{boolSymbol httpReachable} $maybe ldapAdmins <- healthLDAPAdmins
_{MsgHealthLDAPAdmins}
#{textPercent ldapAdmins} $maybe smtpConnect <- healthSMTPConnect
_{MsgHealthSMTPConnect}
#{boolSymbol smtpConnect} $maybe widgetMemcached <- healthWidgetMemcached
_{MsgHealthWidgetMemcached}
#{boolSymbol widgetMemcached} |] provideJson healthReport provideRep . return . Builder.toLazyText $ Aeson.encodePrettyToTextBuilder healthReport getInstanceR :: Handler TypedContent getInstanceR = do instanceInfo@(clusterId, instanceId) <- getsYesod $ (,) <$> appClusterID <*> appInstanceID setWeakEtagHashable (clusterId, instanceId) selectRep $ do provideRep $ siteLayoutMsg MsgInstanceIdentification $ do setTitleI MsgInstanceIdentification [whamlet| $newline never
_{MsgClusterId}
#{UUID.toText clusterId}
_{MsgInstanceId}
#{UUID.toText instanceId} |] provideJson instanceInfo provideRep . return $ tshow instanceInfo