-
Notifications
You must be signed in to change notification settings - Fork 49
/
web.go
89 lines (67 loc) · 1.77 KB
/
web.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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/cschleiden/go-workflows/backend"
"github.com/cschleiden/go-workflows/client"
"github.com/cschleiden/go-workflows/diag"
"github.com/cschleiden/go-workflows/samples"
"github.com/cschleiden/go-workflows/worker"
"github.com/google/uuid"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
b := samples.GetBackend("web")
db, ok := b.(diag.Backend)
if !ok {
panic("backend does not implement diag.Backend")
}
// Start diagnostic server under /diag
m := http.NewServeMux()
m.Handle("/diag/", http.StripPrefix("/diag", diag.NewServeMux(db)))
go http.ListenAndServe(":3000", m)
// Run worker
w := RunWorker(ctx, b)
// Start workflow via client
c := client.New(b)
runWorkflow(ctx, c)
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint
cancel()
if err := w.WaitForCompletion(); err != nil {
panic("could not stop worker" + err.Error())
}
}
func runWorkflow(ctx context.Context, c *client.Client) {
wf, err := c.CreateWorkflowInstance(ctx, client.WorkflowInstanceOptions{
InstanceID: uuid.NewString(),
}, Workflow1, "Hello world"+uuid.NewString(), 2, Inputs{
Msg: "",
Times: 2,
})
if err != nil {
log.Fatal(err)
panic("could not start workflow")
}
result, err := client.GetWorkflowResult[int](ctx, c, wf, time.Second*10)
if err != nil {
log.Fatal(err)
}
log.Println("Workflow finished. Result:", result)
}
func RunWorker(ctx context.Context, mb backend.Backend) *worker.Worker {
w := worker.New(mb, nil)
w.RegisterWorkflow(Workflow1)
w.RegisterWorkflow(RunJob)
w.RegisterWorkflow(RunStep)
w.RegisterActivity(Activity1)
if err := w.Start(ctx); err != nil {
panic("could not start worker")
}
return w
}