Skip to content

Commit 45b3421

Browse files
committed
chore: add WithContext() option for engine.Invoke()
1 parent 9560aa9 commit 45b3421

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

pkg/workflow/engineimpl.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package workflow
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67
"net/url"
@@ -37,9 +38,10 @@ type EngineImpl struct {
3738
var _ Engine = (*EngineImpl)(nil)
3839

3940
type engineRuntimeConfig struct {
40-
config configuration.Configuration
41-
input []Data
42-
ic analytics.InstrumentationCollector
41+
config configuration.Configuration
42+
input []Data
43+
ic analytics.InstrumentationCollector
44+
ctxFunc func() context.Context
4345
}
4446

4547
type EngineInvokeOption func(*engineRuntimeConfig)
@@ -62,6 +64,12 @@ func WithInstrumentationCollector(ic analytics.InstrumentationCollector) EngineI
6264
}
6365
}
6466

67+
func WithContext(ctx context.Context) EngineInvokeOption {
68+
return func(e *engineRuntimeConfig) {
69+
e.ctxFunc = func() context.Context { return ctx }
70+
}
71+
}
72+
6573
func (e *EngineImpl) GetLogger() *zerolog.Logger {
6674
return e.logger
6775
}
@@ -321,11 +329,11 @@ func (e *EngineImpl) Invoke(
321329
e.mu.Unlock()
322330

323331
// create a context object for the invocation
324-
context := NewInvocationContext(id, options.config, localEngine, localNetworkAccess, localLogger, localAnalytics, localUi)
332+
invocationCtx := NewInvocationContext(options.ctxFunc, id, options.config, localEngine, localNetworkAccess, localLogger, localAnalytics, localUi)
325333

326334
// invoke workflow through its callback
327335
localLogger.Printf("Workflow Start")
328-
output, err = callback(context, options.input)
336+
output, err = callback(invocationCtx, options.input)
329337
localLogger.Printf("Workflow End")
330338
}
331339
} else {

pkg/workflow/invocationcontextimpl.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
)
1515

1616
func NewInvocationContext(
17+
ctxFunc func() context.Context,
1718
id Identifier,
1819
config configuration.Configuration,
1920
engine Engine,
@@ -22,7 +23,11 @@ func NewInvocationContext(
2223
analyticsImpl analytics.Analytics,
2324
ui ui.UserInterface,
2425
) InvocationContext {
26+
if ctxFunc == nil {
27+
ctxFunc = context.Background
28+
}
2529
return &invocationContextImpl{
30+
ctxFunc: ctxFunc,
2631
WorkflowID: id,
2732
Configuration: config,
2833
WorkflowEngine: engine,
@@ -35,6 +40,7 @@ func NewInvocationContext(
3540

3641
// invocationContextImpl is the default implementation of the InvocationContext interface.
3742
type invocationContextImpl struct {
43+
ctxFunc func() context.Context
3844
WorkflowID Identifier
3945
WorkflowEngine Engine
4046
Configuration configuration.Configuration
@@ -47,10 +53,8 @@ type invocationContextImpl struct {
4753
var _ InvocationContext = (*invocationContextImpl)(nil)
4854

4955
// Context returns the context of the workflow that is being invoked.
50-
func (*invocationContextImpl) Context() context.Context {
51-
// TODO: This is using context.Background() as a placeholder. Ideally this returns
52-
// the context representing the lifecycle of the workflow that is being invoked.
53-
return context.Background()
56+
func (ici *invocationContextImpl) Context() context.Context {
57+
return ici.ctxFunc()
5458
}
5559

5660
// GetWorkflowIdentifier returns the identifier of the workflow that is being invoked.

0 commit comments

Comments
 (0)