packages feed

keiro-dsl-0.2.0.0: test/conformance-process-full/Generated/SurgeDemo/SurgeFlow/Process.hs

{-# LANGUAGE OverloadedStrings #-}

-- @generated by keiro-dsl; do not edit. Regenerated from the .keiro spec.
module Generated.SurgeDemo.SurgeFlow.Process (
    surgeFlowProcessName,
    surgeFlowCategory,
    surgeFlowProcessWorkerOptions,
    surgeFlowTimerRequest,
    surgeFlowFireOutcome,
) where

import Data.Aeson (Value, object, (.=))
import Data.Text (Text)
import Data.Text qualified as T
import Data.Time (UTCTime)
import Data.UUID (UUID)
import Data.UUID.V5 qualified as UUID.V5
import Keiro.Command (CommandError (..))
import Keiro.ProcessManager (PoisonPolicy (..), RejectedCommandPolicy (..), WorkerOptions (..))
import Keiro.Stream qualified as Stream
import Keiro.Timer (TimerId (..), TimerRequest (..))
import Shibuya.Core.Ack (RetryDelay (..))

-- The define-once ProcessManager name (hole-kind 5: referenced, never retyped).
surgeFlowProcessName :: Text
surgeFlowProcessName = "surge-demo"

-- The validated saga stream category (hole-kind 5: referenced, never retyped).
-- Saga streams are '<category>-<correlationId>' via Keiro.Stream.entityStream.
-- categoryUnsafe is safe here because keiro-dsl check proved the literal legal.
surgeFlowCategory :: Stream.StreamCategory a
surgeFlowCategory = Stream.categoryUnsafe "surge"

-- Node-level worker policy lowered from the spec. Pass this value to
-- Keiro.ProcessManager.runProcessManagerWorkerWith.
surgeFlowProcessWorkerOptions :: WorkerOptions es msg
surgeFlowProcessWorkerOptions =
    WorkerOptions
        { poisonPolicy = PoisonHalt
        , rejectedCommandPolicy = RejectedHalt
        , transientRetryDelay = RetryDelay 5 -- matches defaultWorkerOptions; runtime tuning
        , metrics = Nothing -- runtime configuration; install at call site
        }

-- The deterministic timer-request builder: id derived from the correlation
-- key (hole-kind 1), processManagerName referenced, payload from the spec.
-- (timer id derived as uuidv5 of "surge-timer:" <> correlationId)
surgeFlowTimerRequest :: Text -> UTCTime -> TimerRequest
surgeFlowTimerRequest correlationId fireAtTime =
    TimerRequest
        { timerId = TimerId (namedUuid ("surge-timer:" <> correlationId))
        , processManagerName = surgeFlowProcessName
        , correlationId = correlationId
        , fireAt = fireAtTime
        , payload = object ["kind" .= ("surge-follow-up" :: Value)]
        }

-- The timer-fire disposition table (hole-kind 2), derived from the spec.
-- on-reject => Fired is the benign inversion.
-- A duplicate append reaches on-error unless it is confirmed against the
-- target stream. Use Keiro.ProcessManager.confirmBenignDuplicate:
--   StreamName -> EventId -> CommandError -> Eff es Bool
-- Fold True into the duplicate result and surface False as the failure.
surgeFlowFireOutcome :: Either CommandError a -> Maybe ()
surgeFlowFireOutcome result = case result of
    Right{} -> Just () -- Fired
    Left CommandRejected -> Just () -- Fired
    Left (CommandAmbiguous _) -> Nothing -- Retry  -- explicit definition-bug arm
    Left{} -> Nothing -- Retry

-- max-attempts = 5, dead-letter = "ceiling"
-- (the timer worker must pass Just 5 to runTimerWorkerWith, never the
--  defaultTimerWorkerOptions Nothing ceiling that retries forever).

-- deterministic v5 UUID of a correlation-keyed string (hole-kind 1).
namedUuid :: Text -> UUID
namedUuid v = UUID.V5.generateNamed UUID.V5.namespaceURL (map (fromIntegral . fromEnum) (T.unpack v))