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.
61 lines
2.3 KiB
Go
61 lines
2.3 KiB
Go
package chasm
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
|
|
"go.temporal.io/api/serviceerror"
|
|
)
|
|
|
|
// ErrInvalidTransition is returned from [Transition.Apply] on an invalid state transition.
|
|
var ErrInvalidTransition = serviceerror.NewFailedPrecondition("invalid transition")
|
|
|
|
// A StateMachine is anything that can get and set a comparable state S and re-generate tasks based on current state.
|
|
// It is meant to be used with [Transition] objects to safely transition their state on a given event.
|
|
type StateMachine[S comparable] interface {
|
|
StateMachineState() S
|
|
SetStateMachineState(S)
|
|
}
|
|
|
|
// Transition represents a state machine transition for a machine of type SM with state S and event E.
|
|
type Transition[S comparable, SM StateMachine[S], E any] struct {
|
|
// Source states that are valid for this transition.
|
|
Sources []S
|
|
// Destination state to transition to.
|
|
Destination S
|
|
// Function to apply the transition. Mutate the state machine object here and schedule tasks.
|
|
apply func(SM, MutableContext, E) error
|
|
}
|
|
|
|
// NewTransition creates a new [Transition] from the given source states to a destination state for a given event.
|
|
// The apply function is called after verifying the transition is possible but before setting the destination state,
|
|
// so it can inspect the current (source) state.
|
|
func NewTransition[S comparable, SM StateMachine[S], E any](src []S, dst S, apply func(SM, MutableContext, E) error) Transition[S, SM, E] {
|
|
return Transition[S, SM, E]{
|
|
Sources: src,
|
|
Destination: dst,
|
|
apply: apply,
|
|
}
|
|
}
|
|
|
|
// Possible returns a boolean indicating whether the transition is possible for the current state.
|
|
func (t Transition[S, SM, E]) Possible(sm SM) bool {
|
|
return slices.Contains(t.Sources, sm.StateMachineState())
|
|
}
|
|
|
|
// Apply applies a transition event to the given state machine changing the state machine's state to the transition's
|
|
// Destination on success. The apply function is called before the state is changed, so it can inspect the current
|
|
// (source) state.
|
|
func (t Transition[S, SM, E]) Apply(sm SM, ctx MutableContext, event E) error {
|
|
prevState := sm.StateMachineState()
|
|
if !t.Possible(sm) {
|
|
return fmt.Errorf("%w from %v", ErrInvalidTransition, prevState)
|
|
}
|
|
|
|
if err := t.apply(sm, ctx, event); err != nil {
|
|
return err
|
|
}
|
|
sm.SetStateMachineState(t.Destination)
|
|
return nil
|
|
}
|