Skip to content

Commit 06b3bef

Browse files
authored
Signal sample
1 parent fab3bb8 commit 06b3bef

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

samples/signal/signal.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
"time"
99

1010
"github.com/cschleiden/go-workflows/backend"
11-
"github.com/cschleiden/go-workflows/backend/sqlite"
11+
"github.com/cschleiden/go-workflows/backend/redis"
1212
"github.com/cschleiden/go-workflows/client"
13+
"github.com/cschleiden/go-workflows/samples"
1314
"github.com/cschleiden/go-workflows/worker"
1415
"github.com/cschleiden/go-workflows/workflow"
1516
"github.com/google/uuid"
@@ -18,7 +19,11 @@ import (
1819
func main() {
1920
ctx := context.Background()
2021

21-
b := sqlite.NewInMemoryBackend()
22+
// b := sqlite.NewInMemoryBackend()
23+
b, err := redis.NewRedisBackend("localhost:6379", "", "RedisPassw0rd", 0)
24+
if err != nil {
25+
panic(err)
26+
}
2227

2328
// Run worker
2429
go RunWorker(ctx, b)
@@ -69,42 +74,37 @@ func RunWorker(ctx context.Context, mb backend.Backend) {
6974
}
7075

7176
func Workflow1(ctx workflow.Context, msg string, subID string) (string, error) {
72-
log.Println("Entering Workflow1")
73-
log.Println("\tWorkflow instance input:", msg)
74-
log.Println("\tIsReplaying:", workflow.Replaying(ctx))
75-
defer log.Println("Leaving Workflow1")
77+
samples.Trace(ctx, "Entering Workflow1")
7678

77-
log.Println("Waiting for first signal")
79+
samples.Trace(ctx, "Waiting for first signal")
7880
workflow.Select(ctx,
7981
workflow.Receive(workflow.NewSignalChannel[int](ctx, "test"), func(ctx workflow.Context, r int, ok bool) {
80-
log.Println("Received signal:", r)
81-
log.Println("\tIsReplaying:", workflow.Replaying(ctx))
82+
samples.Trace(ctx, "Received signal:", r)
8283
}),
8384
)
8485

85-
log.Println("Waiting for second signal")
86+
samples.Trace(ctx, "Waiting for second signal")
8687
workflow.NewSignalChannel[int](ctx, "test2").Receive(ctx)
87-
log.Println("Received second signal")
88+
samples.Trace(ctx, "Received second signal")
8889

8990
if _, err := workflow.CreateSubWorkflowInstance[any](ctx, workflow.SubWorkflowOptions{
9091
InstanceID: subID,
9192
}, SubWorkflow1).Get(ctx); err != nil {
9293
panic(err)
9394
}
9495

95-
log.Println("Sub workflow finished")
96+
samples.Trace(ctx, "Sub workflow finished")
9697

9798
return "result", nil
9899
}
99100

100101
func SubWorkflow1(ctx workflow.Context) (string, error) {
101-
log.Println("Waiting for signal from sub-worflow")
102-
defer log.Println("Leaving SubWorkflow1")
102+
samples.Trace(ctx, "Waiting for signal from sub-worflow")
103103

104104
c := workflow.NewSignalChannel[string](ctx, "sub-signal")
105105
c.Receive(ctx)
106106

107-
log.Println("Received.")
107+
samples.Trace(ctx, "Received sub-workflow signal")
108108

109109
return "World", nil
110110
}

0 commit comments

Comments
 (0)