mirror of
https://github.com/hatchet-dev/hatchet.git
synced 2026-01-07 01:09:38 -06:00
* re-add new testing harness * add healthcheck port and pick random grpc port to listen on * feat: parallel load tests and faster tests * make parallelism = 5 * fix: lint * add linter to pre * fix: add back rampup fixes * reduce matrix on PR, add matrix to pre-release step * make load tests less likely to block * make limit strategy group round robin * uncomment lines
117 lines
2.4 KiB
Go
117 lines
2.4 KiB
Go
package worker
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/hatchet-dev/hatchet/pkg/client/compute"
|
|
"github.com/hatchet-dev/hatchet/pkg/client/types"
|
|
)
|
|
|
|
type Service struct {
|
|
Name string
|
|
|
|
mws *middlewares
|
|
|
|
worker *Worker
|
|
}
|
|
|
|
func (s *Service) Use(mws ...MiddlewareFunc) {
|
|
s.mws.add(mws...)
|
|
}
|
|
|
|
func (s *Service) RegisterWorkflow(workflow workflowConverter) error {
|
|
return s.On(workflow.ToWorkflowTrigger(), workflow)
|
|
}
|
|
|
|
// Deprecated: Use RegisterWorkflow instead
|
|
func (s *Service) On(t triggerConverter, workflow workflowConverter) error {
|
|
namespace := s.worker.client.Namespace()
|
|
|
|
apiWorkflow := workflow.ToWorkflow(s.Name, namespace)
|
|
|
|
wt := &types.WorkflowTriggers{}
|
|
|
|
t.ToWorkflowTriggers(wt, namespace)
|
|
|
|
apiWorkflow.Triggers = *wt
|
|
|
|
// create the workflow via the API
|
|
err := s.worker.client.Admin().PutWorkflow(&apiWorkflow)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// register all steps as actions
|
|
for actionId, action := range workflow.ToActionMap(s.Name) {
|
|
parsedAction, err := types.ParseActionID(actionId)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if parsedAction.Service != s.Name {
|
|
// check that it's concurrency, otherwise throw error
|
|
if parsedAction.Service != "concurrency" {
|
|
return fmt.Errorf("action %s does not belong to service %s (parsed action service %s)", actionId, s.Name, parsedAction.Service)
|
|
}
|
|
}
|
|
|
|
err = s.worker.registerAction(parsedAction.Service, parsedAction.Verb, action.fn, action.compute)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type registerActionOpts struct {
|
|
name string
|
|
compute *compute.Compute
|
|
}
|
|
|
|
type RegisterActionOpt func(*registerActionOpts)
|
|
|
|
func WithActionName(name string) RegisterActionOpt {
|
|
return func(opts *registerActionOpts) {
|
|
opts.name = name
|
|
}
|
|
}
|
|
|
|
func WithCompute(compute *compute.Compute) RegisterActionOpt {
|
|
return func(opts *registerActionOpts) {
|
|
opts.compute = compute
|
|
}
|
|
}
|
|
|
|
func (s *Service) RegisterAction(fn any, opts ...RegisterActionOpt) error {
|
|
fnOpts := ®isterActionOpts{}
|
|
|
|
for _, opt := range opts {
|
|
opt(fnOpts)
|
|
}
|
|
|
|
if fnOpts.name == "" {
|
|
fnOpts.name = getFnName(fn)
|
|
}
|
|
|
|
return s.worker.registerAction(s.Name, fnOpts.name, fn, fnOpts.compute)
|
|
}
|
|
|
|
func (s *Service) Call(verb string) *WorkflowStep {
|
|
actionId := fmt.Sprintf("%s:%s", s.Name, verb)
|
|
|
|
registeredAction, exists := s.worker.actions[actionId]
|
|
|
|
if !exists {
|
|
panic(fmt.Sprintf("action %s does not exist", actionId))
|
|
}
|
|
|
|
return &WorkflowStep{
|
|
Function: registeredAction.MethodFn(),
|
|
Name: verb,
|
|
}
|
|
}
|