-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathmain.go
More file actions
90 lines (70 loc) · 2.15 KB
/
main.go
File metadata and controls
90 lines (70 loc) · 2.15 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
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/cschleiden/go-workflows/client"
"github.com/cschleiden/go-workflows/worker"
"github.com/cschleiden/go-workflows/workflow"
"github.com/cschleiden/go-workflows/backend/sqlite"
)
// Workflow demonstrates accessing workflow info including history length
func Workflow(ctx workflow.Context) error {
// Get workflow info at the start
info := workflow.InstanceExecutionDetails(ctx)
logger := workflow.Logger(ctx)
logger.Info("Workflow started", "historyLength", info.HistoryLength)
// Execute an activity
_, err := workflow.ExecuteActivity[any](ctx, workflow.DefaultActivityOptions, Activity).Get(ctx)
if err != nil {
return err
}
// Check history length again after activity
info = workflow.InstanceExecutionDetails(ctx)
logger.Info("After activity execution", "historyLength", info.HistoryLength)
// Execute another activity
_, err = workflow.ExecuteActivity[any](ctx, workflow.DefaultActivityOptions, Activity).Get(ctx)
if err != nil {
return err
}
// Check history length again
info = workflow.InstanceExecutionDetails(ctx)
logger.Info("After second activity", "historyLength", info.HistoryLength)
return nil
}
func Activity(ctx context.Context) error {
log.Println("Activity executed")
return nil
}
func main() {
ctx := context.Background()
// Create in-memory SQLite backend
b := sqlite.NewInMemoryBackend()
// Create worker
w := worker.New(b, nil)
// Register workflow and activity
w.RegisterWorkflow(Workflow)
w.RegisterActivity(Activity)
// Start worker
if err := w.Start(ctx); err != nil {
panic(err)
}
// Create client
c := client.New(b)
// Create workflow instance
wfi, err := c.CreateWorkflowInstance(ctx, client.WorkflowInstanceOptions{
InstanceID: "workflow-info-demo",
}, Workflow)
if err != nil {
panic(err)
}
fmt.Println("Created workflow instance:", wfi.InstanceID)
// Wait for result (10 second timeout)
err = c.WaitForWorkflowInstance(ctx, wfi, 10*time.Second)
if err != nil {
panic(err)
}
fmt.Println("Workflow completed successfully!")
fmt.Println("Check the logs above to see how the history length increased as the workflow executed.")
}