2019-11-26 15:14:21 +03:00
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
module Main where
|
2019-03-12 08:46:27 +03:00
|
|
|
|
2020-07-14 22:00:58 +03:00
|
|
|
import Control.Exception
|
2020-09-08 21:13:35 +03:00
|
|
|
import Data.Int (Int64)
|
2020-01-23 00:55:55 +03:00
|
|
|
import Data.Text.Conversions (convertText)
|
2020-09-08 21:13:35 +03:00
|
|
|
import Data.Time.Clock.POSIX (getPOSIXTime)
|
2020-01-23 00:55:55 +03:00
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
import Hasura.App
|
|
|
|
import Hasura.Logging (Hasura)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.Prelude
|
2020-11-03 21:01:33 +03:00
|
|
|
import Hasura.RQL.DDL.Metadata (fetchMetadataFromHdbTables)
|
2019-11-20 21:21:30 +03:00
|
|
|
import Hasura.RQL.DDL.Schema
|
2019-10-21 19:01:05 +03:00
|
|
|
import Hasura.RQL.Types
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.Server.Init
|
2020-02-07 14:03:12 +03:00
|
|
|
import Hasura.Server.Migrate (downgradeCatalog, dropCatalog)
|
2019-11-26 15:14:21 +03:00
|
|
|
import Hasura.Server.Version
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2020-07-14 22:00:58 +03:00
|
|
|
import qualified Data.ByteString.Char8 as BC
|
2019-11-26 15:14:21 +03:00
|
|
|
import qualified Data.ByteString.Lazy as BL
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as BLC
|
2020-07-14 22:00:58 +03:00
|
|
|
import qualified Data.Environment as Env
|
2019-11-26 15:14:21 +03:00
|
|
|
import qualified Database.PG.Query as Q
|
2020-07-15 13:40:48 +03:00
|
|
|
import qualified Hasura.Tracing as Tracing
|
2020-07-14 22:00:58 +03:00
|
|
|
import qualified System.Exit as Sys
|
2020-08-18 22:53:12 +03:00
|
|
|
import qualified System.Metrics as EKG
|
2020-11-03 21:01:33 +03:00
|
|
|
import qualified System.Posix.Signals as Signals
|
2020-08-18 22:53:12 +03:00
|
|
|
|
2019-03-12 08:46:27 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
main :: IO ()
|
2020-07-14 22:00:58 +03:00
|
|
|
main = do
|
|
|
|
tryExit $ do
|
|
|
|
args <- parseArgs
|
|
|
|
env <- Env.getEnvironment
|
|
|
|
unAppM (runApp env args)
|
|
|
|
where
|
|
|
|
tryExit io = try io >>= \case
|
|
|
|
Left (ExitException _code msg) -> BC.putStrLn msg >> Sys.exitFailure
|
|
|
|
Right r -> return r
|
2018-07-27 12:34:50 +03:00
|
|
|
|
2020-07-14 22:00:58 +03:00
|
|
|
runApp :: Env.Environment -> HGEOptions Hasura -> AppM ()
|
|
|
|
runApp env (HGEOptionsG rci hgeCmd) =
|
|
|
|
withVersion $$(getVersionFromEnvironment) $ case hgeCmd of
|
2019-11-26 15:14:21 +03:00
|
|
|
HCServe serveOptions -> do
|
2020-07-14 22:00:58 +03:00
|
|
|
(initCtx, initTime) <- initialiseCtx env hgeCmd rci
|
2020-11-03 21:01:33 +03:00
|
|
|
|
2020-09-08 19:19:52 +03:00
|
|
|
ekgStore <- liftIO do
|
|
|
|
s <- EKG.newStore
|
|
|
|
EKG.registerGcMetrics s
|
2020-11-03 21:01:33 +03:00
|
|
|
|
2020-09-08 21:13:35 +03:00
|
|
|
let getTimeMs :: IO Int64
|
|
|
|
getTimeMs = (round . (* 1000)) `fmap` getPOSIXTime
|
|
|
|
|
|
|
|
EKG.registerCounter "ekg.server_timestamp_ms" getTimeMs s
|
2020-09-08 19:19:52 +03:00
|
|
|
pure s
|
2020-11-03 21:01:33 +03:00
|
|
|
|
2020-07-14 22:00:58 +03:00
|
|
|
let shutdownApp = return ()
|
2020-07-16 16:19:42 +03:00
|
|
|
-- Catches the SIGTERM signal and initiates a graceful shutdown.
|
2020-06-16 20:44:59 +03:00
|
|
|
-- Graceful shutdown for regular HTTP requests is already implemented in
|
2020-06-03 00:27:14 +03:00
|
|
|
-- Warp, and is triggered by invoking the 'closeSocket' callback.
|
2020-06-16 20:44:59 +03:00
|
|
|
-- We only catch the SIGTERM signal once, that is, if the user hits CTRL-C
|
2020-06-03 00:27:14 +03:00
|
|
|
-- once again, we terminate the process immediately.
|
|
|
|
_ <- liftIO $ Signals.installHandler
|
|
|
|
Signals.sigTERM
|
|
|
|
(Signals.CatchOnce (shutdownGracefully initCtx))
|
|
|
|
Nothing
|
2020-08-18 22:53:12 +03:00
|
|
|
runHGEServer env serveOptions initCtx Nothing initTime shutdownApp Nothing ekgStore
|
2020-07-14 22:00:58 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
HCExport -> do
|
2020-07-14 22:00:58 +03:00
|
|
|
(initCtx, _) <- initialiseCtx env hgeCmd rci
|
2020-11-03 21:01:33 +03:00
|
|
|
res <- runTx' initCtx fetchMetadataFromHdbTables Q.ReadCommitted
|
2020-07-14 22:00:58 +03:00
|
|
|
either (printErrJExit MetadataExportError) printJSON res
|
2019-01-02 14:24:17 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
HCClean -> do
|
2020-07-14 22:00:58 +03:00
|
|
|
(initCtx, _) <- initialiseCtx env hgeCmd rci
|
2020-04-09 10:41:24 +03:00
|
|
|
res <- runTx' initCtx dropCatalog Q.ReadCommitted
|
2020-07-14 22:00:58 +03:00
|
|
|
either (printErrJExit MetadataCleanError) (const cleanSuccess) res
|
2019-01-02 14:24:17 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
HCExecute -> do
|
2020-07-14 22:00:58 +03:00
|
|
|
(InitCtx{..}, _) <- initialiseCtx env hgeCmd rci
|
2019-11-26 15:14:21 +03:00
|
|
|
queryBs <- liftIO BL.getContents
|
2019-04-17 19:29:39 +03:00
|
|
|
let sqlGenCtx = SQLGenCtx False
|
2020-07-14 22:00:58 +03:00
|
|
|
res <- runAsAdmin _icPgPool sqlGenCtx _icHttpManager $ do
|
|
|
|
schemaCache <- buildRebuildableSchemaCache env
|
2020-07-16 16:19:42 +03:00
|
|
|
execQuery env queryBs
|
2020-07-15 13:40:48 +03:00
|
|
|
& Tracing.runTraceTWithReporter Tracing.noReporter "execute"
|
2019-11-20 21:21:30 +03:00
|
|
|
& runHasSystemDefinedT (SystemDefined False)
|
|
|
|
& runCacheRWT schemaCache
|
2020-01-30 02:03:49 +03:00
|
|
|
& fmap (\(res, _, _) -> res)
|
2020-07-14 22:00:58 +03:00
|
|
|
either (printErrJExit ExecuteProcessError) (liftIO . BLC.putStrLn) res
|
2019-01-28 16:55:28 +03:00
|
|
|
|
2020-02-07 14:03:12 +03:00
|
|
|
HCDowngrade opts -> do
|
2020-07-14 22:00:58 +03:00
|
|
|
(InitCtx{..}, initTime) <- initialiseCtx env hgeCmd rci
|
2020-02-07 14:03:12 +03:00
|
|
|
let sqlGenCtx = SQLGenCtx False
|
|
|
|
res <- downgradeCatalog opts initTime
|
|
|
|
& runAsAdmin _icPgPool sqlGenCtx _icHttpManager
|
2020-07-14 22:00:58 +03:00
|
|
|
either (printErrJExit DowngradeProcessError) (liftIO . print) res
|
2020-02-07 14:03:12 +03:00
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
HCVersion -> liftIO $ putStrLn $ "Hasura GraphQL Engine: " ++ convertText currentVersion
|
2019-11-26 15:14:21 +03:00
|
|
|
where
|
2020-04-09 10:41:24 +03:00
|
|
|
runTx' initCtx tx txIso =
|
|
|
|
liftIO $ runExceptT $ Q.runTx (_icPgPool initCtx) (txIso, Nothing) tx
|
2019-01-28 16:55:28 +03:00
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
cleanSuccess = liftIO $ putStrLn "successfully cleaned graphql-engine related data"
|