2020-05-13 15:33:16 +03:00
|
|
|
module Hasura.RQL.DDL.ScheduledTrigger
|
2021-09-24 01:56:37 +03:00
|
|
|
( runCreateCronTrigger,
|
|
|
|
runDeleteCronTrigger,
|
|
|
|
dropCronTriggerInMetadata,
|
|
|
|
resolveCronTrigger,
|
|
|
|
runCreateScheduledEvent,
|
|
|
|
runDeleteScheduledEvent,
|
|
|
|
runGetScheduledEvents,
|
|
|
|
runGetEventInvocations,
|
|
|
|
populateInitialCronTriggerEvents,
|
2022-01-27 09:43:39 +03:00
|
|
|
runGetCronTriggers,
|
2021-09-24 01:56:37 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.Environment qualified as Env
|
|
|
|
import Data.HashMap.Strict qualified as Map
|
|
|
|
import Data.HashMap.Strict.InsOrd qualified as OMap
|
|
|
|
import Data.Time.Clock qualified as C
|
2022-06-05 23:27:09 +03:00
|
|
|
import Data.URL.Template (printURLTemplate)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.EncJSON
|
|
|
|
import Hasura.Eventing.ScheduledTrigger
|
|
|
|
import Hasura.Metadata.Class
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DDL.EventTrigger (getHeaderInfosFromConf)
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.EventTrigger
|
|
|
|
import Hasura.RQL.Types.Metadata
|
|
|
|
import Hasura.RQL.Types.Metadata.Object
|
|
|
|
import Hasura.RQL.Types.ScheduledTrigger
|
|
|
|
import Hasura.RQL.Types.SchemaCache
|
|
|
|
import Hasura.RQL.Types.SchemaCache.Build
|
2021-09-24 01:56:37 +03:00
|
|
|
import System.Cron.Types (CronSchedule)
|
|
|
|
|
|
|
|
populateInitialCronTriggerEvents ::
|
|
|
|
( MonadIO m,
|
|
|
|
MonadMetadataStorageQueryAPI m
|
|
|
|
) =>
|
|
|
|
CronSchedule ->
|
|
|
|
TriggerName ->
|
|
|
|
m ()
|
2021-05-26 19:19:26 +03:00
|
|
|
populateInitialCronTriggerEvents schedule triggerName = do
|
|
|
|
currentTime <- liftIO C.getCurrentTime
|
|
|
|
let scheduleTimes = generateScheduleTimes currentTime 100 schedule
|
2021-09-13 21:00:53 +03:00
|
|
|
insertCronEvents $ map (CronEventSeed triggerName) scheduleTimes
|
2021-05-26 19:19:26 +03:00
|
|
|
pure ()
|
2021-05-11 18:18:31 +03:00
|
|
|
|
2020-05-13 15:33:16 +03:00
|
|
|
-- | runCreateCronTrigger will update a existing cron trigger when the 'replace'
|
|
|
|
-- value is set to @true@ and when replace is @false@ a new cron trigger will
|
|
|
|
-- be created
|
2021-09-24 01:56:37 +03:00
|
|
|
runCreateCronTrigger ::
|
|
|
|
( CacheRWM m,
|
|
|
|
MonadIO m,
|
|
|
|
MetadataM m,
|
|
|
|
MonadMetadataStorageQueryAPI m
|
|
|
|
) =>
|
|
|
|
CreateCronTrigger ->
|
|
|
|
m EncJSON
|
2020-05-13 15:33:16 +03:00
|
|
|
runCreateCronTrigger CreateCronTrigger {..} = do
|
2021-09-24 01:56:37 +03:00
|
|
|
let q =
|
|
|
|
CronTriggerMetadata
|
2022-03-11 02:22:54 +03:00
|
|
|
_cctName
|
|
|
|
_cctWebhook
|
|
|
|
_cctCronSchedule
|
|
|
|
_cctPayload
|
|
|
|
_cctRetryConf
|
|
|
|
_cctHeaders
|
|
|
|
_cctIncludeInMetadata
|
|
|
|
_cctComment
|
|
|
|
_cctRequestTransform
|
|
|
|
_cctResponseTransform
|
|
|
|
case _cctReplace of
|
2020-05-13 15:33:16 +03:00
|
|
|
True -> updateCronTrigger q
|
|
|
|
False -> do
|
2021-09-24 01:56:37 +03:00
|
|
|
cronTriggersMap <- scCronTriggers <$> askSchemaCache
|
|
|
|
case Map.lookup (ctName q) cronTriggersMap of
|
|
|
|
Nothing -> pure ()
|
|
|
|
Just _ ->
|
|
|
|
throw400 AlreadyExists $
|
|
|
|
"cron trigger with name: "
|
|
|
|
<> triggerNameToTxt (ctName q)
|
|
|
|
<> " already exists"
|
|
|
|
|
2022-03-11 02:22:54 +03:00
|
|
|
let metadataObj = MOCronTrigger _cctName
|
2021-09-24 01:56:37 +03:00
|
|
|
metadata =
|
|
|
|
CronTriggerMetadata
|
2022-03-11 02:22:54 +03:00
|
|
|
_cctName
|
|
|
|
_cctWebhook
|
|
|
|
_cctCronSchedule
|
|
|
|
_cctPayload
|
|
|
|
_cctRetryConf
|
|
|
|
_cctHeaders
|
|
|
|
_cctIncludeInMetadata
|
|
|
|
_cctComment
|
|
|
|
_cctRequestTransform
|
|
|
|
_cctResponseTransform
|
2021-09-24 01:56:37 +03:00
|
|
|
buildSchemaCacheFor metadataObj $
|
|
|
|
MetadataModifier $
|
2022-03-11 02:22:54 +03:00
|
|
|
metaCronTriggers %~ OMap.insert _cctName metadata
|
|
|
|
populateInitialCronTriggerEvents _cctCronSchedule _cctName
|
2021-09-24 01:56:37 +03:00
|
|
|
return successMsg
|
|
|
|
|
|
|
|
resolveCronTrigger ::
|
|
|
|
(QErrM m) =>
|
|
|
|
Env.Environment ->
|
|
|
|
CronTriggerMetadata ->
|
|
|
|
m CronTriggerInfo
|
|
|
|
resolveCronTrigger env CronTriggerMetadata {..} = do
|
2020-12-08 17:22:31 +03:00
|
|
|
webhookInfo <- resolveWebhook env ctWebhook
|
|
|
|
headerInfo <- getHeaderInfosFromConf env ctHeaders
|
2022-06-05 23:27:09 +03:00
|
|
|
let urlTemplate = printURLTemplate $ unInputWebhook ctWebhook
|
2020-05-13 15:33:16 +03:00
|
|
|
pure $
|
2021-09-24 01:56:37 +03:00
|
|
|
CronTriggerInfo
|
|
|
|
ctName
|
|
|
|
ctSchedule
|
|
|
|
ctPayload
|
|
|
|
ctRetryConf
|
2022-06-05 23:27:09 +03:00
|
|
|
(EnvRecord urlTemplate webhookInfo)
|
2021-09-24 01:56:37 +03:00
|
|
|
headerInfo
|
|
|
|
ctComment
|
2022-01-19 07:46:42 +03:00
|
|
|
ctRequestTransform
|
|
|
|
ctResponseTransform
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|
|
|
updateCronTrigger ::
|
|
|
|
( CacheRWM m,
|
|
|
|
MonadIO m,
|
|
|
|
MetadataM m,
|
|
|
|
MonadMetadataStorageQueryAPI m
|
|
|
|
) =>
|
|
|
|
CronTriggerMetadata ->
|
|
|
|
m EncJSON
|
2020-05-13 15:33:16 +03:00
|
|
|
updateCronTrigger cronTriggerMetadata = do
|
2020-12-08 17:22:31 +03:00
|
|
|
let triggerName = ctName cronTriggerMetadata
|
|
|
|
checkExists triggerName
|
2021-09-24 01:56:37 +03:00
|
|
|
buildSchemaCacheFor (MOCronTrigger triggerName) $
|
|
|
|
MetadataModifier $
|
|
|
|
metaCronTriggers %~ OMap.insert triggerName cronTriggerMetadata
|
2021-05-26 19:19:26 +03:00
|
|
|
dropFutureCronEvents $ SingleCronTrigger triggerName
|
2020-06-23 18:21:34 +03:00
|
|
|
currentTime <- liftIO C.getCurrentTime
|
2020-12-08 17:22:31 +03:00
|
|
|
let scheduleTimes = generateScheduleTimes currentTime 100 $ ctSchedule cronTriggerMetadata
|
2021-09-13 21:00:53 +03:00
|
|
|
insertCronEvents $ map (CronEventSeed triggerName) scheduleTimes
|
2020-12-08 17:22:31 +03:00
|
|
|
pure successMsg
|
2020-05-13 15:33:16 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
runDeleteCronTrigger ::
|
|
|
|
( CacheRWM m,
|
|
|
|
MetadataM m,
|
|
|
|
MonadMetadataStorageQueryAPI m
|
|
|
|
) =>
|
|
|
|
ScheduledTriggerName ->
|
|
|
|
m EncJSON
|
2020-05-13 15:33:16 +03:00
|
|
|
runDeleteCronTrigger (ScheduledTriggerName stName) = do
|
|
|
|
checkExists stName
|
2021-09-24 01:56:37 +03:00
|
|
|
withNewInconsistentObjsCheck $
|
|
|
|
buildSchemaCache $
|
|
|
|
dropCronTriggerInMetadata stName
|
2021-05-26 19:19:26 +03:00
|
|
|
dropFutureCronEvents $ SingleCronTrigger stName
|
2020-05-13 15:33:16 +03:00
|
|
|
return successMsg
|
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
dropCronTriggerInMetadata :: TriggerName -> MetadataModifier
|
|
|
|
dropCronTriggerInMetadata name =
|
|
|
|
MetadataModifier $ metaCronTriggers %~ OMap.delete name
|
2020-05-13 15:33:16 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
runCreateScheduledEvent ::
|
|
|
|
(MonadMetadataStorageQueryAPI m) =>
|
|
|
|
CreateScheduledEvent ->
|
|
|
|
m EncJSON
|
2021-02-18 19:46:14 +03:00
|
|
|
runCreateScheduledEvent scheduledEvent = do
|
2021-09-13 21:00:53 +03:00
|
|
|
eid <- createOneOffScheduledEvent scheduledEvent
|
2021-09-24 01:56:37 +03:00
|
|
|
pure $ encJFromJValue $ J.object ["message" J..= J.String "success", "event_id" J..= eid]
|
2020-05-13 15:33:16 +03:00
|
|
|
|
|
|
|
checkExists :: (CacheRM m, MonadError QErr m) => TriggerName -> m ()
|
|
|
|
checkExists name = do
|
|
|
|
cronTriggersMap <- scCronTriggers <$> askSchemaCache
|
2021-09-24 01:56:37 +03:00
|
|
|
void $
|
|
|
|
onNothing (Map.lookup name cronTriggersMap) $
|
|
|
|
throw400 NotExists $
|
|
|
|
"cron trigger with name: " <> triggerNameToTxt name <> " does not exist"
|
|
|
|
|
|
|
|
runDeleteScheduledEvent ::
|
|
|
|
(MonadMetadataStorageQueryAPI m) => DeleteScheduledEvent -> m EncJSON
|
|
|
|
runDeleteScheduledEvent DeleteScheduledEvent {..} = do
|
2021-01-07 12:04:22 +03:00
|
|
|
dropEvent _dseEventId _dseType
|
|
|
|
pure successMsg
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
runGetScheduledEvents ::
|
|
|
|
( CacheRM m,
|
|
|
|
MonadMetadataStorageQueryAPI m
|
|
|
|
) =>
|
|
|
|
GetScheduledEvents ->
|
|
|
|
m EncJSON
|
2021-01-07 12:04:22 +03:00
|
|
|
runGetScheduledEvents gse = do
|
|
|
|
case _gseScheduledEvent gse of
|
2021-09-24 01:56:37 +03:00
|
|
|
SEOneOff -> pure ()
|
2021-01-07 12:04:22 +03:00
|
|
|
SECron name -> checkExists name
|
|
|
|
encJFromJValue <$> fetchScheduledEvents gse
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
runGetEventInvocations ::
|
|
|
|
( CacheRM m,
|
|
|
|
MonadMetadataStorageQueryAPI m
|
|
|
|
) =>
|
|
|
|
GetEventInvocations ->
|
|
|
|
m EncJSON
|
2022-09-15 22:10:53 +03:00
|
|
|
runGetEventInvocations getEventInvocations@GetEventInvocations {..} = do
|
2021-01-07 12:04:22 +03:00
|
|
|
case _geiInvocationsBy of
|
|
|
|
GIBEventId _ _ -> pure ()
|
|
|
|
GIBEvent event -> case event of
|
2021-09-24 01:56:37 +03:00
|
|
|
SEOneOff -> pure ()
|
2021-01-07 12:04:22 +03:00
|
|
|
SECron name -> checkExists name
|
2022-09-15 22:10:53 +03:00
|
|
|
WithOptionalTotalCount countMaybe invocations <- fetchInvocations getEventInvocations
|
2021-09-24 01:56:37 +03:00
|
|
|
pure $
|
|
|
|
encJFromJValue $
|
2022-09-15 22:10:53 +03:00
|
|
|
J.object $
|
|
|
|
("invocations" J..= invocations) : (maybe mempty (\count -> ["count" J..= count]) countMaybe)
|
2022-01-27 09:43:39 +03:00
|
|
|
|
|
|
|
-- | Metadata API handler to retrieve all the cron triggers from the metadata
|
|
|
|
runGetCronTriggers :: MetadataM m => m EncJSON
|
|
|
|
runGetCronTriggers = do
|
|
|
|
cronTriggers <- toList . _metaCronTriggers <$> getMetadata
|
|
|
|
pure $
|
|
|
|
encJFromJValue $
|
|
|
|
J.object
|
|
|
|
["cron_triggers" J..= cronTriggers]
|