2020-07-02 14:57:09 +03:00
|
|
|
module Hasura.Eventing.Common where
|
|
|
|
|
|
|
|
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
|
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
|