Skip to content

Commit b2187cb

Browse files
committed
feat: adds support for workflow initializers
1 parent 384ad77 commit b2187cb

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

internal/plugin/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const (
1717
clientPkg = "go.temporal.io/sdk/client"
1818
enumsPkg = "go.temporal.io/api/enums/v1"
1919
expressionPkg = "github.com/cludden/protoc-gen-go-temporal/pkg/expression"
20+
helpersPkg = "github.com/cludden/protoc-gen-go-temporal/pkg/helpers"
2021
temporalPkg = "go.temporal.io/sdk/temporal"
2122
updatePkg = "go.temporal.io/api/update/v1"
2223
uuidPkg = "github.com/google/uuid"

internal/plugin/worker.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ func (svc *Service) genWorkerBuilderFunction(f *g.File, workflow string) {
9393
}),
9494
)
9595

96+
fn.If(
97+
g.List(g.Id("initializable"), g.Id("ok")).Op(":=").Id("wf").Op(".").Parens(g.Qual(helpersPkg, "Initializable")),
98+
g.Id("ok"),
99+
).Block(
100+
g.If(g.Err().Op(":=").Id("initializable").Dot("Initialize").Call(g.Id("ctx")), g.Err().Op("!=").Nil()).Block(
101+
g.ReturnFunc(func(returnVals *g.Group) {
102+
if hasOutput {
103+
returnVals.Nil()
104+
}
105+
returnVals.Err()
106+
}),
107+
),
108+
)
109+
96110
// register query handlers
97111
for _, q := range opts.GetQuery() {
98112
query := q.GetRef()

pkg/helpers/helpers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package helpers
2+
3+
import "go.temporal.io/sdk/workflow"
4+
5+
// Initializable describes a Workflow that supports initialization prior to registering signal, query, or update handlers
6+
type Initializable interface {
7+
Initialize(ctx workflow.Context) error
8+
}

0 commit comments

Comments
 (0)