This is my current process. If creating a machine fails, how does temporary save the state
func ScaleOut(ctx workflow.Context, count int) (ScaleResult, error) {
var (
err error
workflowCtx = workflow.WithActivityOptions(ctx, defaultTaskOptions)
logger = workflow.GetLogger(workflowCtx)
)
var result = ScaleResult{
Success: make([]string, 0),
Failure: make([]string, 0),
Errors: make([]string, 0),
}
// batch creation of virtual machines
// response : {"success": true, servers: ["1.1.1.1", "1.1.1.2"]}
var response components.CreateServerResponse
if err = workflow.ExecuteActivity(workflowCtx, CreateVMServers, count).Get(workflowCtx, &response); err != nil {
result.Errors = append(result.Errors, err.Error())
logger.Error("scale out error", "error", err)
return result, err
}
if !response.Success {
result.Errors = append(result.Errors, fmt.Sprintf("create vm failed [%s]", response.RequestID))
return result, nil
}
selector := workflow.NewSelector(workflowCtx)
childCtx, _ := workflow.WithCancel(ctx)
for _, server := range response.servers {
// create server init flow for each server
f := workflow.ExecuteChildWorkflow(childCtx, ServerInit, server)
selector.AddFuture(f, func(f workflow.Future) {
var resp ScaleServerResult
err := f.Get(childCtx, &resp)
if err != nil {
logger.Error("linux server init error", "error", err.Error())
return
}
if resp.Success {
result.Success = append(result.Success, resp.IP)
return
}
result.Failure = append(result.Failure, resp.ServiceTag)
if resp.Message != "" {
result.Errors = append(result.Errors, resp.Message)
}
})
}
// wait all server finished
for i := 0; i < response.Count(); i++ {
selector.Select(childCtx)
}
return result, nil
}