Last active
March 1, 2022 18:50
-
-
Save percybolmer/769e3c69d0de6da28243bb20470d3fcc to your computer and use it in GitHub Desktop.
Cadence order workflow beginning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package orders | |
import ( | |
"time" | |
"go.uber.org/cadence/workflow" | |
"go.uber.org/zap" | |
) | |
// Order is a simple type to represent orders made | |
type Order struct { | |
Item string `json:"item"` | |
Price float32 `json:"price"` | |
By string `json:"by"` | |
} | |
func init() { | |
workflow.Register(workflowOrder) | |
} | |
// workflowOrder will handle incomming Orders | |
func workflowOrder(ctx workflow.Context) error { | |
ao := workflow.ActivityOptions{ | |
ScheduleToStartTimeout: time.Minute * 60, | |
StartToCloseTimeout: time.Minute * 60, | |
HeartbeatTimeout: time.Hour * 20, | |
// Here we will Add Retry policies etc later | |
} | |
// Add the Options to Context to apply configurations | |
ctx = workflow.WithActivityOptions(ctx, ao) | |
logger := workflow.GetLogger(ctx) | |
logger.Info("Waiting for Orders") | |
// Grab the Selector from the workflow Context, | |
selector := workflow.NewSelector(ctx) | |
// For ever running loop | |
for { | |
// Get the Signal used to identify an Event, we named our Order event into order | |
signalChan := workflow.GetSignalChannel(ctx, "order") | |
// We add a "Receiver" to the Selector, The receiver is a function that will trigger once a new Signal is recieved | |
selector.AddReceive(signalChan, func(c workflow.Channel, more bool) { | |
// Create the Order to marshal the Input into | |
var order Order | |
// Receive will read input data into the struct | |
c.Receive(ctx, &order) | |
logger.Info("Order made", zap.String("item", order.Item), zap.Float32("price", order.Price)) | |
}) | |
selector.Select(ctx) | |
// TODO Add Signal for Leaving the Tavern, Close this workflow | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment