-
Notifications
You must be signed in to change notification settings - Fork 126
/
main.go
143 lines (116 loc) · 2.79 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"fmt"
"log"
"github.com/joho/godotenv"
"github.com/hatchet-dev/hatchet/pkg/client"
"github.com/hatchet-dev/hatchet/pkg/cmdutils"
"github.com/hatchet-dev/hatchet/pkg/worker"
)
type userCreateEvent struct {
Username string `json:"username"`
UserID string `json:"user_id"`
Data map[string]string `json:"data"`
}
type stepOneOutput struct {
Message string `json:"message"`
}
func main() {
err := godotenv.Load()
if err != nil {
panic(err)
}
events := make(chan string, 50)
interrupt := cmdutils.InterruptChan()
cleanup, err := run(events)
if err != nil {
panic(err)
}
<-interrupt
if err := cleanup(); err != nil {
panic(fmt.Errorf("error cleaning up: %w", err))
}
}
func getConcurrencyKey(ctx worker.HatchetContext) (string, error) {
return "user-create", nil
}
func run(events chan<- string) (func() error, error) {
c, err := client.New(
client.WithNamespace("sample"),
)
if err != nil {
return nil, fmt.Errorf("error creating client: %w", err)
}
w, err := worker.NewWorker(
worker.WithClient(
c,
),
)
if err != nil {
return nil, fmt.Errorf("error creating worker: %w", err)
}
testSvc := w.NewService("test")
err = testSvc.On(
worker.Events("user:create:simple"),
&worker.WorkflowJob{
Name: "simple",
Description: "This runs after an update to the user model.",
Concurrency: worker.Concurrency(getConcurrencyKey),
Steps: []*worker.WorkflowStep{
worker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {
input := &userCreateEvent{}
err = ctx.WorkflowInput(input)
if err != nil {
return nil, err
}
log.Printf("step-one")
events <- "step-one"
return &stepOneOutput{
Message: "Username is: " + input.Username,
}, nil
},
).SetName("step-one"),
worker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {
input := &stepOneOutput{}
err = ctx.StepOutput("step-one", input)
if err != nil {
return nil, err
}
log.Printf("step-two")
events <- "step-two"
return &stepOneOutput{
Message: "Above message is: " + input.Message,
}, nil
}).SetName("step-two").AddParents("step-one"),
},
},
)
if err != nil {
return nil, fmt.Errorf("error registering workflow: %w", err)
}
go func() {
testEvent := userCreateEvent{
Username: "echo-test",
UserID: "1234",
Data: map[string]string{
"test": "test",
},
}
log.Printf("pushing event user:create:simple")
// push an event
err := c.Event().Push(
context.Background(),
"user:create:simple",
testEvent,
)
if err != nil {
panic(fmt.Errorf("error pushing event: %w", err))
}
}()
cleanup, err := w.Start()
if err != nil {
panic(err)
}
return cleanup, nil
}