Files
Roey BermanandGitHub 89727703f5 Rename plugins to components (#5781)
## Why?

The name works better since these are essentially components of of the
codebase, "plugins" work better for code that is external to this repo.
2024-04-23 11:25:00 -07:00

114 lines
3.1 KiB
Go

// The MIT License
//
// Copyright (c) 2024 Temporal Technologies Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package callbacks
import (
"fmt"
"time"
"go.temporal.io/server/service/history/hsm"
)
var (
TaskTypeInvocation = hsm.TaskType{
ID: 1,
Name: "callbacks.Invocation",
}
TaskTypeBackoff = hsm.TaskType{
ID: 2,
Name: "callbacks.Backoff",
}
)
type InvocationTask struct {
Destination string
}
var _ hsm.Task = InvocationTask{}
func (InvocationTask) Type() hsm.TaskType {
return TaskTypeInvocation
}
func (t InvocationTask) Kind() hsm.TaskKind {
return hsm.TaskKindOutbound{Destination: t.Destination}
}
func (InvocationTask) Concurrent() bool {
return false
}
type InvocationTaskSerializer struct{}
func (InvocationTaskSerializer) Deserialize(data []byte, kind hsm.TaskKind) (hsm.Task, error) {
if kind, ok := kind.(hsm.TaskKindOutbound); ok {
return InvocationTask{Destination: kind.Destination}, nil
}
return nil, fmt.Errorf("%w: expected outbound", hsm.ErrInvalidTaskKind)
}
func (InvocationTaskSerializer) Serialize(hsm.Task) ([]byte, error) {
return nil, nil
}
type BackoffTask struct {
Deadline time.Time
}
var _ hsm.Task = BackoffTask{}
func (BackoffTask) Type() hsm.TaskType {
return TaskTypeBackoff
}
func (t BackoffTask) Kind() hsm.TaskKind {
return hsm.TaskKindTimer{Deadline: t.Deadline}
}
func (BackoffTask) Concurrent() bool {
return false
}
type BackoffTaskSerializer struct{}
func (BackoffTaskSerializer) Deserialize(data []byte, kind hsm.TaskKind) (hsm.Task, error) {
if kind, ok := kind.(hsm.TaskKindTimer); ok {
return BackoffTask{Deadline: kind.Deadline}, nil
}
return nil, fmt.Errorf("%w: expected timer", hsm.ErrInvalidTaskKind)
}
func (BackoffTaskSerializer) Serialize(hsm.Task) ([]byte, error) {
return nil, nil
}
func RegisterTaskSerializers(reg *hsm.Registry) error {
if err := reg.RegisterTaskSerializer(TaskTypeInvocation.ID, InvocationTaskSerializer{}); err != nil {
return err
}
if err := reg.RegisterTaskSerializer(TaskTypeBackoff.ID, BackoffTaskSerializer{}); err != nil { // nolint:revive
return err
}
return nil
}