mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
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).
366 lines
10 KiB
Go
366 lines
10 KiB
Go
package nexusoperations
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
enumspb "go.temporal.io/api/enums/v1"
|
|
"go.temporal.io/api/serviceerror"
|
|
enumsspb "github.com/hanzoai/tasks/api/enums/v1"
|
|
persistencespb "github.com/hanzoai/tasks/api/persistence/v1"
|
|
"github.com/hanzoai/tasks/common/persistence/serialization"
|
|
"github.com/hanzoai/tasks/service/history/consts"
|
|
"github.com/hanzoai/tasks/service/history/hsm"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
const (
|
|
TaskTypeInvocation = "nexusoperations.Invocation"
|
|
TaskTypeBackoff = "nexusoperations.Backoff"
|
|
TaskTypeCancelation = "nexusoperations.Cancelation"
|
|
TaskTypeCancelationBackoff = "nexusoperations.CancelationBackoff"
|
|
// NOTE: the name `Timeout` is used for backward compatibility with existing persisted tasks and predates the addition of more flexible timeout types.
|
|
TaskTypeScheduleToCloseTimeout = "nexusoperations.Timeout"
|
|
TaskTypeScheduleToStartTimeout = "nexusoperations.ScheduleToStartTimeout"
|
|
TaskTypeStartToCloseTimeout = "nexusoperations.StartToCloseTimeout"
|
|
)
|
|
|
|
var errSerializationCast = errors.New("cannot serialize HSM task. unable to cast to expected type")
|
|
|
|
type ScheduleToCloseTimeoutTask struct {
|
|
deadline time.Time
|
|
}
|
|
|
|
var _ hsm.Task = ScheduleToCloseTimeoutTask{}
|
|
|
|
func (ScheduleToCloseTimeoutTask) Type() string {
|
|
return TaskTypeScheduleToCloseTimeout
|
|
}
|
|
|
|
func (t ScheduleToCloseTimeoutTask) Deadline() time.Time {
|
|
return t.deadline
|
|
}
|
|
|
|
func (ScheduleToCloseTimeoutTask) Destination() string {
|
|
return ""
|
|
}
|
|
|
|
// Validate checks if the timeout task is still valid to execute for the given node state.
|
|
func (t ScheduleToCloseTimeoutTask) Validate(ref *persistencespb.StateMachineRef, node *hsm.Node) error {
|
|
if err := node.CheckRunning(); err != nil {
|
|
return err
|
|
}
|
|
op, err := hsm.MachineData[Operation](node)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !TransitionTimedOut.Possible(op) {
|
|
return fmt.Errorf(
|
|
"%w: %w: cannot timeout machine in state %v",
|
|
consts.ErrStaleReference,
|
|
hsm.ErrInvalidTransition,
|
|
op.State(),
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type TimeoutTaskSerializer struct{}
|
|
|
|
func (TimeoutTaskSerializer) Deserialize(data []byte, attrs hsm.TaskAttributes) (hsm.Task, error) {
|
|
return ScheduleToCloseTimeoutTask{deadline: attrs.Deadline}, nil
|
|
}
|
|
|
|
func (TimeoutTaskSerializer) Serialize(hsm.Task) ([]byte, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
type InvocationTask struct {
|
|
EndpointName string
|
|
Attempt int32
|
|
}
|
|
|
|
var _ hsm.Task = InvocationTask{}
|
|
|
|
func (InvocationTask) Type() string {
|
|
return TaskTypeInvocation
|
|
}
|
|
|
|
func (InvocationTask) Deadline() time.Time {
|
|
return hsm.Immediate
|
|
}
|
|
|
|
func (t InvocationTask) Destination() string {
|
|
return t.EndpointName
|
|
}
|
|
|
|
func (InvocationTask) Validate(ref *persistencespb.StateMachineRef, node *hsm.Node) error {
|
|
if err := node.CheckRunning(); err != nil {
|
|
return err
|
|
}
|
|
return hsm.ValidateState[enumsspb.NexusOperationState, Operation](node, enumsspb.NEXUS_OPERATION_STATE_SCHEDULED)
|
|
}
|
|
|
|
type InvocationTaskSerializer struct{}
|
|
|
|
func (InvocationTaskSerializer) Deserialize(data []byte, attrs hsm.TaskAttributes) (hsm.Task, error) {
|
|
var info persistencespb.NexusInvocationTaskInfo
|
|
err := proto.Unmarshal(data, &info)
|
|
if err != nil {
|
|
return nil, serialization.NewDeserializationError(enumspb.ENCODING_TYPE_PROTO3, err)
|
|
}
|
|
return InvocationTask{EndpointName: attrs.Destination, Attempt: info.Attempt}, nil
|
|
}
|
|
|
|
func (InvocationTaskSerializer) Serialize(task hsm.Task) ([]byte, error) {
|
|
switch task := task.(type) {
|
|
case InvocationTask:
|
|
return proto.Marshal(&persistencespb.NexusInvocationTaskInfo{Attempt: task.Attempt})
|
|
default:
|
|
return nil, serviceerror.NewInternalf("unknown HSM task type while serializing: %v", task)
|
|
}
|
|
}
|
|
|
|
type BackoffTask struct {
|
|
deadline time.Time
|
|
}
|
|
|
|
var _ hsm.Task = BackoffTask{}
|
|
|
|
func (BackoffTask) Type() string {
|
|
return TaskTypeBackoff
|
|
}
|
|
|
|
func (t BackoffTask) Deadline() time.Time {
|
|
return t.deadline
|
|
}
|
|
|
|
func (t BackoffTask) Destination() string {
|
|
return ""
|
|
}
|
|
|
|
func (t BackoffTask) Validate(_ *persistencespb.StateMachineRef, node *hsm.Node) error {
|
|
if err := node.CheckRunning(); err != nil {
|
|
return err
|
|
}
|
|
return hsm.ValidateState[enumsspb.NexusOperationState, Operation](node, enumsspb.NEXUS_OPERATION_STATE_BACKING_OFF)
|
|
}
|
|
|
|
type BackoffTaskSerializer struct{}
|
|
|
|
func (BackoffTaskSerializer) Deserialize(data []byte, attrs hsm.TaskAttributes) (hsm.Task, error) {
|
|
return BackoffTask{deadline: attrs.Deadline}, nil
|
|
}
|
|
|
|
func (BackoffTaskSerializer) Serialize(hsm.Task) ([]byte, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
type CancelationTask struct {
|
|
EndpointName string
|
|
Attempt int32
|
|
}
|
|
|
|
var _ hsm.Task = CancelationTask{}
|
|
|
|
func (CancelationTask) Type() string {
|
|
return TaskTypeCancelation
|
|
}
|
|
|
|
func (CancelationTask) Deadline() time.Time {
|
|
return hsm.Immediate
|
|
}
|
|
|
|
func (t CancelationTask) Destination() string {
|
|
return t.EndpointName
|
|
}
|
|
|
|
func (CancelationTask) Validate(ref *persistencespb.StateMachineRef, node *hsm.Node) error {
|
|
if err := node.CheckRunning(); err != nil {
|
|
return err
|
|
}
|
|
return hsm.ValidateState[enumspb.NexusOperationCancellationState, Cancelation](node, enumspb.NEXUS_OPERATION_CANCELLATION_STATE_SCHEDULED)
|
|
}
|
|
|
|
type CancelationTaskSerializer struct{}
|
|
|
|
func (CancelationTaskSerializer) Deserialize(data []byte, attrs hsm.TaskAttributes) (hsm.Task, error) {
|
|
var info persistencespb.NexusCancelationTaskInfo
|
|
err := proto.Unmarshal(data, &info)
|
|
if err != nil {
|
|
return nil, serialization.NewDeserializationError(enumspb.ENCODING_TYPE_PROTO3, err)
|
|
}
|
|
return CancelationTask{EndpointName: attrs.Destination, Attempt: info.Attempt}, nil
|
|
}
|
|
|
|
func (CancelationTaskSerializer) Serialize(task hsm.Task) ([]byte, error) {
|
|
switch task := task.(type) {
|
|
case CancelationTask:
|
|
return proto.Marshal(&persistencespb.NexusCancelationTaskInfo{Attempt: task.Attempt})
|
|
default:
|
|
return nil, serviceerror.NewInternalf("unknown HSM task type while serializing: %v", task)
|
|
}
|
|
}
|
|
|
|
type CancelationBackoffTask struct {
|
|
deadline time.Time
|
|
}
|
|
|
|
var _ hsm.Task = CancelationBackoffTask{}
|
|
|
|
func (CancelationBackoffTask) Type() string {
|
|
return TaskTypeCancelationBackoff
|
|
}
|
|
|
|
func (t CancelationBackoffTask) Deadline() time.Time {
|
|
return t.deadline
|
|
}
|
|
|
|
func (CancelationBackoffTask) Destination() string {
|
|
return ""
|
|
}
|
|
|
|
func (CancelationBackoffTask) Validate(ref *persistencespb.StateMachineRef, node *hsm.Node) error {
|
|
if err := node.CheckRunning(); err != nil {
|
|
return err
|
|
}
|
|
return hsm.ValidateState[enumspb.NexusOperationCancellationState, Cancelation](node, enumspb.NEXUS_OPERATION_CANCELLATION_STATE_BACKING_OFF)
|
|
}
|
|
|
|
type CancelationBackoffTaskSerializer struct{}
|
|
|
|
func (CancelationBackoffTaskSerializer) Deserialize(data []byte, attrs hsm.TaskAttributes) (hsm.Task, error) {
|
|
return CancelationBackoffTask{deadline: attrs.Deadline}, nil
|
|
}
|
|
|
|
func (CancelationBackoffTaskSerializer) Serialize(hsm.Task) ([]byte, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
type ScheduleToStartTimeoutTask struct {
|
|
deadline time.Time
|
|
}
|
|
|
|
var _ hsm.Task = ScheduleToStartTimeoutTask{}
|
|
|
|
func (ScheduleToStartTimeoutTask) Type() string {
|
|
return TaskTypeScheduleToStartTimeout
|
|
}
|
|
|
|
func (t ScheduleToStartTimeoutTask) Deadline() time.Time {
|
|
return t.deadline
|
|
}
|
|
|
|
func (ScheduleToStartTimeoutTask) Destination() string {
|
|
return ""
|
|
}
|
|
|
|
// Validate checks if the schedule-to-start timeout task is still valid.
|
|
// Only valid if operation is still in SCHEDULED or BACKING_OFF state.
|
|
func (t ScheduleToStartTimeoutTask) Validate(ref *persistencespb.StateMachineRef, node *hsm.Node) error {
|
|
if err := node.CheckRunning(); err != nil {
|
|
return err
|
|
}
|
|
op, err := hsm.MachineData[Operation](node)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Only timeout if we haven't started yet
|
|
switch op.State() {
|
|
case enumsspb.NEXUS_OPERATION_STATE_SCHEDULED,
|
|
enumsspb.NEXUS_OPERATION_STATE_BACKING_OFF:
|
|
return nil
|
|
default:
|
|
// Already started or completed, timeout not applicable
|
|
return fmt.Errorf(
|
|
"%w: %w: cannot apply schedule-to-start timeout to machine in state %v",
|
|
consts.ErrStaleReference,
|
|
hsm.ErrInvalidTransition,
|
|
op.State(),
|
|
)
|
|
}
|
|
}
|
|
|
|
type ScheduleToStartTimeoutTaskSerializer struct{}
|
|
|
|
func (ScheduleToStartTimeoutTaskSerializer) Deserialize(data []byte, attrs hsm.TaskAttributes) (hsm.Task, error) {
|
|
return ScheduleToStartTimeoutTask{deadline: attrs.Deadline}, nil
|
|
}
|
|
|
|
func (ScheduleToStartTimeoutTaskSerializer) Serialize(hsm.Task) ([]byte, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
type StartToCloseTimeoutTask struct {
|
|
deadline time.Time
|
|
}
|
|
|
|
var _ hsm.Task = StartToCloseTimeoutTask{}
|
|
|
|
func (StartToCloseTimeoutTask) Type() string {
|
|
return TaskTypeStartToCloseTimeout
|
|
}
|
|
|
|
func (t StartToCloseTimeoutTask) Deadline() time.Time {
|
|
return t.deadline
|
|
}
|
|
|
|
func (StartToCloseTimeoutTask) Destination() string {
|
|
return ""
|
|
}
|
|
|
|
// Validate checks if the start-to-close timeout task is still valid.
|
|
// Only valid if operation is in STARTED state.
|
|
func (t StartToCloseTimeoutTask) Validate(ref *persistencespb.StateMachineRef, node *hsm.Node) error {
|
|
if err := node.CheckRunning(); err != nil {
|
|
return err
|
|
}
|
|
op, err := hsm.MachineData[Operation](node)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Only timeout if we're in started state
|
|
if op.State() != enumsspb.NEXUS_OPERATION_STATE_STARTED {
|
|
return fmt.Errorf(
|
|
"%w: %w: cannot apply start-to-close timeout to machine in state %v",
|
|
consts.ErrStaleReference,
|
|
hsm.ErrInvalidTransition,
|
|
op.State(),
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type StartToCloseTimeoutTaskSerializer struct{}
|
|
|
|
func (StartToCloseTimeoutTaskSerializer) Deserialize(data []byte, attrs hsm.TaskAttributes) (hsm.Task, error) {
|
|
return StartToCloseTimeoutTask{deadline: attrs.Deadline}, nil
|
|
}
|
|
|
|
func (StartToCloseTimeoutTaskSerializer) Serialize(hsm.Task) ([]byte, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func RegisterTaskSerializers(reg *hsm.Registry) error {
|
|
if err := reg.RegisterTaskSerializer(TaskTypeScheduleToCloseTimeout, TimeoutTaskSerializer{}); err != nil {
|
|
return err
|
|
}
|
|
if err := reg.RegisterTaskSerializer(TaskTypeInvocation, InvocationTaskSerializer{}); err != nil {
|
|
return err
|
|
}
|
|
if err := reg.RegisterTaskSerializer(TaskTypeBackoff, BackoffTaskSerializer{}); err != nil {
|
|
return err
|
|
}
|
|
if err := reg.RegisterTaskSerializer(TaskTypeCancelation, CancelationTaskSerializer{}); err != nil {
|
|
return err
|
|
}
|
|
if err := reg.RegisterTaskSerializer(TaskTypeCancelationBackoff, CancelationBackoffTaskSerializer{}); err != nil { // nolint:revive
|
|
return err
|
|
}
|
|
if err := reg.RegisterTaskSerializer(TaskTypeScheduleToStartTimeout, ScheduleToStartTimeoutTaskSerializer{}); err != nil {
|
|
return err
|
|
}
|
|
return reg.RegisterTaskSerializer(TaskTypeStartToCloseTimeout, StartToCloseTimeoutTaskSerializer{})
|
|
}
|