temporal-sdk
Safe HaskellNone
LanguageHaskell2010

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

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.

data Ref a b Source #

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.

Instances

Instances details
type Eval (Ref f :: Type -> Type) Source # 
Instance details

Defined in Temporal.Bundle

type Eval (Ref f :: Type -> Type) = ApplyRef f f

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.

data Def a b c Source #

Instances

Instances details
type Eval (Def _1 f :: Type -> Type) Source # 
Instance details

Defined in Temporal.Bundle

type Eval (Def _1 f :: Type -> Type) = ApplyDef f

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

Instances details
type Eval (InWorkflowProxies ProxyAsync (KnownActivity args res) :: Type -> Type) Source # 
Instance details

Defined in Temporal.Bundle

type Eval (InWorkflowProxies ProxyAsync (KnownWorkflow args res) :: Type -> Type) Source # 
Instance details

Defined in Temporal.Bundle

type Eval (InWorkflowProxies ProxySync (KnownActivity args res) :: Type -> Type) Source # 
Instance details

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 # 
Instance details

Defined in Temporal.Bundle

type Eval (InWorkflowProxies ProxySync (KnownWorkflow args res) :: Type -> Type) = args :->: Workflow res

data RefStartOptions a b Source #

Instances

Instances details
type Eval (RefStartOptions f :: Type -> Type) Source # 
Instance details

Defined in Temporal.Bundle

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 #

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 #

class (c @@ a) ~ (d @@ a) => Equate (c :: k1 -> Exp k) (d :: k1 -> Exp k) (a :: k1) Source #

Instances

Instances details
(c @@ a) ~ (d @@ a) => Equate (c :: k1 -> Exp k) (d :: k1 -> Exp k) (a :: k1) Source # 
Instance details

Defined in Temporal.Bundle

data ProxySync Source #

Constructors

ProxySync 

Instances

Instances details
VarArgs args => UseAsInWorkflowProxy ProxySync (KnownActivity args res) Source # 
Instance details

Defined in Temporal.Bundle

UseAsInWorkflowProxy ProxySync (KnownWorkflow args res) Source # 
Instance details

Defined in Temporal.Bundle

type Eval (InWorkflowProxies ProxySync (KnownActivity args res) :: Type -> Type) Source # 
Instance details

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 # 
Instance details

Defined in Temporal.Bundle

type Eval (InWorkflowProxies ProxySync (KnownWorkflow args res) :: Type -> Type) = args :->: Workflow res

data ProxyAsync Source #

Constructors

ProxyAsync 

Instances

Instances details
VarArgs args => UseAsInWorkflowProxy ProxyAsync (KnownActivity args res) Source # 
Instance details

Defined in Temporal.Bundle

UseAsInWorkflowProxy ProxyAsync (KnownWorkflow args res) Source # 
Instance details

Defined in Temporal.Bundle

type Eval (InWorkflowProxies ProxyAsync (KnownActivity args res) :: Type -> Type) Source # 
Instance details

Defined in Temporal.Bundle

type Eval (InWorkflowProxies ProxyAsync (KnownWorkflow args res) :: Type -> Type) Source # 
Instance details

Defined in Temporal.Bundle

Reexports

Constraints

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

Instances details
RefFromFunction' codec f f => RefFromFunction codec f Source # 
Instance details

Defined in Temporal.Bundle

class RefFromFunction' codec f original where Source #

Methods

refFromFunction :: Proxy f -> codec -> String -> Proxy original -> Ref @@ original Source #

Instances

