-
Notifications
You must be signed in to change notification settings - Fork 145
/
main.go
135 lines (109 loc) · 2.49 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
package main
import (
"context"
"time"
"github.com/hatchet-dev/hatchet/pkg/client"
"github.com/hatchet-dev/hatchet/pkg/cmdutils"
"github.com/hatchet-dev/hatchet/pkg/worker"
"github.com/joho/godotenv"
)
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 StepOne(ctx context.Context, input *userCreateEvent) (result *stepOneOutput, err error) {
return &stepOneOutput{
Message: "Username is: " + input.Username,
}, nil
}
func StepTwo(ctx context.Context, input *stepOneOutput) (result *stepOneOutput, err error) {
return &stepOneOutput{
Message: "Above message is: " + input.Message,
}, nil
}
func main() {
err := godotenv.Load()
if err != nil {
panic(err)
}
client, err := client.New()
if err != nil {
panic(err)
}
// Create a worker. This automatically reads in a TemporalClient from .env and workflow files from the .hatchet
// directory, but this can be customized with the `worker.WithTemporalClient` and `worker.WithWorkflowFiles` options.
w, err := worker.NewWorker(
worker.WithClient(
client,
),
)
if err != nil {
panic(err)
}
err = w.On(worker.Event("user:create"), &worker.WorkflowJob{
Name: "test-job",
Description: "This is a test job.",
Steps: []worker.WorkflowStep{
{
Function: StepOne,
},
{
Function: StepTwo,
},
},
})
if err != nil {
panic(err)
}
// err = worker.RegisterAction("echo:echo", func(ctx context.Context, input *actionInput) (result any, err error) {
// return map[string]interface{}{
// "message": input.Message,
// }, nil
// })
// if err != nil {
// panic(err)
// }
// err = worker.RegisterAction("echo:object", func(ctx context.Context, input *actionInput) (result any, err error) {
// return nil, nil
// })
// if err != nil {
// panic(err)
// }
interruptCtx, cancel := cmdutils.InterruptContextFromChan(cmdutils.InterruptChan())
defer cancel()
go func() {
err = w.Start(interruptCtx)
if err != nil {
panic(err)
}
cancel()
}()
testEvent := userCreateEvent{
Username: "echo-test",
UserId: "1234",
Data: map[string]string{
"test": "test",
},
}
// push an event
err = client.Event().Push(
context.Background(),
"user:create",
testEvent,
)
if err != nil {
panic(err)
}
for {
select {
case <-interruptCtx.Done():
return
default:
time.Sleep(time.Second)
}
}
}