Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active March 4, 2022 06:00
Show Gist options
  • Save percybolmer/e10b4ae2455c885f74b7e6b8b0267fc0 to your computer and use it in GitHub Desktop.
Save percybolmer/e10b4ae2455c885f74b7e6b8b0267fc0 to your computer and use it in GitHub Desktop.
Cadence create the client to manage workflows
const (
cadenceClientName = "cadence-client"
cadenceService = "cadence-frontend"
)
const (
// The names of the Workflows we will be using
OrderWorkflow = "programmingpercy/cadence-tavern/workflows/orders.WorkflowOrder"
GreetingsWorkflow = "programmingpercy/cadence-tavern/workflows/greetings.workflowGreetings"
)
type CadenceClient struct {
//dispatcher used to communicate
dispatcher *yarpc.Dispatcher
// wfClient is the workflow Client
wfClient workflowserviceclient.Interface
// client is the client used for cadence
client client.Client
// orderWorkflowID is used to remember the workflow id
orderWorkflowID string
// orderWorkflowRunID is the run id of the order workflow
orderWorkflowRunID string
}
// SetupCadenceClient is used to create the client we can use
func SetupCadenceClient() (*CadenceClient, error) {
// Create a dispatcher used to communicate with server
dispatcher := yarpc.NewDispatcher(yarpc.Config{
Name: cadenceClientName,
Outbounds: yarpc.Outbounds{
// This shouldnt be hard coded in real app
// This is a map, so we store this communication channel on "cadence-frontend"
cadenceService: {Unary: grpc.NewTransport().NewSingleOutbound("localhost:7833")},
},
})
// Start dispatcher
if err := dispatcher.Start(); err != nil {
return nil, err
}
// Grab the Configurations from the Dispatcher based on cadenceService name
yarpConfig := dispatcher.ClientConfig(cadenceService)
// Build the workflowserviceClient that handles the workflows
wfClient := workflowserviceclient.New(yarpConfig)
// clientoptions used to control metrics etc
opts := &client.Options{
MetricsScope: tally.NoopScope, // Do nothing for now
}
// Build the Cadence Client
cadenceClient := client.NewClient(wfClient, "tavern", opts)
return &CadenceClient{
dispatcher: dispatcher,
wfClient: wfClient,
client: cadenceClient,
}, nil
}
// SetOrderWorkflowIds is used to store workflows IDS in Memory
func (cc *CadenceClient) SetOrderWorkflowIds(id, runID string) {
cc.orderWorkflowID = id
cc.orderWorkflowRunID = runID
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment