Repository: hasura/graphql-engine
File: server/src-lib/Hasura/App.hs (Haskell)

Code before:
runHGEServer
  :: ( HasVersion
     , MonadIO m
     , MonadStateless IO m
     , UserAuthentication m
     , MetadataApiAuthorization m
     , HttpLog m
     , ConsoleRenderer m
     , LA.Forall (LA.Pure m)
     )
  => ServeOptions impl
  -> InitCtx
  -> UTCTime
  -- ^ start time
  -> m ()
runHGEServer ServeOptions{..} InitCtx{..} initTime = do
  let sqlGenCtx = SQLGenCtx soStringifyNum
      Loggers loggerCtx logger _ = _icLoggers

  authModeRes <- runExceptT $ mkAuthMode soAdminSecret soAuthHook soJwtSecret soUnAuthRole
                              _icHttpManager logger

  authMode <- either (printErrExit . T.unpack) return authModeRes

  schemaCache <- onNothing _icRebuildableSchemaCache
                 (printErrExit "expected schema cache to be initialised")

  HasuraApp app cacheRef cacheInitTime shutdownApp <- mkWaiApp soTxIso
                                                               logger
                                                               sqlGenCtx
                                                               soEnableAllowlist
                                                               _icPgPool
                                                               _icConnInfo
                                                               _icHttpManager
                                                               authMode
                                                               soCorsConfig
                                                               soEnableConsole
                                                               soConsoleAssetsDir
                                                               soEnableTelemetry
                                                               _icInstanceId
                                                               soEnabledAPIs
                                                               soLiveQueryOpts
                                                               soPlanCacheOptions
                                                               schemaCache

  -- log inconsistent schema objects
  inconsObjs <- scInconsistentObjs <$> liftIO (getSCFromRef cacheRef)
  liftIO $ logInconsObjs logger inconsObjs

  -- start a background thread for schema sync
  startSchemaSync sqlGenCtx _icPgPool logger _icHttpManager

Review comment (performance):
I would probably just pass the schema cache as a separate argument to this function rather than include it in the `InitCtx` wrapped in a `Maybe`. That way the type system can prevent this error from ever happening.

Code after:
  => ServeOptions impl
  -> InitCtx
  -> UTCTime
  -- ^ start time
  -> m ()
runHGEServer ServeOptions{..} InitCtx{..} initTime = do
  -- Comment this to enable expensive assertions from "GHC.AssertNF". These will log lines to
  -- STDOUT containing "not in normal form". In the future we could try to integrate this into
  -- our tests. For now this is a development tool.
  --
  -- NOTE: be sure to compile WITHOUT code coverage, for this to work properly.
  liftIO disableAssertNF

  let sqlGenCtx = SQLGenCtx soStringifyNum
      Loggers loggerCtx logger _ = _icLoggers

  authModeRes <- runExceptT $ mkAuthMode soAdminSecret soAuthHook soJwtSecret soUnAuthRole
                              _icHttpManager logger

  authMode <- either (printErrExit . T.unpack) return authModeRes

  HasuraApp app cacheRef cacheInitTime shutdownApp <- mkWaiApp soTxIso
                                                               logger
                                                               sqlGenCtx
                                                               soEnableAllowlist
                                                               _icPgPool
                                                               _icConnInfo
                                                               _icHttpManager
                                                               authMode
                                                               soCorsConfig
                                                               soEnableConsole
                                                               soConsoleAssetsDir
                                                               soEnableTelemetry
                                                               _icInstanceId
                                                               soEnabledAPIs
                                                               soLiveQueryOpts
                                                               soPlanCacheOptions

  -- log inconsistent schema objects
  inconsObjs <- scInconsistentObjs <$> liftIO (getSCFromRef cacheRef)
  liftIO $ logInconsObjs logger inconsObjs

  -- start background threads for schema sync
  (_schemaSyncListenerThread, _schemaSyncProcessorThread) <-
    startSchemaSyncThreads sqlGenCtx _icPgPool logger _icHttpManager
                           cacheRef _icInstanceId cacheInitTime

  let warpSettings = Warp.setPort soPort
                     . Warp.setHost soHost
                     . Warp.setGracefulShutdownTimeout (Just 30) -- 30s graceful shutdown
                     . Warp.setInstallShutdownHandler (shutdownHandler logger shutdownApp)