Instances details
(FunctionSupportsCodec' Workflow codec original, (Ref @@ original) ~ KnownWorkflow (ArgsOf original) (ResultOf Workflow original)) => RefFromFunction' codec (Workflow result) original Source # 
Instance details

Defined in Temporal.Bundle

Methods

refFromFunction :: Proxy (Workflow result) -> codec -> String -> Proxy original -> Ref @@ original Source #

(FunctionSupportsCodec' (Activity env) codec original, (Ref @@ original) ~ KnownActivity (ArgsOf original) (ResultOf (Activity env) original)) => RefFromFunction' codec (Activity env result) original Source # 
Instance details

Defined in Temporal.Bundle

Methods

refFromFunction :: Proxy (Activity env result) -> codec -> String -> Proxy original -> Ref @@ original Source #

RefFromFunction' codec b original => RefFromFunction' codec (a -> b) original Source # 
Instance details

Defined in Temporal.Bundle

Methods

refFromFunction :: Proxy (a -> b) -> codec -> String -> Proxy original -> Ref @@ original Source #

class DefFromFunction' codec env f f => DefFromFunction codec env f Source #

Instances

Instances details
DefFromFunction' codec env f f => DefFromFunction codec env f Source # 
Instance details

Defined in Temporal.Bundle

class DefFromFunction' codec env f original where Source #

Methods

defFromFunction :: Proxy f -> codec -> String -> original -> Def env @@ original Source #

Instances

Instances details
(FunctionSupportsCodec' Workflow codec original, (Def env @@ original) ~ WorkflowDefinition) => DefFromFunction' codec env (Workflow result) original Source # 
Instance details

Defined in Temporal.Bundle

Methods

defFromFunction :: Proxy (Workflow result) -> codec -> String -> original -> Def env @@ original Source #

(FunctionSupportsCodec' (Activity env) codec original, (Def env @@ original) ~ ActivityDefinition env) => DefFromFunction' codec env (Activity env result) original Source # 
Instance details

Defined in Temporal.Bundle

Methods

defFromFunction :: Proxy (Activity env result) -> codec -> String -> original -> Def env @@ original Source #

DefFromFunction' codec env b original => DefFromFunction' codec env (a -> b) original Source # 
Instance details

Defined in Temporal.Bundle

Methods

defFromFunction :: Proxy (a -> b) -> codec -> String -> original -> Def env @@ original Source #

class WorkflowRef f where Source #

Associated Types

type WorkflowArgs f :: [Type] Source #

type WorkflowResult f Source #

Instances

Instances details
(Fn f, WorkflowFn f) => WorkflowRef (WorkflowImpl f) Source # 
Instance details

Defined in Temporal.TH.Classes

Associated Types

type WorkflowArgs (WorkflowImpl f) 
Instance details

Defined in Temporal.TH.Classes

type WorkflowResult (WorkflowImpl f) 
Instance details

Defined in Temporal.TH.Classes

VarArgs (ArgsOf f) => WorkflowRef (ProvidedWorkflow f) Source # 
Instance details

Defined in Temporal.Workflow.Definition

WorkflowRef (KnownWorkflow args result) Source # 
Instance details

Defined in Temporal.Workflow.Definition

Associated Types

type WorkflowArgs (KnownWorkflow args result) 
Instance details

Defined in Temporal.Workflow.Definition

type WorkflowArgs (KnownWorkflow args result) = args
type WorkflowResult (KnownWorkflow args result) 
Instance details

Defined in Temporal.Workflow.Definition

type WorkflowResult (KnownWorkflow args result) = result

Methods

workflowRef :: KnownWorkflow args result -> KnownWorkflow (WorkflowArgs (KnownWorkflow args result)) (WorkflowResult (KnownWorkflow args result)) Source #

class ActivityRef f where Source #

Associated Types

type ActivityArgs f :: [Type] Source #

type ActivityResult f Source #

Instances

Instances details
(Fn f, ActivityFn f) => ActivityRef (ActivityImpl f) Source # 
Instance details

Defined in Temporal.TH.Classes

Associated Types

type ActivityArgs (ActivityImpl f) 
Instance details

Defined in Temporal.TH.Classes

type ActivityResult (ActivityImpl f) 
Instance details

Defined in Temporal.TH.Classes

(TypeError DirectActivityReferenceMsg :: Constraint) => ActivityRef (Activity env a) Source # 
Instance details

Defined in Temporal.Activity.Definition

Associated Types

type ActivityArgs (Activity env a) 
Instance details

Defined in Temporal.Activity.Definition

type ActivityArgs (Activity env a) = '[] :: [Type]
type ActivityResult (Activity env a) 
Instance details

Defined in Temporal.Activity.Definition

type ActivityResult (Activity env a) = a
VarArgs args => ActivityRef (KnownActivity args result) Source # 
Instance details

Defined in Temporal.Activity.Definition

Associated Types

type ActivityArgs (KnownActivity args result) 
Instance details

Defined in Temporal.Activity.Definition

type ActivityArgs (KnownActivity args result) = args
type ActivityResult (KnownActivity args result) 
Instance details

Defined in Temporal.Activity.Definition

type ActivityResult (KnownActivity args result) = result

Methods

activityRef :: KnownActivity args result -> KnownActivity (ActivityArgs (KnownActivity args result)) (ActivityResult (KnownActivity args result)) Source #

ActivityRef (ProvidedActivity env f) Source # 
Instance details

Defined in Temporal.Activity.Definition

Associated Types

type ActivityArgs (ProvidedActivity env f) 
Instance details

Defined in Temporal.Activity.Definition

type ActivityResult (ProvidedActivity env f) 
Instance details

Defined in Temporal.Activity.Definition

(f ~ (ArgsOf f :->: Activity env (ResultOf (Activity env) f)), TypeError DirectActivityReferenceMsg :: Constraint) => ActivityRef (a -> f) Source # 
Instance details

Defined in Temporal.Activity.Definition

Associated Types

type ActivityArgs (a -> f) 
Instance details

Defined in Temporal.Activity.Definition

type ActivityArgs (a -> f) = '[] :: [Type]
type ActivityResult (a -> f) 
Instance details

Defined in Temporal.Activity.Definition

type ActivityResult (a -> f) = ()

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 ApplyDef f where ... Source #

Equations

ApplyDef (Workflow _1) = WorkflowDefinition 
ApplyDef (Activity env _1) = ActivityDefinition env 
ApplyDef (_1 -> b) = ApplyDef b 
ApplyDef a = TypeError ('Text "Expected a Workflow or 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