Files
tasks/service/history/api/respondactivitytaskcanceled/api.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
Full module path rebrand from Temporal to Hanzo Tasks:
- go.mod module: github.com/hanzoai/tasks
- All internal Go imports updated (1906 files)
- cmd/server renamed to cmd/tasksd
- Binary name: tasksd (was temporal-server)
- Docker images: hanzoai/tasks (was temporalio/server)
- README rewritten with Hanzo Tasks branding
- LLM.md created with integration notes
- Makefile, goreleaser, CI workflows updated
- go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos)

Fork of Temporal.io (MIT license).
2026-03-19 10:36:40 -07:00

131 lines
4.2 KiB
Go

package respondactivitytaskcanceled
import (
"context"
"time"
enumspb "go.temporal.io/api/enums/v1"
"github.com/hanzoai/tasks/api/historyservice/v1"
"github.com/hanzoai/tasks/common"
"github.com/hanzoai/tasks/common/definition"
"github.com/hanzoai/tasks/common/metrics"
"github.com/hanzoai/tasks/common/namespace"
"github.com/hanzoai/tasks/common/tasktoken"
"github.com/hanzoai/tasks/service/history/api"
"github.com/hanzoai/tasks/service/history/consts"
historyi "github.com/hanzoai/tasks/service/history/interfaces"
"github.com/hanzoai/tasks/service/history/workflow"
)
func Invoke(
ctx context.Context,
req *historyservice.RespondActivityTaskCanceledRequest,
shard historyi.ShardContext,
workflowConsistencyChecker api.WorkflowConsistencyChecker,
) (resp *historyservice.RespondActivityTaskCanceledResponse, retError error) {
request := req.CancelRequest
tokenSerializer := tasktoken.NewSerializer()
token, err0 := tokenSerializer.Deserialize(request.TaskToken)
if err0 != nil {
return nil, consts.ErrDeserializingToken
}
namespaceEntry, err := api.GetActiveNamespace(shard, namespace.ID(req.GetNamespaceId()), token.WorkflowId)
if err != nil {
return nil, err
}
namespaceName := namespaceEntry.Name()
if err := api.SetActivityTaskRunID(ctx, token, workflowConsistencyChecker); err != nil {
return nil, err
}
var attemptStartedTime time.Time
var firstScheduledTime time.Time
var taskQueue string
var workflowTypeName string
var versioningBehavior enumspb.VersioningBehavior
err = api.GetAndUpdateWorkflowWithNew(
ctx,
token.Clock,
definition.NewWorkflowKey(
token.NamespaceId,
token.WorkflowId,
token.RunId,
),
func(workflowLease api.WorkflowLease) (*api.UpdateWorkflowAction, error) {
mutableState := workflowLease.GetMutableState()
workflowTypeName = mutableState.GetWorkflowType().GetName()
if !mutableState.IsWorkflowExecutionRunning() {
return nil, consts.ErrWorkflowCompleted
}
scheduledEventID := token.GetScheduledEventId()
if scheduledEventID == common.EmptyEventID { // client call CompleteActivityById, so get scheduledEventID by activityID
scheduledEventID, err0 = api.GetActivityScheduledEventID(token.GetActivityId(), mutableState)
if err0 != nil {
return nil, err0
}
}
ai, isRunning := mutableState.GetActivityInfo(scheduledEventID)
// First check to see if cache needs to be refreshed as we could potentially have stale workflow execution in
// some extreme cassandra failure cases.
if !isRunning && scheduledEventID >= mutableState.GetNextEventID() {
metrics.StaleMutableStateCounter.With(shard.GetMetricsHandler()).Record(
1,
metrics.OperationTag(metrics.HistoryRespondActivityTaskCanceledScope))
return nil, consts.ErrStaleState
}
if !isRunning || api.IsActivityTaskNotFoundForToken(token, ai, nil) {
return nil, consts.ErrActivityTaskNotFound
}
// sanity check if activity is requested to be cancelled
if !ai.CancelRequested {
return nil, consts.ErrActivityTaskNotCancelRequested
}
if _, err := mutableState.AddActivityTaskCanceledEvent(
scheduledEventID,
ai.StartedEventId,
ai.CancelRequestId,
request.Details,
request.Identity); err != nil {
// Unable to add ActivityTaskCanceled event to history
return nil, err
}
attemptStartedTime = ai.StartedTime.AsTime()
firstScheduledTime = ai.FirstScheduledTime.AsTime()
taskQueue = ai.TaskQueue
versioningBehavior = mutableState.GetEffectiveVersioningBehavior()
return &api.UpdateWorkflowAction{
Noop: false,
CreateWorkflowTask: true,
}, nil
},
nil,
shard,
workflowConsistencyChecker,
)
if err == nil {
workflow.RecordActivityCompletionMetrics(
shard,
namespaceName,
taskQueue,
workflow.ActivityCompletionMetrics{
Status: workflow.ActivityStatusCanceled,
AttemptStartedTime: attemptStartedTime,
FirstScheduledTime: firstScheduledTime,
Closed: true,
},
metrics.OperationTag(metrics.HistoryRespondActivityTaskCanceledScope),
metrics.WorkflowTypeTag(workflowTypeName),
metrics.ActivityTypeTag(token.ActivityType),
metrics.VersioningBehaviorTag(versioningBehavior))
}
return &historyservice.RespondActivityTaskCanceledResponse{}, err
}