Skip to content

Commit da52a6d

Browse files
tsurdiloyuandrew
andauthored
update cancellation sample (#276)
* update cancelation sample Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * update Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * updated sample Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> --------- Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> Co-authored-by: Andrew Yuan <andrew.yuan@temporal.io>
1 parent ada9d4a commit da52a6d

2 files changed

Lines changed: 10 additions & 14 deletions

File tree

cancellation/activity.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (a *Activities) ActivityToBeCanceled(ctx context.Context) (string, error) {
2121
activity.RecordHeartbeat(ctx, "")
2222
case <-ctx.Done():
2323
logger.Info("context is cancelled")
24-
return "I am canceled by Done", nil
24+
return "", ctx.Err()
2525
}
2626
}
2727
}
@@ -31,11 +31,4 @@ func (a *Activities) CleanupActivity(ctx context.Context) error {
3131
logger.Info("Cleanup Activity started")
3232
return nil
3333
}
34-
35-
func (a *Activities) ActivityToBeSkipped(ctx context.Context) error {
36-
logger := activity.GetLogger(ctx)
37-
logger.Info("this Activity will be skipped due to cancellation")
38-
return nil
39-
}
40-
4134
// @@@SNIPEND

cancellation/workflow.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cancellation
22

33
import (
44
"errors"
5-
"fmt"
65
"go.temporal.io/sdk/workflow"
76
"time"
87
)
@@ -26,21 +25,25 @@ func YourWorkflow(ctx workflow.Context) error {
2625
}
2726

2827
// When the Workflow is canceled, it has to get a new disconnected context to execute any Activities
28+
// Specify activity options for cleanup activity
29+
aoCleanup := workflow.ActivityOptions{
30+
StartToCloseTimeout: 2 * time.Second,
31+
}
2932
newCtx, _ := workflow.NewDisconnectedContext(ctx)
33+
newCtx = workflow.WithActivityOptions(newCtx, aoCleanup)
3034
err := workflow.ExecuteActivity(newCtx, a.CleanupActivity).Get(ctx, nil)
3135
if err != nil {
3236
logger.Error("CleanupActivity failed", "Error", err)
3337
}
38+
3439
}()
3540

3641
var result string
3742
err := workflow.ExecuteActivity(ctx, a.ActivityToBeCanceled).Get(ctx, &result)
38-
logger.Info(fmt.Sprintf("ActivityToBeCanceled returns %v, %v", result, err))
39-
40-
err = workflow.ExecuteActivity(ctx, a.ActivityToBeSkipped).Get(ctx, nil)
41-
logger.Error("Error from ActivityToBeSkipped", "Error", err)
43+
if err != nil {
44+
return err
45+
}
4246

4347
logger.Info("Workflow Execution complete.")
44-
4548
return nil
4649
}

0 commit comments

Comments
 (0)