-
Notifications
You must be signed in to change notification settings - Fork 157
/
service.go
60 lines (41 loc) · 1.04 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
package worker
import (
"fmt"
"reflect"
"github.com/hatchet-dev/hatchet/pkg/client/types"
)
type Service struct {
Name string
worker *Worker
}
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) {
err := s.worker.registerAction(actionId, fn)
if err != nil {
return err
}
}
return nil
}
func (s *Service) RegisterAction(fn any) error {
fnType := reflect.TypeOf(fn)
if fnType.Kind() != reflect.Func {
return fmt.Errorf("method must be a function")
}
if fnType.Name() == "" {
return fmt.Errorf("function cannot be anonymous")
}
fnId := fnType.Name()
actionId := fmt.Sprintf("%s:%s", s.Name, fnId)
return s.worker.registerAction(actionId, fn)
}