-
Notifications
You must be signed in to change notification settings - Fork 157
/
main.go
116 lines (91 loc) · 1.97 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
package main
import (
"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 scheduledInput struct {
ScheduledAt time.Time `json:"scheduled_at"`
ExecuteAt time.Time `json:"scheduled_for"`
}
type stepOneOutput struct {
Message string `json:"message"`
}
func StepOne(ctx worker.HatchetContext) (result *stepOneOutput, err error) {
input := &scheduledInput{}
err = ctx.WorkflowInput(input)
if err != nil {
return nil, err
}
// get time between execute at and scheduled at
timeBetween := time.Since(input.ScheduledAt)
return &stepOneOutput{
Message: fmt.Sprintf("This ran %s after scheduling", timeBetween),
}, nil
}
func main() {
err := godotenv.Load()
if err != nil {
panic(err)
}
c, err := client.New()
if err != nil {
panic(err)
}
w, err := worker.NewWorker(
worker.WithClient(
c,
),
)
if err != nil {
panic(err)
}
err = w.On(
worker.NoTrigger(),
&worker.WorkflowJob{
Name: "scheduled-workflow",
Description: "This runs at a scheduled time.",
Steps: []*worker.WorkflowStep{
worker.Fn(StepOne).SetName("step-one"),
},
},
)
if err != nil {
panic(err)
}
interruptCtx, cancel := cmdutils.InterruptContextFromChan(cmdutils.InterruptChan())
defer cancel()
cleanup, err := w.Start()
if err != nil {
panic(fmt.Errorf("error cleaning up: %w", err))
}
go func() {
time.Sleep(5 * time.Second)
executeAt := time.Now().Add(time.Second * 10)
err = c.Admin().ScheduleWorkflow(
"scheduled-workflow",
client.WithSchedules(executeAt),
client.WithInput(&scheduledInput{
ScheduledAt: time.Now(),
ExecuteAt: executeAt,
}),
)
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)
}
}
}