-
Notifications
You must be signed in to change notification settings - Fork 151
/
run.go
121 lines (104 loc) · 2.92 KB
/
run.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
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/google/uuid"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/db"
"github.com/hatchet-dev/hatchet/pkg/client"
"github.com/hatchet-dev/hatchet/pkg/client/types"
"github.com/hatchet-dev/hatchet/pkg/worker"
)
func run(events chan<- string) (func() error, error) {
c, err := client.New()
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)
}
err = w.On(
worker.Events("user:create:cancellation"),
&worker.WorkflowJob{
Name: "cancellation",
Description: "cancellation",
Concurrency: worker.Concurrency(func(ctx worker.HatchetContext) (string, error) {
return "concurrency:1", nil
}).MaxRuns(1).LimitStrategy(types.CancelInProgress),
Steps: []*worker.WorkflowStep{
worker.Fn(func(ctx worker.HatchetContext) (result *stepOneOutput, err error) {
select {
case <-ctx.Done():
events <- "done"
log.Printf("context cancelled")
return nil, nil
case <-time.After(30 * time.Second):
log.Printf("workflow never cancelled")
return &stepOneOutput{
Message: "done",
}, nil
}
}).SetName("step-one"),
},
},
)
if err != nil {
return nil, fmt.Errorf("error registering workflow: %w", err)
}
go func() {
log.Printf("pushing event")
testEvent := userCreateEvent{
Username: "echo-test",
UserID: "1234",
Data: map[string]string{
"test": "test",
},
}
// push an event
err := c.Event().Push(
context.Background(),
"user:create:cancellation",
testEvent,
)
if err != nil {
panic(fmt.Errorf("error pushing event: %w", err))
}
time.Sleep(10 * time.Second)
//workflows, err := c.API().WorkflowListWithResponse(context.Background(), uuid.MustParse(c.TenantId()))
//if err != nil {
// panic(fmt.Errorf("error listing workflows: %w", err))
//}
client := db.NewClient()
if err := client.Connect(); err != nil {
panic(fmt.Errorf("error connecting to database: %w", err))
}
defer client.Disconnect()
stepRuns, err := client.StepRun.FindMany(
db.StepRun.TenantID.Equals(c.TenantId()),
db.StepRun.Status.Equals(db.StepRunStatusRunning),
).Exec(context.Background())
if err != nil {
panic(fmt.Errorf("error finding step runs: %w", err))
}
for _, stepRun := range stepRuns {
stepRunID := stepRun.ID
log.Printf("cancelling step run id: %s", stepRunID)
res, err := c.API().StepRunUpdateCancelWithResponse(context.Background(), uuid.MustParse(c.TenantId()), uuid.MustParse(stepRunID))
if err != nil {
panic(fmt.Errorf("error cancelling step run: %w", err))
}
log.Printf("step run cancelled: %v", res.JSON200)
}
}()
cleanup, err := w.Start()
if err != nil {
return nil, fmt.Errorf("error starting worker: %w", err)
}
return cleanup, nil
}