Safe Haskell | None |
---|---|
Language | Haskell2010 |
Temporal.Bundle
Description
Define and reference workflows and activities using records.
The usage of this module is best explained by example. First, define a record type that contains all of the workflows and activities that you want to define:
data EmailWorkflowsB t f = Workflows { emailNewUser :: Wear t f (Int -> Workflow ()) , sendEmail :: Wear t f (String -> Activity () ()) , emailPasswordReset :: Wear t f (String -> Workflow ()) } deriving (Generic) type EmailWorkflows = EmailWorkflowsB Bare Identity type EmailWorkflowsH = EmailWorkflowsB Covered
We define all of our fields using the Wear
type, which is a type family
that lets us convert fields from their direct function definitions into
versions that are wrapped in a functor.
Next, we will need some boilerplate instances for this type:
instance FunctorB (EmailWorkflowsB Covered) instance TraversableB (EmailWorkflowsB Covered) instance ApplicativeB (EmailWorkflowsB Covered) instance ConstraintsB (EmailWorkflowsB Covered) instance BareB EmailWorkflowsB instance Label (EmailWorkflowsB Covered)
As an alternative to using Generic
and deriving these instances, you can use
TH
to generate them for you. With this style, you don't have
to introduce the type parameters manually:
import Temporal.Bundle.TH passthroughBareB [d| data EmailWorkflows = Workflows { emailNewUser :: Int -> Workflow () , sendEmail :: String -> Activity () () , emailPasswordReset :: String -> Workflow () } |]
This will generate the same instances as the manual version above, with the following type synonyms:
data EmailWorkflowsB = Workflows {..} type EmailWorkflows = EmailWorkflowsB Bare Identity type EmailWorkflowsH = EmailWorkflowsB Covered
Now that we have our record type, we can turn it into references and definitions:
Using refs
, we can use our record type to provide references to our workflows and activities.
References are essentially identifiers that we can use across application / process boundaries
to invoke our workflows and activities with Temporal.
wfRefs :: Refs EmailWorkflowsB wfRefs = refs JSON workflowsImpl
Now, we can implement our workflows and activities:
workflowsImpl :: EmailWorkflows workflowsImpl = Workflows { emailNewUser = userId -> provideCallStack $ do h <- startActivity wfRefs.sendEmail -- <=== Note how we can reference other workflows and activities using the refs we defined above (defaultStartActivityOptions $ ScheduleToClose $ TimeSpec 0 0) "foo" wait h , sendEmail = email -> do pure () , emailPasswordReset = email -> do pure () }
Lastly, we want a way to register our workflows and activities with the Temporal worker:
workerConfigFromRecord = defsToConfig $ defs JSON workflowsImpl
This will produce a ConfigM
that we can use to register our workflows and activities with the Temporal worker
as part of a larger ConfigM
that we pass to startWorker
.
Synopsis
- refs :: CanUseAsRefs r codec => codec -> Refs r
- type Refs (f :: (Type -> Type -> Type) -> k) = f Ref
- data Ref a b
- defs :: CanUseAsDefs f codec env => codec -> Impl f -> Defs env f
- type Defs env (f :: (Type -> Type -> Type) -> k) = f (Def env)
- data Def a b c
- collectTemporalDefinitions :: forall rec actEnv (f :: Type -> Exp Type). (TraversableRec rec, ConstraintsRec rec, AllRecF (ToDefinitions actEnv) f rec) => rec f -> Definitions actEnv
- type Impl (f :: (a -> a -> Type) -> k) = f (Pure :: a -> a -> Type)
- inWorkflowProxies :: forall synchronicity rec. (RequireCallStack, ConstraintsRec rec, AllRec (ClassF (UseAsInWorkflowProxy synchronicity) Ref) rec, ApplicativeRec rec) => synchronicity -> rec (RefStartOptions <=< Ref) -> rec Ref -> rec (InWorkflowProxies synchronicity <=< Ref)
- data InWorkflowProxies a b c
- type InWorkflowProxyOptions = RefStartOptions <=< Ref
- data RefStartOptions a b
- class UseAsInWorkflowProxy synchronicity ref where
- useAsInWorkflowProxy :: synchronicity -> ref -> (RefStartOptions @@ ref) -> InWorkflowProxies synchronicity @@ ref
- provideDefaultOptions :: (ApplicativeRec rec, ConstraintsRec rec, AllRec FieldToStartOptionDefaults rec) => StartActivityOptions -> StartChildWorkflowOptions -> rec RefStartOptions
- class FieldToStartOptionDefaults f where
- type family RefStartOptionsType f where ...
- withTaskQueues :: (TraversableRec rec, MonadUnliftIO m, MonadCatch m) => Client -> WorkerConfigs env rec -> (Workers env rec -> m a) -> m a
- linkTaskQueues :: forall m rec env. (TraversableRec rec, MonadUnliftIO m) => Workers env rec -> m ()
- startTaskQueues :: (TraversableRec rec, MonadUnliftIO m, MonadCatch m) => Client -> WorkerConfigs env rec -> m (Workers env rec)
- shutdownTaskQueues :: forall m rec env. (TraversableRec rec, MonadUnliftIO m) => Workers env rec -> m (rec (ConstFn (Either SomeException ()) :: Type -> Type -> Type))
- type WorkerConfigs env (rec :: (b -> Type -> Type) -> k) = rec (ConstFn (WorkerConfig env) :: b -> Type -> Type)
- type Workers env (rec :: (b -> Type -> Type) -> k) = rec (ConstFn (Worker env) :: b -> Type -> Type)
- coerceRec :: forall (c :: Type -> Exp Type) (d :: Type -> Exp Type) rec. AllRec (Equate c d) rec => rec c -> rec d
- class (c @@ a) ~ (d @@ a) => Equate (c :: k1 -> Exp k) (d :: k1 -> Exp k) (a :: k1)
- data ProxySync = ProxySync
- data ProxyAsync = ProxyAsync
- type CanUseAsRefs (f :: (Type -> Exp Type) -> Type) codec = (ConstraintsRec f, WitnessFieldTypes f, AllRec (RefFromFunction codec) f, ApplicativeRec f)
- type CanUseAsDefs (f :: (Type -> Exp Type) -> Type) codec env = (ConstraintsRec f, WitnessFieldTypes f, AllRec (DefFromFunction codec env) f, ApplicativeRec f)
- class RefFromFunction' codec f f => RefFromFunction codec f
- class RefFromFunction' codec f original where
- class DefFromFunction' codec env f f => DefFromFunction codec env f
- class DefFromFunction' codec env f original where
- defFromFunction :: Proxy f -> codec -> String -> original -> Def env @@ original
- class WorkflowRef f where
- type WorkflowArgs f :: [Type]
- type WorkflowResult f
- workflowRef :: f -> KnownWorkflow (WorkflowArgs f) (WorkflowResult f)
- class ActivityRef f where
- type ActivityArgs f :: [Type]
- type ActivityResult f
- activityRef :: f -> KnownActivity (ActivityArgs f) (ActivityResult f)
- type family InnerActivityResult f where ...
- type family ApplyDef f where ...
- type family ApplyRef original f where ...
Turning implementations into references and definitions
refs :: CanUseAsRefs r codec => codec -> Refs r Source #
Produce a record of references to workflows and activities from a record of implementations.
The fields in the records produced by this function can be used to invoke the workflows and activities via the Temporal client or within Temporal workflows.
type Refs (f :: (Type -> Type -> Type) -> k) = f Ref Source #
References to workflows and activities that can be used at invokation callsites.
A reference to a workflow or activity.
Depending on whether the result monad is Workflow
or Activity
,
the Eval
instance will resolve to either a KnownWorkflow
or KnownActivity
.
defs :: CanUseAsDefs f codec env => codec -> Impl f -> Defs env f Source #
Produce a record of definitions from a record of implementations, using the supplied codec.
type Defs env (f :: (Type -> Type -> Type) -> k) = f (Def env) Source #
A wrapped version of the implementations that carries information needed to register workflows and activities with the Temporal worker.
collectTemporalDefinitions :: forall rec actEnv (f :: Type -> Exp Type). (TraversableRec rec, ConstraintsRec rec, AllRecF (ToDefinitions actEnv) f rec) => rec f -> Definitions actEnv Source #
Convert a record of Def
values into a worker and that you can pass to startWorker
.
type Impl (f :: (a -> a -> Type) -> k) = f (Pure :: a -> a -> Type) Source #
A bare record type that supplies the actual implementation of workflows and activities.
inWorkflowProxies :: forall synchronicity rec. (RequireCallStack, ConstraintsRec rec, AllRec (ClassF (UseAsInWorkflowProxy synchronicity) Ref) rec, ApplicativeRec rec) => synchronicity -> rec (RefStartOptions <=< Ref) -> rec Ref -> rec (InWorkflowProxies synchronicity <=< Ref) Source #
Combine a record of references with a record of default options to give a record of
functions that can be used to start activities and workflows. This is just a convenience
function that makes it so that you don't have to call executeActivity
or executeChildWorkflow
directly as often.
data InWorkflowProxies a b c Source #
Instances
type Eval (InWorkflowProxies ProxyAsync (KnownActivity args res) :: Type -> Type) Source # | |
Defined in Temporal.Bundle type Eval (InWorkflowProxies ProxyAsync (KnownActivity args res) :: Type -> Type) = args :->: Workflow (Task res) | |
type Eval (InWorkflowProxies ProxyAsync (KnownWorkflow args res) :: Type -> Type) Source # | |
Defined in Temporal.Bundle type Eval (InWorkflowProxies ProxyAsync (KnownWorkflow args res) :: Type -> Type) = args :->: Workflow (ChildWorkflowHandle res) | |
type Eval (InWorkflowProxies ProxySync (KnownActivity args res) :: Type -> Type) Source # | |
Defined in Temporal.Bundle type Eval (InWorkflowProxies ProxySync (KnownActivity args res) :: Type -> Type) = args :->: Workflow res | |
type Eval (InWorkflowProxies ProxySync (KnownWorkflow args res) :: Type -> Type) Source # | |
Defined in Temporal.Bundle type Eval (InWorkflowProxies ProxySync (KnownWorkflow args res) :: Type -> Type) = args :->: Workflow res |
data RefStartOptions a b Source #
Instances
type Eval (RefStartOptions f :: Type -> Type) Source # | |
Defined in Temporal.Bundle |
class UseAsInWorkflowProxy synchronicity ref where Source #
Methods
useAsInWorkflowProxy :: synchronicity -> ref -> (RefStartOptions @@ ref) -> InWorkflowProxies synchronicity @@ ref Source #
Instances
provideDefaultOptions :: (ApplicativeRec rec, ConstraintsRec rec, AllRec FieldToStartOptionDefaults rec) => StartActivityOptions -> StartChildWorkflowOptions -> rec RefStartOptions Source #
Define a record that provides the same default options for all activities in the a record.
This is useful when you want to provide the same default options for all activities in a record. For example, you might want to provide a default timeout or non-retryable error type list for all activities.
class FieldToStartOptionDefaults f where Source #
Methods
refStartOptionsDefaults :: Proxy f -> StartActivityOptions -> StartChildWorkflowOptions -> RefStartOptionsType f Source #
Instances
type family RefStartOptionsType f where ... Source #
Equations
RefStartOptionsType (Activity _1 _2) = StartActivityOptions | |
RefStartOptionsType (_1 -> b) = RefStartOptionsType b | |
RefStartOptionsType (Workflow _1) = StartChildWorkflowOptions | |
RefStartOptionsType (KnownWorkflow _1 _2) = StartChildWorkflowOptions | |
RefStartOptionsType (KnownActivity _1 _2) = StartActivityOptions |
Worker management
withTaskQueues :: (TraversableRec rec, MonadUnliftIO m, MonadCatch m) => Client -> WorkerConfigs env rec -> (Workers env rec -> m a) -> m a Source #
linkTaskQueues :: forall m rec env. (TraversableRec rec, MonadUnliftIO m) => Workers env rec -> m () Source #
startTaskQueues :: (TraversableRec rec, MonadUnliftIO m, MonadCatch m) => Client -> WorkerConfigs env rec -> m (Workers env rec) Source #
Start a Temporal worker for each task queue specified in the MercuryTaskQueues record.
This function starts each worker concurrently, waits for them to initialize, and then returns a worker for each task queue.
shutdownTaskQueues :: forall m rec env. (TraversableRec rec, MonadUnliftIO m) => Workers env rec -> m (rec (ConstFn (Either SomeException ()) :: Type -> Type -> Type)) Source #
Stop each Temporal worker for each task queue specified in the MercuryTaskQueues record.
This function stops each worker concurrently, waits for them to complete shutdown (gracefully or not), and then returns.
type WorkerConfigs env (rec :: (b -> Type -> Type) -> k) = rec (ConstFn (WorkerConfig env) :: b -> Type -> Type) Source #
type Workers env (rec :: (b -> Type -> Type) -> k) = rec (ConstFn (Worker env) :: b -> Type -> Type) Source #
Other utilities
coerceRec :: forall (c :: Type -> Exp Type) (d :: Type -> Exp Type) rec. AllRec (Equate c d) rec => rec c -> rec d Source #
Constructors
ProxySync |
Instances
data ProxyAsync Source #
Constructors
ProxyAsync |
Instances
Reexports
Constraints
type CanUseAsRefs (f :: (Type -> Exp Type) -> Type) codec = (ConstraintsRec f, WitnessFieldTypes f, AllRec (RefFromFunction codec) f, ApplicativeRec f) Source #
type CanUseAsDefs (f :: (Type -> Exp Type) -> Type) codec env = (ConstraintsRec f, WitnessFieldTypes f, AllRec (DefFromFunction codec env) f, ApplicativeRec f) Source #
Constraints needed to turn a record of implementations into a record of definitions.
That is, all of the fields (Workflows, Activities) in the record support the supplied codec,
and the activities have an environment of type env
.
class RefFromFunction' codec f f => RefFromFunction codec f Source #
Instances
RefFromFunction' codec f f => RefFromFunction codec f Source # | |
Defined in Temporal.Bundle |
class RefFromFunction' codec f original where Source #
Instances
(FunctionSupportsCodec' Workflow codec original, (Ref @@ original) ~ KnownWorkflow (ArgsOf original) (ResultOf Workflow original)) => RefFromFunction' codec (Workflow result) original Source # | |
Defined in Temporal.Bundle | |
(FunctionSupportsCodec' (Activity env) codec original, (Ref @@ original) ~ KnownActivity (ArgsOf original) (ResultOf (Activity env) original)) => RefFromFunction' codec (Activity env result) original Source # | |
Defined in Temporal.Bundle | |
RefFromFunction' codec b original => RefFromFunction' codec (a -> b) original Source # | |
Defined in Temporal.Bundle |
class DefFromFunction' codec env f f => DefFromFunction codec env f Source #
Instances
DefFromFunction' codec env f f => DefFromFunction codec env f Source # | |
Defined in Temporal.Bundle |
class DefFromFunction' codec env f original where Source #
Instances
(FunctionSupportsCodec' Workflow codec original, (Def env @@ original) ~ WorkflowDefinition) => DefFromFunction' codec env (Workflow result) original Source # | |
Defined in Temporal.Bundle | |
(FunctionSupportsCodec' (Activity env) codec original, (Def env @@ original) ~ ActivityDefinition env) => DefFromFunction' codec env (Activity env result) original Source # | |
Defined in Temporal.Bundle | |
DefFromFunction' codec env b original => DefFromFunction' codec env (a -> b) original Source # | |
Defined in Temporal.Bundle |
class WorkflowRef f where Source #
Methods
workflowRef :: f -> KnownWorkflow (WorkflowArgs f) (WorkflowResult f) Source #
Instances
class ActivityRef f where Source #
Methods
activityRef :: f -> KnownActivity (ActivityArgs f) (ActivityResult f) Source #
Instances
(Fn f, ActivityFn f) => ActivityRef (ActivityImpl f) Source # | |||||||||
Defined in Temporal.TH.Classes Associated Types
Methods activityRef :: ActivityImpl f -> KnownActivity (ActivityArgs (ActivityImpl f)) (ActivityResult (ActivityImpl f)) Source # | |||||||||
(TypeError DirectActivityReferenceMsg :: Constraint) => ActivityRef (Activity env a) Source # | |||||||||
Defined in Temporal.Activity.Definition Associated Types
Methods activityRef :: Activity env a -> KnownActivity (ActivityArgs (Activity env a)) (ActivityResult (Activity env a)) Source # | |||||||||
VarArgs args => ActivityRef (KnownActivity args result) Source # | |||||||||
Defined in Temporal.Activity.Definition Associated Types
Methods activityRef :: KnownActivity args result -> KnownActivity (ActivityArgs (KnownActivity args result)) (ActivityResult (KnownActivity args result)) Source # | |||||||||
ActivityRef (ProvidedActivity env f) Source # | |||||||||
Defined in Temporal.Activity.Definition Associated Types
Methods activityRef :: ProvidedActivity env f -> KnownActivity (ActivityArgs (ProvidedActivity env f)) (ActivityResult (ProvidedActivity env f)) Source # | |||||||||
(f ~ (ArgsOf f :->: Activity env (ResultOf (Activity env) f)), TypeError DirectActivityReferenceMsg :: Constraint) => ActivityRef (a -> f) Source # | |||||||||
Defined in Temporal.Activity.Definition Associated Types
Methods activityRef :: (a -> f) -> KnownActivity (ActivityArgs (a -> f)) (ActivityResult (a -> f)) Source # |
type family InnerActivityResult f where ... Source #
Equations
InnerActivityResult (Activity _1 result) = result | |
InnerActivityResult (_1 -> b) = InnerActivityResult b | |
InnerActivityResult a = TypeError ('Text "Expected an Activity, but got " ':<>: 'ShowType a) :: Type |
type family ApplyRef original f where ... Source #
Equations
ApplyRef original (Workflow result) = KnownWorkflow (ArgsOf original) result | |
ApplyRef original (Activity _1 result) = KnownActivity (ArgsOf original) result | |
ApplyRef original (_1 -> b) = ApplyRef original b | |
ApplyRef _1 a = TypeError ('Text "Expected a Workflow or Activity, but got " ':<>: 'ShowType a) :: Type |