The workflow I am working on requires different cleanup procedures depending on how much has been done (aka what activities have been completed).
My current solution looks something like this:
type activityOutput struct {
Executed bool
}
func MyWorkflow(ctx workflow.Context, name string) (string, error) {
outA := &activityOutput{}
outB := &activityOutput{}
outC := &activityOutput{}
defer func() {
if !errors.Is(ctx.Err(), workflow.ErrCanceled) {
return
}
if outA.Executed {
logger.Info("activity A finished")
// do something...
}
if outB.Executed {
logger.Info("activity B finished")
// do something...
}
if outC.Executed {
logger.Info("activity C finished")
// do something...
}
return
}()
err := workflow.ExecuteActivity(
workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
StartToCloseTimeout: 100 * time.Second,
WaitForCancellation: true,
}),
ActivityA, nil).Get(ctx, outA)
// same for ActivityB and ActivityC
}
Each activity will return activityOutput
with Executed
set to true
. I enabled WaitForCancellation
so that a in-progress activity can finish upon cancellation.
Is there a better way to determine if an activity has been executed in the workflow cancellation cleanup function? Thank you!