2021-07-02 20:24:49 +03:00
|
|
|
module Hasura.Eventing.Common
|
|
|
|
( PGTextArray(..)
|
|
|
|
, LockedEventsCtx(..)
|
|
|
|
, saveLockedEvents
|
|
|
|
, removeEventFromLockedEvents
|
|
|
|
) where
|
2020-07-02 14:57:09 +03:00
|
|
|
|
|
|
|
import Control.Concurrent.STM.TVar
|
|
|
|
import Control.Monad.STM
|
2020-07-14 22:00:58 +03:00
|
|
|
import Hasura.Prelude
|
2021-05-14 12:38:37 +03:00
|
|
|
import Hasura.RQL.Types.Action (LockedActionEventId)
|
2020-07-02 14:57:09 +03:00
|
|
|
import Hasura.RQL.Types.EventTrigger (EventId)
|
2020-09-07 09:15:15 +03:00
|
|
|
import Hasura.RQL.Types.ScheduledTrigger (CronEventId, OneOffScheduledEventId)
|
2020-07-02 14:57:09 +03:00
|
|
|
|
2020-07-14 22:00:58 +03:00
|
|
|
import qualified Data.Set as Set
|
2021-05-26 19:19:26 +03:00
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
import qualified Database.PG.Query.PTI as PTI
|
|
|
|
import qualified PostgreSQL.Binary.Encoding as PE
|
|
|
|
|
|
|
|
-- | PGTextArray is only used for PG array encoding
|
|
|
|
newtype PGTextArray =
|
|
|
|
PGTextArray { unPGTextArray :: [Text]}
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
instance Q.ToPrepArg PGTextArray where
|
|
|
|
toPrepVal (PGTextArray l) =
|
|
|
|
Q.toPrepValHelper PTI.unknown encoder l
|
|
|
|
where
|
|
|
|
-- 25 is the OID value of TEXT, https://jdbc.postgresql.org/development/privateapi/constant-values.html
|
|
|
|
encoder = PE.array 25 . PE.dimensionArray foldl' (PE.encodingArray . PE.text_strict)
|
2020-07-02 14:57:09 +03:00
|
|
|
|
|
|
|
data LockedEventsCtx
|
|
|
|
= LockedEventsCtx
|
2020-09-07 09:15:15 +03:00
|
|
|
{ leCronEvents :: TVar (Set.Set CronEventId)
|
|
|
|
, leOneOffEvents :: TVar (Set.Set OneOffScheduledEventId)
|
|
|
|
, leEvents :: TVar (Set.Set EventId)
|
2021-05-14 12:38:37 +03:00
|
|
|
, leActionEvents :: TVar (Set.Set LockedActionEventId)
|
2020-07-02 14:57:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
-- | After the events are fetched from the DB, we store the locked events
|
|
|
|
-- in a hash set(order doesn't matter and look ups are faster) in the
|
|
|
|
-- event engine context
|
2020-11-25 13:56:44 +03:00
|
|
|
saveLockedEvents :: (MonadIO m) => [EventId] -> TVar (Set.Set EventId) -> m ()
|
2020-07-02 14:57:09 +03:00
|
|
|
saveLockedEvents eventIds lockedEvents =
|
2020-07-14 22:00:58 +03:00
|
|
|
liftIO $ atomically $ do
|
2020-07-02 14:57:09 +03:00
|
|
|
lockedEventsVals <- readTVar lockedEvents
|
|
|
|
writeTVar lockedEvents $!
|
|
|
|
Set.union lockedEventsVals $ Set.fromList eventIds
|
|
|
|
|
|
|
|
-- | Remove an event from the 'LockedEventsCtx' after it has been processed
|
2020-11-25 13:56:44 +03:00
|
|
|
removeEventFromLockedEvents
|
|
|
|
:: MonadIO m => EventId -> TVar (Set.Set EventId) -> m ()
|
2020-07-02 14:57:09 +03:00
|
|
|
removeEventFromLockedEvents eventId lockedEvents =
|
2020-07-14 22:00:58 +03:00
|
|
|
liftIO $ atomically $ do
|
2020-07-02 14:57:09 +03:00
|
|
|
lockedEventsVals <- readTVar lockedEvents
|
|
|
|
writeTVar lockedEvents $! Set.delete eventId lockedEventsVals
|