mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
Goes from 135 MB binary with 1153 files importing go.temporal.io and 224
importing google.golang.org/grpc to a 10 MB native binary with ZERO of
either.
Deleted:
- tasks/, tasktests/, service/, chasm/, client/, api/, proto/, tools/
- cmd/tools/, components/, docker/
- schema/{cassandra,elasticsearch,mysql,postgresql,sqlite}/
- 40 tainted common/ subdirs + 7 top-level common files
- tests/testutils (no longer referenced after surviving deps drop common)
New cmd/tasksd (~100 lines) boots tasks.Embed (zap.Node) + http.Server.
New pkg/tasks/embed.go registers ZAP handlers for the SDK opcode surface
(0x0060-0x00A5). Workflow handlers return a 501 "not yet implemented"
envelope; native engine lands behind this contract.
Build: go build ./cmd/tasksd → 10 MB
Vet: go vet ./... → clean
Boot: tasksd --zap-port 9999 --http :7243
Probes: /healthz, /v1/tasks/health → 200
UI: /_/tasks/, / → 200 (embedded Vite bundle)
ZAP: _tasks._tcp on :9999 (mDNS disabled; direct connect)
go.mod: tidy. No go.temporal.io. No google.golang.org/grpc.
182 lines
4.9 KiB
Go
182 lines
4.9 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.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,
|
|
) (invocable, error) {
|
|
target := c.CompletionSource.Get(ctx)
|
|
|
|
completion, err := target.GetNexusCompletion(ctx, c.RequestId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
callback := c.GetCallback().GetNexus()
|
|
if callback == nil {
|
|
return nil, queueserrors.NewUnprocessableTaskError(
|
|
fmt.Sprintf("unprocessable callback variant: %v", callback),
|
|
)
|
|
}
|
|
|
|
if callback.Url == chasm.NexusCompletionHandlerURL {
|
|
return invocableInternal{
|
|
callback: callback,
|
|
attempt: c.Attempt,
|
|
completion: completion,
|
|
requestID: c.RequestId,
|
|
}, nil
|
|
}
|
|
return invocableOutbound{
|
|
callback: callback,
|
|
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")
|
|
}
|
|
|
|
// ScheduleStandbyCallbacks transitions all STANDBY callbacks to SCHEDULED state,
|
|
// triggering their invocation. Used by both workflows and standalone activities
|
|
// when the execution reaches a terminal state.
|
|
func ScheduleStandbyCallbacks(ctx chasm.MutableContext, callbacks chasm.Map[string, *Callback]) error {
|
|
for _, field := range callbacks {
|
|
cb := field.Get(ctx)
|
|
if cb.Status != callbackspb.CALLBACK_STATUS_STANDBY {
|
|
continue
|
|
}
|
|
if err := TransitionScheduled.Apply(cb, ctx, EventScheduled{}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|