-
Notifications
You must be signed in to change notification settings - Fork 145
/
main.go
86 lines (67 loc) · 1.35 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
package main
import (
"context"
"fmt"
"time"
"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 sampleEvent struct{}
type requeueInput struct{}
func main() {
err := godotenv.Load()
if err != nil {
panic(err)
}
client, err := client.New(
client.InitWorkflows(),
)
if err != nil {
panic(err)
}
worker, err := worker.NewWorker(
worker.WithClient(
client,
),
)
if err != nil {
panic(err)
}
err = worker.RegisterAction("requeue:requeue", func(ctx context.Context, input *requeueInput) (result any, err error) {
return map[string]interface{}{}, nil
})
if err != nil {
panic(err)
}
interruptCtx, cancel := cmdutils.InterruptContextFromChan(cmdutils.InterruptChan())
defer cancel()
event := sampleEvent{}
// push an event
err = client.Event().Push(
context.Background(),
"example:event",
event,
)
if err != nil {
panic(err)
}
// wait to register the worker for 10 seconds, to let the requeuer kick in
time.Sleep(10 * time.Second)
cleanup, err := worker.Start()
if err != nil {
panic(err)
}
for {
select {
case <-interruptCtx.Done():
if err := cleanup(); err != nil {
panic(fmt.Errorf("error cleaning up: %w", err))
}
return
default:
time.Sleep(time.Second)
}
}
}