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
|
2020-07-02 14:57:09 +03:00
|
|
|
import Hasura.RQL.Types.EventTrigger (EventId)
|
2020-07-14 22:00:58 +03:00
|
|
|
import Hasura.RQL.Types.ScheduledTrigger (CronEventId, StandAloneScheduledEventId)
|
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-07-14 22:00:58 +03:00
|
|
|
{ leCronEvents :: TVar (Set.Set CronEventId)
|
2020-07-02 14:57:09 +03:00
|
|
|
, leStandAloneEvents :: TVar (Set.Set StandAloneScheduledEventId)
|
2020-07-14 22:00:58 +03:00
|
|
|
, leEvents :: TVar (Set.Set EventId)
|
2020-07-02 14:57:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
initLockedEventsCtx :: STM LockedEventsCtx
|
|
|
|
initLockedEventsCtx = do
|
|
|
|
leCronEvents <- newTVar Set.empty
|
|
|
|
leStandAloneEvents <- newTVar Set.empty
|
|
|
|
leEvents <- newTVar Set.empty
|
|
|
|
return $ LockedEventsCtx{..}
|
|
|
|
|
|
|
|
-- | 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-07-14 22:00:58 +03:00
|
|
|
saveLockedEvents :: (MonadIO m) => [Text] -> TVar (Set.Set Text) -> 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-07-14 22:00:58 +03:00
|
|
|
removeEventFromLockedEvents :: MonadIO m => Text -> TVar (Set.Set Text) -> 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
|