mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
Adds a state machine defining valid transitions for a CHASM activity. The state machine does not include transitions related to cancel and pause states: we can add those when we implement those features. Part of foundations for CHASM activity implementation / Standalone Activity. - Built (a unit test was written but not committed)
166 lines
4.4 KiB
Go
166 lines
4.4 KiB
Go
package callback
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
commonpb "go.temporal.io/api/common/v1"
|
|
"go.temporal.io/api/serviceerror"
|
|
"go.temporal.io/server/chasm"
|
|
callbackspb "go.temporal.io/server/chasm/lib/callback/gen/callbackpb/v1"
|
|
"go.temporal.io/server/common/backoff"
|
|
"go.temporal.io/server/common/nexus/nexusrpc"
|
|
queueserrors "go.temporal.io/server/service/history/queues/errors"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
)
|
|
|
|
type CompletionSource interface {
|
|
GetNexusCompletion(ctx chasm.Context, requestID string) (nexusrpc.OperationCompletion, error)
|
|
}
|
|
|
|
var _ chasm.Component = (*Callback)(nil)
|
|
var _ chasm.StateMachine[callbackspb.CallbackStatus] = (*Callback)(nil)
|
|
|
|
// Callback represents a callback component in CHASM.
|
|
type Callback struct {
|
|
chasm.UnimplementedComponent
|
|
|
|
// Persisted internal state
|
|
*callbackspb.CallbackState
|
|
|
|
// Interface to retrieve Nexus operation completion data
|
|
CompletionSource chasm.ParentPtr[CompletionSource]
|
|
}
|
|
|
|
func NewCallback(
|
|
requestID string,
|
|
registrationTime *timestamppb.Timestamp,
|
|
state *callbackspb.CallbackState,
|
|
cb *callbackspb.Callback,
|
|
) *Callback {
|
|
return &Callback{
|
|
CallbackState: &callbackspb.CallbackState{
|
|
RequestId: requestID,
|
|
RegistrationTime: registrationTime,
|
|
Callback: cb,
|
|
Status: callbackspb.CALLBACK_STATUS_STANDBY,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (c *Callback) LifecycleState(_ chasm.Context) chasm.LifecycleState {
|
|
switch c.Status {
|
|
case callbackspb.CALLBACK_STATUS_SUCCEEDED:
|
|
return chasm.LifecycleStateCompleted
|
|
case callbackspb.CALLBACK_STATUS_FAILED:
|
|
return chasm.LifecycleStateFailed
|
|
default:
|
|
return chasm.LifecycleStateRunning
|
|
}
|
|
}
|
|
|
|
func (c *Callback) StateMachineState() callbackspb.CallbackStatus {
|
|
return c.Status
|
|
}
|
|
|
|
func (c *Callback) SetStateMachineState(status callbackspb.CallbackStatus) {
|
|
c.Status = status
|
|
}
|
|
|
|
func (c *Callback) recordAttempt(ts time.Time) {
|
|
c.Attempt++
|
|
c.LastAttemptCompleteTime = timestamppb.New(ts)
|
|
}
|
|
|
|
//nolint:revive // context.Context is an input parameter for chasm.ReadComponent, not a function parameter
|
|
func (c *Callback) loadInvocationArgs(
|
|
ctx chasm.Context,
|
|
_ chasm.NoValue,
|
|
) (callbackInvokable, error) {
|
|
target := c.CompletionSource.Get(ctx)
|
|
|
|
completion, err := target.GetNexusCompletion(ctx, c.RequestId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
variant := c.GetCallback().GetNexus()
|
|
if variant == nil {
|
|
return nil, queueserrors.NewUnprocessableTaskError(
|
|
fmt.Sprintf("unprocessable callback variant: %v", variant),
|
|
)
|
|
}
|
|
|
|
if variant.Url == chasm.NexusCompletionHandlerURL {
|
|
return chasmInvocation{
|
|
nexus: variant,
|
|
attempt: c.Attempt,
|
|
completion: completion,
|
|
requestID: c.RequestId,
|
|
}, nil
|
|
}
|
|
return nexusInvocation{
|
|
nexus: variant,
|
|
completion: completion,
|
|
workflowID: ctx.ExecutionKey().BusinessID,
|
|
runID: ctx.ExecutionKey().RunID,
|
|
attempt: c.Attempt,
|
|
}, nil
|
|
}
|
|
|
|
type saveResultInput struct {
|
|
result invocationResult
|
|
retryPolicy backoff.RetryPolicy
|
|
}
|
|
|
|
func (c *Callback) saveResult(
|
|
ctx chasm.MutableContext,
|
|
input saveResultInput,
|
|
) (chasm.NoValue, error) {
|
|
switch r := input.result.(type) {
|
|
case invocationResultOK:
|
|
err := TransitionSucceeded.Apply(c, ctx, EventSucceeded{Time: ctx.Now(c)})
|
|
return nil, err
|
|
case invocationResultRetry:
|
|
err := TransitionAttemptFailed.Apply(c, ctx, EventAttemptFailed{
|
|
Time: ctx.Now(c),
|
|
Err: r.err,
|
|
RetryPolicy: input.retryPolicy,
|
|
})
|
|
return nil, err
|
|
case invocationResultFail:
|
|
err := TransitionFailed.Apply(c, ctx, EventFailed{
|
|
Time: ctx.Now(c),
|
|
Err: r.err,
|
|
})
|
|
return nil, err
|
|
default:
|
|
return nil, queueserrors.NewUnprocessableTaskError(
|
|
fmt.Sprintf("unrecognized callback result %v", input.result),
|
|
)
|
|
}
|
|
}
|
|
|
|
// ToAPICallback converts a CHASM callback to API callback proto.
|
|
func (c *Callback) ToAPICallback() (*commonpb.Callback, error) {
|
|
// Convert CHASM callback proto to API callback proto
|
|
chasmCB := c.GetCallback()
|
|
res := &commonpb.Callback{
|
|
Links: chasmCB.GetLinks(),
|
|
}
|
|
|
|
// CHASM currently only supports Nexus callbacks
|
|
if variant, ok := chasmCB.Variant.(*callbackspb.Callback_Nexus_); ok {
|
|
res.Variant = &commonpb.Callback_Nexus_{
|
|
Nexus: &commonpb.Callback_Nexus{
|
|
Url: variant.Nexus.GetUrl(),
|
|
Header: variant.Nexus.GetHeader(),
|
|
},
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
// This should not happen as CHASM only supports Nexus callbacks currently
|
|
return nil, serviceerror.NewInternal("unsupported CHASM callback type")
|
|
}
|