2020-03-11 09:27:31 +03:00
|
|
|
{-# LANGUAGE StrictData #-} -- TODO project-wide, maybe. See #3941
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
2018-09-05 14:26:46 +03:00
|
|
|
module Hasura.Events.Lib
|
|
|
|
( initEventEngineCtx
|
2020-03-11 09:27:31 +03:00
|
|
|
, processEventQueue
|
2018-09-05 14:26:46 +03:00
|
|
|
, unlockAllEvents
|
|
|
|
, defaultMaxEventThreads
|
2018-10-30 18:20:18 +03:00
|
|
|
, defaultFetchIntervalMilliSec
|
2018-09-07 14:51:01 +03:00
|
|
|
, Event(..)
|
2018-09-05 14:26:46 +03:00
|
|
|
) where
|
|
|
|
|
2020-03-11 09:27:31 +03:00
|
|
|
import Control.Concurrent.Extended (sleep)
|
|
|
|
import Control.Concurrent.Async (wait, withAsync, async, link)
|
2018-09-05 14:26:46 +03:00
|
|
|
import Control.Concurrent.STM.TVar
|
2020-03-11 09:27:31 +03:00
|
|
|
import Control.Exception.Lifted (finally, mask_, try)
|
2020-03-05 20:59:26 +03:00
|
|
|
import Control.Monad.STM
|
2018-09-05 14:26:46 +03:00
|
|
|
import Data.Aeson
|
|
|
|
import Data.Aeson.Casing
|
|
|
|
import Data.Aeson.TH
|
|
|
|
import Data.Has
|
|
|
|
import Data.Int (Int64)
|
2020-03-05 20:59:26 +03:00
|
|
|
import Data.String
|
2018-09-24 14:50:11 +03:00
|
|
|
import Data.Time.Clock
|
2020-03-11 09:27:31 +03:00
|
|
|
import Data.Word
|
2018-09-05 14:26:46 +03:00
|
|
|
import Hasura.Events.HTTP
|
2019-08-23 11:57:19 +03:00
|
|
|
import Hasura.HTTP
|
2018-09-05 14:26:46 +03:00
|
|
|
import Hasura.Prelude
|
2018-11-23 16:02:46 +03:00
|
|
|
import Hasura.RQL.DDL.Headers
|
2018-09-05 14:26:46 +03:00
|
|
|
import Hasura.RQL.Types
|
2020-01-23 00:55:55 +03:00
|
|
|
import Hasura.Server.Version (HasVersion)
|
2018-09-05 14:26:46 +03:00
|
|
|
import Hasura.SQL.Types
|
|
|
|
|
2018-09-24 14:50:11 +03:00
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import qualified Data.CaseInsensitive as CI
|
2018-09-05 14:26:46 +03:00
|
|
|
import qualified Data.HashMap.Strict as M
|
|
|
|
import qualified Data.TByteString as TBS
|
|
|
|
import qualified Data.Text as T
|
2018-09-24 14:50:11 +03:00
|
|
|
import qualified Data.Text.Encoding as T
|
2018-10-26 19:28:03 +03:00
|
|
|
import qualified Data.Text.Encoding as TE
|
|
|
|
import qualified Data.Text.Encoding.Error as TE
|
2018-09-05 14:26:46 +03:00
|
|
|
import qualified Data.Time.Clock as Time
|
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
import qualified Hasura.Logging as L
|
2019-02-14 10:37:59 +03:00
|
|
|
import qualified Network.HTTP.Client as HTTP
|
|
|
|
import qualified Network.HTTP.Types as HTTP
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2018-10-26 19:28:03 +03:00
|
|
|
type Version = T.Text
|
|
|
|
|
|
|
|
invocationVersion :: Version
|
|
|
|
invocationVersion = "2"
|
|
|
|
|
|
|
|
type LogEnvHeaders = Bool
|
2018-09-05 14:26:46 +03:00
|
|
|
|
|
|
|
newtype EventInternalErr
|
|
|
|
= EventInternalErr QErr
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
instance L.ToEngineLog EventInternalErr L.Hasura where
|
|
|
|
toEngineLog (EventInternalErr qerr) = (L.LevelError, L.eventTriggerLogType, toJSON qerr)
|
2018-09-05 14:26:46 +03:00
|
|
|
|
|
|
|
data TriggerMeta
|
2019-03-25 20:10:52 +03:00
|
|
|
= TriggerMeta { tmName :: TriggerName }
|
|
|
|
deriving (Show, Eq)
|
2018-09-05 14:26:46 +03:00
|
|
|
|
|
|
|
$(deriveJSON (aesonDrop 2 snakeCase){omitNothingFields=True} ''TriggerMeta)
|
|
|
|
|
2019-02-07 15:37:28 +03:00
|
|
|
data DeliveryInfo
|
|
|
|
= DeliveryInfo
|
|
|
|
{ diCurrentRetry :: Int
|
|
|
|
, diMaxRetries :: Int
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
$(deriveJSON (aesonDrop 2 snakeCase){omitNothingFields=True} ''DeliveryInfo)
|
|
|
|
|
2020-03-11 09:27:31 +03:00
|
|
|
-- | Change data for a particular row
|
|
|
|
--
|
|
|
|
-- https://docs.hasura.io/1.0/graphql/manual/event-triggers/payload.html
|
2018-09-05 14:26:46 +03:00
|
|
|
data Event
|
|
|
|
= Event
|
2018-09-07 14:51:01 +03:00
|
|
|
{ eId :: EventId
|
2018-09-05 14:26:46 +03:00
|
|
|
, eTable :: QualifiedTable
|
|
|
|
, eTrigger :: TriggerMeta
|
|
|
|
, eEvent :: Value
|
2018-09-07 14:51:01 +03:00
|
|
|
, eTries :: Int
|
2018-09-05 14:26:46 +03:00
|
|
|
, eCreatedAt :: Time.UTCTime
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
2019-02-07 15:37:28 +03:00
|
|
|
$(deriveFromJSON (aesonDrop 1 snakeCase){omitNothingFields=True} ''Event)
|
|
|
|
|
|
|
|
newtype QualifiedTableStrict = QualifiedTableStrict
|
|
|
|
{ getQualifiedTable :: QualifiedTable
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
instance ToJSON QualifiedTableStrict where
|
|
|
|
toJSON (QualifiedTableStrict (QualifiedObject sn tn)) =
|
|
|
|
object [ "schema" .= sn
|
|
|
|
, "name" .= tn
|
2018-09-05 14:26:46 +03:00
|
|
|
]
|
|
|
|
|
2020-03-11 09:27:31 +03:00
|
|
|
-- | See 'Event'.
|
2019-02-07 15:37:28 +03:00
|
|
|
data EventPayload
|
|
|
|
= EventPayload
|
|
|
|
{ epId :: EventId
|
|
|
|
, epTable :: QualifiedTableStrict
|
|
|
|
, epTrigger :: TriggerMeta
|
|
|
|
, epEvent :: Value
|
|
|
|
, epDeliveryInfo :: DeliveryInfo
|
|
|
|
, epCreatedAt :: Time.UTCTime
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
$(deriveToJSON (aesonDrop 2 snakeCase){omitNothingFields=True} ''EventPayload)
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2019-01-28 09:12:52 +03:00
|
|
|
data WebhookRequest
|
|
|
|
= WebhookRequest
|
2018-10-26 19:28:03 +03:00
|
|
|
{ _rqPayload :: Value
|
|
|
|
, _rqHeaders :: Maybe [HeaderConf]
|
|
|
|
, _rqVersion :: T.Text
|
|
|
|
}
|
2019-01-28 09:12:52 +03:00
|
|
|
$(deriveToJSON (aesonDrop 3 snakeCase){omitNothingFields=True} ''WebhookRequest)
|
2018-10-26 19:28:03 +03:00
|
|
|
|
|
|
|
data WebhookResponse
|
|
|
|
= WebhookResponse
|
|
|
|
{ _wrsBody :: TBS.TByteString
|
|
|
|
, _wrsHeaders :: Maybe [HeaderConf]
|
|
|
|
, _wrsStatus :: Int
|
|
|
|
}
|
|
|
|
$(deriveToJSON (aesonDrop 4 snakeCase){omitNothingFields=True} ''WebhookResponse)
|
|
|
|
|
2019-02-14 10:37:59 +03:00
|
|
|
data ClientError = ClientError { _ceMessage :: TBS.TByteString}
|
|
|
|
$(deriveToJSON (aesonDrop 3 snakeCase){omitNothingFields=True} ''ClientError)
|
2018-10-26 19:28:03 +03:00
|
|
|
|
2019-02-14 10:37:59 +03:00
|
|
|
data Response = ResponseType1 WebhookResponse | ResponseType2 ClientError
|
2018-10-26 19:28:03 +03:00
|
|
|
|
|
|
|
instance ToJSON Response where
|
2019-02-22 15:25:36 +03:00
|
|
|
toJSON (ResponseType1 resp) = object
|
|
|
|
[ "type" .= String "webhook_response"
|
|
|
|
, "data" .= toJSON resp
|
|
|
|
, "version" .= invocationVersion
|
|
|
|
]
|
|
|
|
toJSON (ResponseType2 err) = object
|
|
|
|
[ "type" .= String "client_error"
|
|
|
|
, "data" .= toJSON err
|
|
|
|
, "version" .= invocationVersion
|
|
|
|
]
|
2018-10-26 19:28:03 +03:00
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
data Invocation
|
|
|
|
= Invocation
|
2018-09-07 14:51:01 +03:00
|
|
|
{ iEventId :: EventId
|
2018-10-26 19:28:03 +03:00
|
|
|
, iStatus :: Int
|
2019-01-28 09:12:52 +03:00
|
|
|
, iRequest :: WebhookRequest
|
2018-10-26 19:28:03 +03:00
|
|
|
, iResponse :: Response
|
2018-09-05 14:26:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data EventEngineCtx
|
|
|
|
= EventEngineCtx
|
2020-03-11 09:27:31 +03:00
|
|
|
{ _eeCtxEventThreadsCapacity :: TVar Int
|
2020-01-16 04:56:57 +03:00
|
|
|
, _eeCtxFetchInterval :: DiffTime
|
2018-09-05 14:26:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
defaultMaxEventThreads :: Int
|
|
|
|
defaultMaxEventThreads = 100
|
|
|
|
|
2020-01-16 04:56:57 +03:00
|
|
|
defaultFetchIntervalMilliSec :: Milliseconds
|
2018-10-30 18:20:18 +03:00
|
|
|
defaultFetchIntervalMilliSec = 1000
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2018-10-26 19:28:03 +03:00
|
|
|
retryAfterHeader :: CI.CI T.Text
|
|
|
|
retryAfterHeader = "Retry-After"
|
|
|
|
|
2020-01-16 04:56:57 +03:00
|
|
|
initEventEngineCtx :: Int -> DiffTime -> STM EventEngineCtx
|
2020-03-11 09:27:31 +03:00
|
|
|
initEventEngineCtx maxT _eeCtxFetchInterval = do
|
|
|
|
_eeCtxEventThreadsCapacity <- newTVar maxT
|
|
|
|
return $ EventEngineCtx{..}
|
|
|
|
|
|
|
|
-- | Service events from our in-DB queue.
|
|
|
|
--
|
|
|
|
-- There are a few competing concerns and constraints here; we want to...
|
|
|
|
-- - fetch events in batches for lower DB pressure
|
|
|
|
-- - don't fetch more than N at a time (since that can mean: space leak, less
|
|
|
|
-- effective scale out, possible double sends for events we've checked out
|
|
|
|
-- on exit (TODO clean shutdown procedure))
|
|
|
|
-- - try not to cause webhook workers to stall waiting on DB fetch
|
|
|
|
-- - limit webhook HTTP concurrency per HASURA_GRAPHQL_EVENTS_HTTP_POOL_SIZE
|
|
|
|
processEventQueue
|
2020-01-23 00:55:55 +03:00
|
|
|
:: (HasVersion) => L.Logger L.Hasura -> LogEnvHeaders -> HTTP.Manager-> Q.PGPool
|
2020-03-05 20:59:26 +03:00
|
|
|
-> IO SchemaCache -> EventEngineCtx
|
2020-03-11 09:27:31 +03:00
|
|
|
-> IO void
|
|
|
|
processEventQueue logger logenv httpMgr pool getSchemaCache EventEngineCtx{..} = do
|
|
|
|
events0 <- popEventsBatch
|
|
|
|
go events0 0 False
|
2018-09-05 14:26:46 +03:00
|
|
|
where
|
2020-03-11 09:27:31 +03:00
|
|
|
fetchBatchSize = 100
|
|
|
|
popEventsBatch = do
|
|
|
|
let run = runExceptT . Q.runTx pool (Q.RepeatableRead, Just Q.ReadWrite)
|
|
|
|
run (fetchEvents fetchBatchSize) >>= \case
|
|
|
|
Left err -> do
|
|
|
|
L.unLogger logger $ EventInternalErr err
|
|
|
|
return []
|
|
|
|
Right events ->
|
|
|
|
return events
|
|
|
|
|
|
|
|
-- work on this batch of events while prefetching the next. Recurse after we've forked workers
|
|
|
|
-- for each in the batch, minding the requested pool size.
|
|
|
|
go :: [Event] -> Int -> Bool -> IO void
|
|
|
|
go events !fullFetchCount !alreadyWarned = do
|
|
|
|
-- process events ASAP until we've caught up; only then can we sleep
|
|
|
|
when (null events) $ sleep _eeCtxFetchInterval
|
|
|
|
|
|
|
|
-- Prefetch next events payload while concurrently working through our current batch.
|
|
|
|
-- NOTE: we probably don't need to prefetch so early, but probably not
|
|
|
|
-- worth the effort for something more fine-tuned
|
|
|
|
eventsNext <- withAsync popEventsBatch $ \eventsNextA -> do
|
|
|
|
-- process approximately in order, minding HASURA_GRAPHQL_EVENTS_HTTP_POOL_SIZE:
|
|
|
|
forM_ events $ \event ->
|
|
|
|
mask_ $ do
|
|
|
|
atomically $ do -- block until < HASURA_GRAPHQL_EVENTS_HTTP_POOL_SIZE threads:
|
|
|
|
capacity <- readTVar _eeCtxEventThreadsCapacity
|
|
|
|
check $ capacity > 0
|
2020-03-18 04:31:22 +03:00
|
|
|
writeTVar _eeCtxEventThreadsCapacity $! (capacity - 1)
|
2020-03-11 09:27:31 +03:00
|
|
|
-- since there is some capacity in our worker threads, we can launch another:
|
|
|
|
let restoreCapacity = liftIO $ atomically $
|
|
|
|
modifyTVar' _eeCtxEventThreadsCapacity (+ 1)
|
|
|
|
t <- async $ flip runReaderT (logger, httpMgr) $
|
|
|
|
processEvent event `finally` restoreCapacity
|
|
|
|
link t
|
|
|
|
|
|
|
|
-- return when next batch ready; some 'processEvent' threads may be running.
|
|
|
|
wait eventsNextA
|
|
|
|
|
|
|
|
let lenEvents = length events
|
|
|
|
if | lenEvents == fetchBatchSize -> do
|
|
|
|
-- If we've seen N fetches in a row from the DB come back full (i.e. only limited
|
|
|
|
-- by our LIMIT clause), then we say we're clearly falling behind:
|
|
|
|
let clearlyBehind = fullFetchCount >= 3
|
|
|
|
unless alreadyWarned $
|
|
|
|
when clearlyBehind $
|
|
|
|
L.unLogger logger $ L.UnstructuredLog L.LevelWarn $ fromString $
|
|
|
|
"Events processor may not be keeping up with events generated in postgres, " <>
|
|
|
|
"or we're working on a backlog of events. Consider increasing " <>
|
|
|
|
"HASURA_GRAPHQL_EVENTS_HTTP_POOL_SIZE"
|
|
|
|
go eventsNext (fullFetchCount+1) (alreadyWarned || clearlyBehind)
|
|
|
|
|
|
|
|
| otherwise -> do
|
|
|
|
when (lenEvents /= fetchBatchSize && alreadyWarned) $
|
|
|
|
-- emit as warning in case users are only logging warning severity and saw above
|
|
|
|
L.unLogger logger $ L.UnstructuredLog L.LevelWarn $ fromString $
|
|
|
|
"It looks like the events processor is keeping up again."
|
|
|
|
go eventsNext 0 False
|
|
|
|
|
2020-03-05 20:59:26 +03:00
|
|
|
processEvent
|
|
|
|
:: ( HasVersion
|
|
|
|
, MonadReader r m
|
|
|
|
, Has HTTP.Manager r
|
|
|
|
, Has (L.Logger L.Hasura) r
|
|
|
|
, MonadIO m
|
|
|
|
)
|
|
|
|
=> Event -> m ()
|
|
|
|
processEvent e = do
|
|
|
|
cache <- liftIO getSchemaCache
|
|
|
|
let meti = getEventTriggerInfoFromEvent cache e
|
|
|
|
case meti of
|
|
|
|
Nothing -> do
|
|
|
|
logQErr $ err500 Unexpected "table or event-trigger not found in schema cache"
|
|
|
|
Just eti -> do
|
|
|
|
let webhook = T.unpack $ wciCachedValue $ etiWebhookInfo eti
|
|
|
|
retryConf = etiRetryConf eti
|
|
|
|
timeoutSeconds = fromMaybe defaultTimeoutSeconds (rcTimeoutSec retryConf)
|
|
|
|
responseTimeout = HTTP.responseTimeoutMicro (timeoutSeconds * 1000000)
|
|
|
|
headerInfos = etiHeaders eti
|
|
|
|
etHeaders = map encodeHeader headerInfos
|
|
|
|
headers = addDefaultHeaders etHeaders
|
|
|
|
ep = createEventPayload retryConf e
|
|
|
|
res <- runExceptT $ tryWebhook headers responseTimeout ep webhook
|
|
|
|
let decodedHeaders = map (decodeHeader logenv headerInfos) headers
|
2020-03-11 09:27:31 +03:00
|
|
|
either
|
2020-03-05 20:59:26 +03:00
|
|
|
(processError pool e retryConf decodedHeaders ep)
|
|
|
|
(processSuccess pool e decodedHeaders ep) res
|
2020-03-11 09:27:31 +03:00
|
|
|
>>= flip onLeft logQErr
|
2019-02-22 15:25:36 +03:00
|
|
|
|
|
|
|
createEventPayload :: RetryConf -> Event -> EventPayload
|
|
|
|
createEventPayload retryConf e = EventPayload
|
|
|
|
{ epId = eId e
|
|
|
|
, epTable = QualifiedTableStrict { getQualifiedTable = eTable e}
|
|
|
|
, epTrigger = eTrigger e
|
|
|
|
, epEvent = eEvent e
|
|
|
|
, epDeliveryInfo = DeliveryInfo
|
|
|
|
{ diCurrentRetry = eTries e
|
|
|
|
, diMaxRetries = rcNumRetries retryConf
|
|
|
|
}
|
|
|
|
, epCreatedAt = eCreatedAt e
|
|
|
|
}
|
|
|
|
|
|
|
|
processSuccess
|
|
|
|
:: ( MonadIO m )
|
|
|
|
=> Q.PGPool -> Event -> [HeaderConf] -> EventPayload -> HTTPResp
|
|
|
|
-> m (Either QErr ())
|
|
|
|
processSuccess pool e decodedHeaders ep resp = do
|
|
|
|
let respBody = hrsBody resp
|
|
|
|
respHeaders = hrsHeaders resp
|
|
|
|
respStatus = hrsStatus resp
|
|
|
|
invocation = mkInvo ep respStatus decodedHeaders respBody respHeaders
|
|
|
|
liftIO $ runExceptT $ Q.runTx pool (Q.RepeatableRead, Just Q.ReadWrite) $ do
|
|
|
|
insertInvocation invocation
|
|
|
|
setSuccess e
|
|
|
|
|
|
|
|
processError
|
|
|
|
:: ( MonadIO m
|
|
|
|
, MonadReader r m
|
2019-11-26 15:14:21 +03:00
|
|
|
, Has (L.Logger L.Hasura) r
|
2019-02-22 15:25:36 +03:00
|
|
|
)
|
|
|
|
=> Q.PGPool -> Event -> RetryConf -> [HeaderConf] -> EventPayload -> HTTPErr
|
|
|
|
-> m (Either QErr ())
|
|
|
|
processError pool e retryConf decodedHeaders ep err = do
|
|
|
|
logHTTPErr err
|
|
|
|
let invocation = case err of
|
|
|
|
HClient excp -> do
|
|
|
|
let errMsg = TBS.fromLBS $ encode $ show excp
|
|
|
|
mkInvo ep 1000 decodedHeaders errMsg []
|
|
|
|
HParse _ detail -> do
|
|
|
|
let errMsg = TBS.fromLBS $ encode detail
|
|
|
|
mkInvo ep 1001 decodedHeaders errMsg []
|
|
|
|
HStatus errResp -> do
|
|
|
|
let respPayload = hrsBody errResp
|
|
|
|
respHeaders = hrsHeaders errResp
|
|
|
|
respStatus = hrsStatus errResp
|
|
|
|
mkInvo ep respStatus decodedHeaders respPayload respHeaders
|
|
|
|
HOther detail -> do
|
|
|
|
let errMsg = (TBS.fromLBS $ encode detail)
|
|
|
|
mkInvo ep 500 decodedHeaders errMsg []
|
|
|
|
liftIO $ runExceptT $ Q.runTx pool (Q.RepeatableRead, Just Q.ReadWrite) $ do
|
|
|
|
insertInvocation invocation
|
|
|
|
retryOrSetError e retryConf err
|
|
|
|
|
|
|
|
retryOrSetError :: Event -> RetryConf -> HTTPErr -> Q.TxE QErr ()
|
|
|
|
retryOrSetError e retryConf err = do
|
|
|
|
let mretryHeader = getRetryAfterHeaderFromError err
|
|
|
|
tries = eTries e
|
2020-01-16 04:56:57 +03:00
|
|
|
mretryHeaderSeconds = mretryHeader >>= parseRetryHeader
|
2019-02-22 15:25:36 +03:00
|
|
|
triesExhausted = tries >= rcNumRetries retryConf
|
|
|
|
noRetryHeader = isNothing mretryHeaderSeconds
|
|
|
|
-- current_try = tries + 1 , allowed_total_tries = rcNumRetries retryConf + 1
|
|
|
|
if triesExhausted && noRetryHeader
|
|
|
|
then do
|
|
|
|
setError e
|
|
|
|
else do
|
|
|
|
currentTime <- liftIO getCurrentTime
|
|
|
|
let delay = fromMaybe (rcIntervalSec retryConf) mretryHeaderSeconds
|
|
|
|
diff = fromIntegral delay
|
|
|
|
retryTime = addUTCTime diff currentTime
|
|
|
|
setRetry e retryTime
|
2018-09-05 14:26:46 +03:00
|
|
|
where
|
2018-10-26 19:28:03 +03:00
|
|
|
getRetryAfterHeaderFromError (HStatus resp) = getRetryAfterHeaderFromResp resp
|
2019-02-22 15:25:36 +03:00
|
|
|
getRetryAfterHeaderFromError _ = Nothing
|
2018-10-26 19:28:03 +03:00
|
|
|
|
|
|
|
getRetryAfterHeaderFromResp resp
|
2019-02-22 15:25:36 +03:00
|
|
|
= let mHeader = find (\(HeaderConf name _)
|
|
|
|
-> CI.mk name == retryAfterHeader) (hrsHeaders resp)
|
2018-10-26 19:28:03 +03:00
|
|
|
in case mHeader of
|
|
|
|
Just (HeaderConf _ (HVValue value)) -> Just value
|
|
|
|
_ -> Nothing
|
2020-01-16 04:56:57 +03:00
|
|
|
|
|
|
|
parseRetryHeader = mfilter (> 0) . readMaybe . T.unpack
|
2018-10-26 19:28:03 +03:00
|
|
|
|
2019-02-22 15:25:36 +03:00
|
|
|
encodeHeader :: EventHeaderInfo -> HTTP.Header
|
|
|
|
encodeHeader (EventHeaderInfo hconf cache) =
|
|
|
|
let (HeaderConf name _) = hconf
|
|
|
|
ciname = CI.mk $ T.encodeUtf8 name
|
|
|
|
value = T.encodeUtf8 cache
|
|
|
|
in (ciname, value)
|
|
|
|
|
|
|
|
decodeHeader
|
|
|
|
:: LogEnvHeaders -> [EventHeaderInfo] -> (HTTP.HeaderName, BS.ByteString)
|
|
|
|
-> HeaderConf
|
|
|
|
decodeHeader logenv headerInfos (hdrName, hdrVal)
|
|
|
|
= let name = decodeBS $ CI.original hdrName
|
|
|
|
getName ehi = let (HeaderConf name' _) = ehiHeaderConf ehi
|
|
|
|
in name'
|
|
|
|
mehi = find (\hi -> getName hi == name) headerInfos
|
|
|
|
in case mehi of
|
|
|
|
Nothing -> HeaderConf name (HVValue (decodeBS hdrVal))
|
|
|
|
Just ehi -> if logenv
|
|
|
|
then HeaderConf name (HVValue (ehiCachedValue ehi))
|
|
|
|
else ehiHeaderConf ehi
|
|
|
|
where
|
|
|
|
decodeBS = TE.decodeUtf8With TE.lenientDecode
|
|
|
|
|
|
|
|
mkInvo
|
|
|
|
:: EventPayload -> Int -> [HeaderConf] -> TBS.TByteString -> [HeaderConf]
|
|
|
|
-> Invocation
|
|
|
|
mkInvo ep status reqHeaders respBody respHeaders
|
|
|
|
= let resp = if isClientError status
|
|
|
|
then mkClientErr respBody
|
|
|
|
else mkResp status respBody respHeaders
|
|
|
|
in
|
|
|
|
Invocation
|
|
|
|
(epId ep)
|
|
|
|
status
|
|
|
|
(mkWebhookReq (toJSON ep) reqHeaders)
|
|
|
|
resp
|
|
|
|
|
|
|
|
mkResp :: Int -> TBS.TByteString -> [HeaderConf] -> Response
|
|
|
|
mkResp status payload headers =
|
|
|
|
let wr = WebhookResponse payload (mkMaybe headers) status
|
|
|
|
in ResponseType1 wr
|
|
|
|
|
|
|
|
mkClientErr :: TBS.TByteString -> Response
|
|
|
|
mkClientErr message =
|
|
|
|
let cerr = ClientError message
|
|
|
|
in ResponseType2 cerr
|
|
|
|
|
|
|
|
mkWebhookReq :: Value -> [HeaderConf] -> WebhookRequest
|
|
|
|
mkWebhookReq payload headers = WebhookRequest payload (mkMaybe headers) invocationVersion
|
|
|
|
|
|
|
|
isClientError :: Int -> Bool
|
|
|
|
isClientError status = status >= 1000
|
|
|
|
|
|
|
|
mkMaybe :: [a] -> Maybe [a]
|
|
|
|
mkMaybe [] = Nothing
|
|
|
|
mkMaybe x = Just x
|
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
logQErr :: ( MonadReader r m, Has (L.Logger L.Hasura) r, MonadIO m) => QErr -> m ()
|
2019-02-22 15:25:36 +03:00
|
|
|
logQErr err = do
|
2019-11-26 15:14:21 +03:00
|
|
|
logger :: L.Logger L.Hasura <- asks getter
|
|
|
|
L.unLogger logger $ EventInternalErr err
|
2019-02-22 15:25:36 +03:00
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
logHTTPErr
|
|
|
|
:: ( MonadReader r m
|
|
|
|
, Has (L.Logger L.Hasura) r
|
|
|
|
, MonadIO m
|
|
|
|
)
|
|
|
|
=> HTTPErr -> m ()
|
2019-02-22 15:25:36 +03:00
|
|
|
logHTTPErr err = do
|
2019-11-26 15:14:21 +03:00
|
|
|
logger :: L.Logger L.Hasura <- asks getter
|
|
|
|
L.unLogger logger $ err
|
2019-02-22 15:25:36 +03:00
|
|
|
|
2020-03-11 09:27:31 +03:00
|
|
|
-- These run concurrently on their respective EventPayloads
|
2018-09-05 14:26:46 +03:00
|
|
|
tryWebhook
|
2019-11-26 15:14:21 +03:00
|
|
|
:: ( Has (L.Logger L.Hasura) r
|
2019-02-14 10:37:59 +03:00
|
|
|
, Has HTTP.Manager r
|
2019-11-26 15:14:21 +03:00
|
|
|
, MonadReader r m
|
|
|
|
, MonadIO m
|
|
|
|
, MonadError HTTPErr m
|
2018-09-05 14:26:46 +03:00
|
|
|
)
|
2019-02-22 15:25:36 +03:00
|
|
|
=> [HTTP.Header] -> HTTP.ResponseTimeout -> EventPayload -> String
|
|
|
|
-> m HTTPResp
|
|
|
|
tryWebhook headers responseTimeout ep webhook = do
|
2020-03-05 20:59:26 +03:00
|
|
|
let context = ExtraContext (epCreatedAt ep) (epId ep)
|
2019-02-22 15:25:36 +03:00
|
|
|
initReqE <- liftIO $ try $ HTTP.parseRequest webhook
|
|
|
|
case initReqE of
|
|
|
|
Left excp -> throwError $ HClient excp
|
|
|
|
Right initReq -> do
|
|
|
|
let req = initReq
|
|
|
|
{ HTTP.method = "POST"
|
|
|
|
, HTTP.requestHeaders = headers
|
|
|
|
, HTTP.requestBody = HTTP.RequestBodyLBS (encode ep)
|
|
|
|
, HTTP.responseTimeout = responseTimeout
|
|
|
|
}
|
2020-03-11 09:27:31 +03:00
|
|
|
|
|
|
|
eitherResp <- runHTTP req (Just context)
|
|
|
|
onLeft eitherResp throwError
|
2018-09-05 14:26:46 +03:00
|
|
|
|
|
|
|
getEventTriggerInfoFromEvent :: SchemaCache -> Event -> Maybe EventTriggerInfo
|
|
|
|
getEventTriggerInfoFromEvent sc e = let table = eTable e
|
|
|
|
tableInfo = M.lookup table $ scTables sc
|
2019-07-22 15:47:13 +03:00
|
|
|
in M.lookup ( tmName $ eTrigger e) =<< (_tiEventTriggerInfoMap <$> tableInfo)
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2020-03-11 09:27:31 +03:00
|
|
|
---- DATABASE QUERIES ---------------------
|
|
|
|
--
|
|
|
|
-- The API for our in-database work queue:
|
|
|
|
-------------------------------------------
|
|
|
|
|
|
|
|
-- | Lock and return events not yet being processed or completed, up to some
|
|
|
|
-- limit. Process events approximately in created_at order, but we make no
|
|
|
|
-- ordering guarentees; events can and will race. Nevertheless we want to
|
|
|
|
-- ensure newer change events don't starve older ones.
|
|
|
|
fetchEvents :: Int -> Q.TxE QErr [Event]
|
|
|
|
fetchEvents limitI =
|
2018-09-05 14:26:46 +03:00
|
|
|
map uncurryEvent <$> Q.listQE defaultTxErrorHandler [Q.sql|
|
|
|
|
UPDATE hdb_catalog.event_log
|
|
|
|
SET locked = 't'
|
2018-09-07 11:23:56 +03:00
|
|
|
WHERE id IN ( SELECT l.id
|
|
|
|
FROM hdb_catalog.event_log l
|
2019-11-13 10:29:19 +03:00
|
|
|
WHERE l.delivered = 'f' and l.error = 'f' and l.locked = 'f'
|
2019-02-22 15:25:36 +03:00
|
|
|
and (l.next_retry_at is NULL or l.next_retry_at <= now())
|
2019-11-13 10:29:19 +03:00
|
|
|
and l.archived = 'f'
|
2020-03-11 09:27:31 +03:00
|
|
|
ORDER BY created_at
|
|
|
|
LIMIT $1
|
|
|
|
FOR UPDATE SKIP LOCKED )
|
2019-03-25 20:10:52 +03:00
|
|
|
RETURNING id, schema_name, table_name, trigger_name, payload::json, tries, created_at
|
2020-03-11 09:27:31 +03:00
|
|
|
|] (Identity limit) True
|
2019-03-25 20:10:52 +03:00
|
|
|
where uncurryEvent (id', sn, tn, trn, Q.AltJ payload, tries, created) =
|
2019-02-22 15:25:36 +03:00
|
|
|
Event
|
|
|
|
{ eId = id'
|
|
|
|
, eTable = QualifiedObject sn tn
|
2019-03-25 20:10:52 +03:00
|
|
|
, eTrigger = TriggerMeta trn
|
2019-02-22 15:25:36 +03:00
|
|
|
, eEvent = payload
|
|
|
|
, eTries = tries
|
|
|
|
, eCreatedAt = created
|
|
|
|
}
|
2020-03-11 09:27:31 +03:00
|
|
|
limit = fromIntegral limitI :: Word64
|
2018-09-05 14:26:46 +03:00
|
|
|
|
|
|
|
insertInvocation :: Invocation -> Q.TxE QErr ()
|
|
|
|
insertInvocation invo = do
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
INSERT INTO hdb_catalog.event_invocation_logs (event_id, status, request, response)
|
|
|
|
VALUES ($1, $2, $3, $4)
|
2019-02-22 15:25:36 +03:00
|
|
|
|] ( iEventId invo
|
|
|
|
, toInt64 $ iStatus invo
|
|
|
|
, Q.AltJ $ toJSON $ iRequest invo
|
|
|
|
, Q.AltJ $ toJSON $ iResponse invo) True
|
2018-09-05 14:26:46 +03:00
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
UPDATE hdb_catalog.event_log
|
|
|
|
SET tries = tries + 1
|
|
|
|
WHERE id = $1
|
|
|
|
|] (Identity $ iEventId invo) True
|
|
|
|
|
2019-02-22 15:25:36 +03:00
|
|
|
setSuccess :: Event -> Q.TxE QErr ()
|
|
|
|
setSuccess e = Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
UPDATE hdb_catalog.event_log
|
|
|
|
SET delivered = 't', next_retry_at = NULL, locked = 'f'
|
|
|
|
WHERE id = $1
|
|
|
|
|] (Identity $ eId e) True
|
|
|
|
|
|
|
|
setError :: Event -> Q.TxE QErr ()
|
|
|
|
setError e = Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
UPDATE hdb_catalog.event_log
|
|
|
|
SET error = 't', next_retry_at = NULL, locked = 'f'
|
|
|
|
WHERE id = $1
|
|
|
|
|] (Identity $ eId e) True
|
|
|
|
|
|
|
|
setRetry :: Event -> UTCTime -> Q.TxE QErr ()
|
|
|
|
setRetry e time =
|
2018-09-05 14:26:46 +03:00
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
UPDATE hdb_catalog.event_log
|
2019-02-22 15:25:36 +03:00
|
|
|
SET next_retry_at = $1, locked = 'f'
|
|
|
|
WHERE id = $2
|
|
|
|
|] (time, eId e) True
|
2018-09-05 14:26:46 +03:00
|
|
|
|
|
|
|
unlockAllEvents :: Q.TxE QErr ()
|
|
|
|
unlockAllEvents =
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
UPDATE hdb_catalog.event_log
|
|
|
|
SET locked = 'f'
|
2019-08-27 03:41:38 +03:00
|
|
|
WHERE locked = 't'
|
2018-09-05 14:26:46 +03:00
|
|
|
|] () False
|
|
|
|
|
2018-10-26 19:28:03 +03:00
|
|
|
toInt64 :: (Integral a) => a -> Int64
|
|
|
|
toInt64 = fromIntegral
|