Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update cancellation sample #276

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions cancellation/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (a *Activities) ActivityToBeCanceled(ctx context.Context) (string, error) {
activity.RecordHeartbeat(ctx, "")
case <-ctx.Done():
logger.Info("context is cancelled")
return "I am canceled by Done", nil
return "", ctx.Err()
}
}
}
Expand All @@ -31,11 +31,4 @@ func (a *Activities) CleanupActivity(ctx context.Context) error {
logger.Info("Cleanup Activity started")
return nil
}

func (a *Activities) ActivityToBeSkipped(ctx context.Context) error {
logger := activity.GetLogger(ctx)
logger.Info("this Activity will be skipped due to cancellation")
return nil
}

// @@@SNIPEND
15 changes: 9 additions & 6 deletions cancellation/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cancellation

import (
"errors"
"fmt"
"go.temporal.io/sdk/workflow"
"time"
)
Expand All @@ -26,21 +25,25 @@ func YourWorkflow(ctx workflow.Context) error {
}

// When the Workflow is canceled, it has to get a new disconnected context to execute any Activities
// Specify activity options for cleanup activity
aoCleanup := workflow.ActivityOptions{
StartToCloseTimeout: 2 * time.Second,
}
newCtx, _ := workflow.NewDisconnectedContext(ctx)
newCtx = workflow.WithActivityOptions(newCtx, aoCleanup)
err := workflow.ExecuteActivity(newCtx, a.CleanupActivity).Get(ctx, nil)
if err != nil {
logger.Error("CleanupActivity failed", "Error", err)
}

}()

var result string
err := workflow.ExecuteActivity(ctx, a.ActivityToBeCanceled).Get(ctx, &result)
logger.Info(fmt.Sprintf("ActivityToBeCanceled returns %v, %v", result, err))

err = workflow.ExecuteActivity(ctx, a.ActivityToBeSkipped).Get(ctx, nil)
logger.Error("Error from ActivityToBeSkipped", "Error", err)
if err != nil {
return err
}

logger.Info("Workflow Execution complete.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unreachable code, but in this case I think it's fine


return nil
}