Documentation
¶
Overview ¶
Package nodes_dispatch provides the `dispatch.run` action, which tries explicitly listed actions in order and returns the first successful result.
Typical use:
dispatch.run @{ members: [runtime.fail, runtime.const], payload: { value: "x" } }
Index ¶
Constants ¶
View Source
const ID = "nodes_dispatch"
Variables ¶
View Source
var Dispatch = action.New("dispatch.run", func(ctx context.Context, req DispatchReq) (any, error) { resolver := contracts.ActionResolverFromContext(ctx) if resolver == nil { return nil, xerr.Internal("dispatch: no action resolver in context") } if len(req.Members) == 0 { return nil, xerr.NotFound("dispatch: no member succeeded") } var lastErr error candidates := make([]action.AnyAction, 0, len(req.Members)) for _, name := range req.Members { member := name candidates = append(candidates, action.New("dispatch.member", func(_ context.Context, payload any) (any, error) { target, ok := resolver.Action(member) if !ok { lastErr = xerr.NotFound("dispatch: member not registered: " + member) return nil, lastErr } output, err := action.InvokeAny(ctx, target, payload) if err != nil { lastErr = err } return output, err }).Build()) } firstSuccess := action.FirstSuccessAny("dispatch.run", candidates...).Build() output, err := action.InvokeAny(context.WithoutCancel(ctx), firstSuccess, req.Payload) if err != nil { if lastErr != nil { return nil, lastErr } return nil, err } return output, nil }).Description("Tries each explicitly listed action in order until one succeeds"). Tag("dispatch", "routing"). Build()
Functions ¶
Types ¶
type DispatchReq ¶
Click to show internal directories.
Click to hide internal directories.