Last active
June 5, 2021 00:14
-
-
Save andrewloux/91478d1f2c83531a5d8334cf08d3cc33 to your computer and use it in GitHub Desktop.
Playing around with propagating Temporal WorkflowRun context cancellations
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
type WorkflowRun interface { | |
client.WorkflowRun | |
} | |
type customWorkflowRun struct { | |
client.WorkflowRun | |
cancelWorkflowFunc context.CancelFunc | |
} | |
type Client interface { | |
client.Client | |
} | |
type customClient struct { | |
client.Client | |
} | |
func NewClient(opts client.Options) (Client, error) { | |
c, err := client.NewClient(opts) | |
if err != nil { | |
return nil, err | |
} | |
return &customClient{c}, nil | |
} | |
func (c *customClient) ExecuteWorkflow(ctx context.Context, options client.StartWorkflowOptions, workflow interface{}, args ...interface{}) (client.WorkflowRun, error) { | |
run, err := c.Client.ExecuteWorkflow(ctx, options, workflow, args...) | |
if err != nil { | |
return nil, err | |
} | |
return &customWorkflowRun{ | |
WorkflowRun: run, | |
cancelWorkflowFunc: c.cancelWorkflowFunc(run.GetID()), | |
}, nil | |
} | |
func (c *customClient) cancelWorkflowFunc(workflowID string) context.CancelFunc { | |
return func() { | |
// should use a disconnected context, probably | |
c.CancelWorkflow(context.Background(), workflowID, "") | |
} | |
} | |
func (r *customWorkflowRun) Get(ctx context.Context, valuePtr interface{}) error { | |
defer r.cancelWorkflowFunc() | |
c := make(chan error, 1) | |
// asynchronously start the Get operation | |
go func() { c <- r.WorkflowRun.Get(ctx, valuePtr) }() | |
select { | |
case <-ctx.Done(): | |
return ctx.Err() | |
case err := <-c: | |
return err | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment