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
case healthReport' of
Nothing -> 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
Just (lastUpdated, healthReport) -> do
rContent <- 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
interval <- getsYesod $ view _appHealthCheckInterval
expiresAt $ interval `addUTCTime` lastUpdated
addHeader "Last-Modified" $ formatRFC1123 lastUpdated
let
status
| HealthSuccess <- classifyHealthReport healthReport
= ok200
| otherwise
= internalServerError500
sendResponseStatus status rContent
getInstanceR :: Handler TypedContent
getInstanceR = do
instanceInfo@(clusterId, instanceId) <- getsYesod $ (,) <$> appClusterID <*> appInstanceID
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