2021-07-02 20:24:49 +03:00
|
|
|
module Hasura.Eventing.Common
|
2021-09-24 01:56:37 +03:00
|
|
|
( LockedEventsCtx (..),
|
|
|
|
saveLockedEvents,
|
|
|
|
removeEventFromLockedEvents,
|
|
|
|
)
|
|
|
|
where
|
2021-09-20 10:34:59 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Control.Concurrent.STM.TVar
|
|
|
|
import Control.Monad.STM
|
|
|
|
import Data.Set qualified as Set
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types.Action (LockedActionEventId)
|
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.Eventing (EventId)
|
|
|
|
import Hasura.RQL.Types.ScheduledTrigger (CronEventId, OneOffScheduledEventId)
|
2020-07-02 14:57:09 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
data LockedEventsCtx = LockedEventsCtx
|
|
|
|
{ leCronEvents :: TVar (Set.Set CronEventId),
|
|
|
|
leOneOffEvents :: TVar (Set.Set OneOffScheduledEventId),
|
|
|
|
leEvents :: TVar (HashMap SourceName (Set.Set EventId)),
|
|
|
|
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 =
|
2021-09-24 01:56:37 +03:00
|
|
|
liftIO $
|
|
|
|
atomically $ do
|
|
|
|
lockedEventsVals <- readTVar lockedEvents
|
|
|
|
writeTVar lockedEvents
|
|
|
|
$! Set.union lockedEventsVals
|
|
|
|
$ Set.fromList eventIds
|
2020-07-02 14:57:09 +03:00
|
|
|
|
|
|
|
-- | Remove an event from the 'LockedEventsCtx' after it has been processed
|
2021-09-24 01:56:37 +03:00
|
|
|
removeEventFromLockedEvents ::
|
|
|
|
MonadIO m => EventId -> TVar (Set.Set EventId) -> m ()
|
2020-07-02 14:57:09 +03:00
|
|
|
removeEventFromLockedEvents eventId lockedEvents =
|
2021-09-24 01:56:37 +03:00
|
|
|
liftIO $
|
|
|
|
atomically $ do
|
|
|
|
lockedEventsVals <- readTVar lockedEvents
|
|
|
|
writeTVar lockedEvents $! Set.delete eventId lockedEventsVals
|