mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-30 14:56:19 +00:00
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.
## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
73 lines
2.4 KiB
Go
73 lines
2.4 KiB
Go
package workflow
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
commandpb "go.temporal.io/api/command/v1"
|
|
enumspb "go.temporal.io/api/enums/v1"
|
|
historyi "go.temporal.io/server/service/history/interfaces"
|
|
)
|
|
|
|
// ErrDuplicateRegistration is returned by a [CommandHandlerRegistry] when it detects duplicate registration.
|
|
var ErrDuplicateRegistration = errors.New("duplicate registration")
|
|
|
|
// FailWorkflowTaskError is an error that can be returned from a [CommandHandler] to fail the current workflow task and
|
|
// optionally terminate the entire workflow.
|
|
type FailWorkflowTaskError struct {
|
|
// The cause to set on the WorkflowTaskFailed event.
|
|
Cause enumspb.WorkflowTaskFailedCause
|
|
Message string
|
|
TerminateWorkflow bool
|
|
}
|
|
|
|
func (e FailWorkflowTaskError) Error() string {
|
|
return e.Message
|
|
}
|
|
|
|
// CommandHandler is a function for handling a workflow command as part of processing a RespondWorkflowTaskCompleted
|
|
// worker request.
|
|
type CommandHandler func(
|
|
context.Context,
|
|
historyi.MutableState,
|
|
CommandValidator,
|
|
int64,
|
|
*commandpb.Command,
|
|
) error
|
|
|
|
// CommandHandlerRegistry maintains a mapping of command type to [CommandHandler].
|
|
type CommandHandlerRegistry struct {
|
|
handlers map[enumspb.CommandType]CommandHandler
|
|
}
|
|
|
|
// CommandValidator is a helper for validating workflow commands.
|
|
type CommandValidator interface {
|
|
// IsValidPayloadSize validates that a payload size is within the configured limits.
|
|
IsValidPayloadSize(size int) bool
|
|
}
|
|
|
|
// NewCommandHandlerRegistry creates a new [CommandHandlerRegistry].
|
|
func NewCommandHandlerRegistry() *CommandHandlerRegistry {
|
|
return &CommandHandlerRegistry{
|
|
handlers: make(map[enumspb.CommandType]CommandHandler),
|
|
}
|
|
}
|
|
|
|
// Register registers a [CommandHandler] for a given command type.
|
|
// Returns an [ErrDuplicateRegistration] if a handler for the given command is already registered.
|
|
// All registration is expected to happen in a single thread on process initialization.
|
|
func (r *CommandHandlerRegistry) Register(t enumspb.CommandType, handler CommandHandler) error {
|
|
if existing, ok := r.handlers[t]; ok {
|
|
return fmt.Errorf("%w: command handler for %v: %v", ErrDuplicateRegistration, t, existing)
|
|
}
|
|
r.handlers[t] = handler
|
|
return nil
|
|
}
|
|
|
|
// Handler returns a [CommandHandler] for a given type and a boolean indicating whether it was found.
|
|
func (r *CommandHandlerRegistry) Handler(t enumspb.CommandType) (handler CommandHandler, ok bool) {
|
|
handler, ok = r.handlers[t]
|
|
return
|
|
}
|