-
Notifications
You must be signed in to change notification settings - Fork 126
/
service.go
101 lines (72 loc) · 1.89 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package worker
import (
"fmt"
"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) On(t triggerConverter, workflow workflowConverter) error {
apiWorkflow := workflow.ToWorkflow(s.Name)
wt := &types.WorkflowTriggers{}
t.ToWorkflowTriggers(wt)
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, fn := 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", actionId, s.Name)
}
}
err = s.worker.registerAction(parsedAction.Service, parsedAction.Verb, fn)
if err != nil {
return err
}
}
return nil
}
type registerActionOpts struct {
name string
}
type RegisterActionOpt func(*registerActionOpts)
func WithActionName(name string) RegisterActionOpt {
return func(opts *registerActionOpts) {
opts.name = name
}
}
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)
}
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,
}
}