Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created February 28, 2022 18:36
Show Gist options
  • Save percybolmer/426a871efeb2542996934fb39777cfb2 to your computer and use it in GitHub Desktop.
Save percybolmer/426a871efeb2542996934fb39777cfb2 to your computer and use it in GitHub Desktop.
Cadence workflow for greeting and storing customer information
// workflowGreetings is the Workflow that is used to handle new Customers in the Tavern.
// our Workflow accepts a customer as Input, and Outputs a Customer, and an Error
func workflowGreetings(ctx workflow.Context, visitor customer.Customer) (customer.Customer, error) {
// workflow Options for HeartBeat Timeout and other Timeouts.
ao := workflow.ActivityOptions{
ScheduleToStartTimeout: time.Minute,
StartToCloseTimeout: time.Minute,
HeartbeatTimeout: time.Second * 20,
// Here we will Add Retry policies etc later
}
// Add the Options to Context to apply configurations
ctx = workflow.WithActivityOptions(ctx, ao)
// Grab the Logger that is configured on the Workflow
logger := workflow.GetLogger(ctx)
logger.Info("greetings workflow started")
// Execute the activityGreetings and Wait for the Response with GET
// GET() will Block until the activitiy is Completed.
// Get accepts input to marshal result to,
// ExecuteActivity returns a FUTURE, so if you want async you can simply Skip .Get
// Get takes in a interface{} as input that we can use to Scan the result into.
err := workflow.ExecuteActivity(ctx, activityGreetings, visitor).Get(ctx, &visitor)
if err != nil {
logger.Error("Greetings Activity failed", zap.Error(err))
return customer.Customer{}, err
}
err = workflow.ExecuteActivity(ctx, activityStoreCustomer, visitor).Get(ctx, nil)
if err != nil {
logger.Error("Failed to update customer", zap.Error(err))
return customer.Customer{}, err
}
// The output of the Workflow is a Visitor with filled information
return visitor, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment