Skip to content

Commit df31e88

Browse files
af-mdmaxdml
authored andcommitted
docs: enhance documentation for Go function, detailing its usage and step ID generation
1 parent 81e86be commit df31e88

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

dbos/workflow.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,21 @@ func (c *dbosContext) RunAsStep(_ DBOSContext, fn StepFunc, opts ...StepOption)
12681268
return stepOutput, stepError
12691269
}
12701270

1271-
// TODO: Add docs --- will add once I get the implementation right
1271+
// Go runs a step inside a Go routine and returns a channel to receive the result.
1272+
// Go generates a deterministic step ID for the step before running the step in a routine, since routines are not deterministic.
1273+
// The step ID is used to track the steps within the same workflow and use the step ID to perform recovery.
1274+
// The folliwing examples shows how to use Go:
1275+
//
1276+
// resultChan, err := dbos.Go(ctx, func(ctx context.Context) (string, error) {
1277+
// return "Hello, World!", nil
1278+
// })
1279+
//
1280+
// resultChan := <-resultChan // wait for the channel to receive
1281+
// if resultChan.err != nil {
1282+
// // Handle error
1283+
// }
1284+
// result := resultChan.result
1285+
//
12721286
func Go[R any](ctx DBOSContext, fn Step[R], opts ...StepOption) (chan stepOutcome[R], error) {
12731287
// create a determistic step ID
12741288
stepName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
@@ -1279,7 +1293,7 @@ func Go[R any](ctx DBOSContext, fn Step[R], opts ...StepOption) (chan stepOutcom
12791293
stepID := wfState.nextStepID()
12801294
opts = append(opts, WithNextStepID(stepID))
12811295

1282-
// run step inside a Go routine by passing stepID
1296+
// run step inside a Go routine by passing a stepID
12831297
result := make(chan stepOutcome[R], 1)
12841298
go func() {
12851299
res, err := RunAsStep(ctx, fn, opts...)

0 commit comments

Comments
 (0)