-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathtimer.go
More file actions
110 lines (83 loc) · 2.34 KB
/
Copy pathtimer.go
File metadata and controls
110 lines (83 loc) · 2.34 KB
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
package main
import (
"context"
"log"
"time"
"github.com/cschleiden/go-workflows/activity"
"github.com/cschleiden/go-workflows/backend"
"github.com/cschleiden/go-workflows/client"
"github.com/cschleiden/go-workflows/samples"
"github.com/cschleiden/go-workflows/worker"
"github.com/cschleiden/go-workflows/workflow"
"github.com/google/uuid"
)
func main() {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
b := samples.GetBackend("timer", true)
// Run worker
w := RunWorker(ctx, b)
// Start workflow via client
c := client.New(b)
startWorkflow(ctx, c)
cancel()
w.WaitForCompletion()
}
func startWorkflow(ctx context.Context, c *client.Client) {
wf, err := c.CreateWorkflowInstance(ctx, client.WorkflowInstanceOptions{
InstanceID: uuid.NewString(),
}, Workflow1, "Hello world")
if err != nil {
panic("could not start workflow")
}
result, err := client.GetWorkflowResult[string](ctx, c, wf, time.Second*15)
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.RegisterActivity(Activity1)
if err := w.Start(ctx); err != nil {
panic("could not start worker")
}
return w
}
func Workflow1(ctx workflow.Context, msg string) (string, error) {
logger := workflow.Logger(ctx)
logger.Debug("Entering Workflow1, input: ", "msg", msg)
defer logger.Debug("Leaving Workflow1")
a1 := workflow.ExecuteActivity[int](ctx, workflow.DefaultActivityOptions, Activity1, 35, 12)
tctx, cancel := workflow.WithCancel(ctx)
workflow.Select(
ctx,
workflow.Await(workflow.ScheduleTimer(tctx, 2*time.Second), func(ctx workflow.Context, f workflow.Future[any]) {
if _, err := f.Get(ctx); err != nil {
logger.Debug("Timer canceled")
} else {
logger.Debug("Timer fired")
}
}),
workflow.Await(a1, func(ctx workflow.Context, f workflow.Future[int]) {
r, err := f.Get(ctx)
if err != nil {
panic(err)
}
logger.Debug("Activity result", "r", r)
// Cancel timer
cancel()
}),
)
return "result", nil
}
func Activity1(ctx context.Context, a, b int) (int, error) {
logger := activity.Logger(ctx)
logger.Debug("Entering Activity1")
time.Sleep(10 * time.Second)
defer func() {
logger.Debug("Leaving Activity1")
}()
return a + b, nil
}