Files
tasks/chasm/lib/callback/component.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

166 lines
4.4 KiB
Go

package callback
import (
"fmt"
"time"
commonpb "go.temporal.io/api/common/v1"
"go.temporal.io/api/serviceerror"
"github.com/hanzoai/tasks/chasm"
callbackspb "github.com/hanzoai/tasks/chasm/lib/callback/gen/callbackpb/v1"
"github.com/hanzoai/tasks/common/backoff"
"github.com/hanzoai/tasks/common/nexus/nexusrpc"
queueserrors "github.com/hanzoai/tasks/service/history/queues/errors"
"google.golang.org/protobuf/types/known/timestamppb"
)
type CompletionSource interface {
GetNexusCompletion(ctx chasm.Context, requestID string) (nexusrpc.CompleteOperationOptions, 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")
}