-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathexternalevents.go
96 lines (81 loc) · 2.95 KB
/
externalevents.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
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/microsoft/durabletask-go/api"
"github.com/microsoft/durabletask-go/backend"
"github.com/microsoft/durabletask-go/backend/sqlite"
"github.com/microsoft/durabletask-go/task"
)
func main() {
// Create a new task registry and add the orchestrator and activities
r := task.NewTaskRegistry()
r.AddOrchestrator(ExternalEventOrchestrator)
// Init the client
ctx := context.Background()
client, worker, err := Init(ctx, r)
if err != nil {
log.Fatalf("Failed to initialize the client: %v", err)
}
defer worker.Shutdown(ctx)
// Start a new orchestration
id, err := client.ScheduleNewOrchestration(ctx, "ExternalEventOrchestrator")
if err != nil {
log.Fatalf("Failed to schedule new orchestration: %v", err)
}
metadata, err := client.WaitForOrchestrationStart(ctx, id)
if err != nil {
log.Fatalf("Failed to wait for orchestration to start: %v", err)
}
// Prompt the user for their name and send that to the orchestrator
go func() {
fmt.Println("Enter your first name: ")
var nameInput string
fmt.Scanln(&nameInput)
if err = client.RaiseEvent(ctx, id, "Name", api.WithEventPayload(nameInput)); err != nil {
log.Fatalf("Failed to raise event: %v", err)
}
}()
// After the orchestration receives the event, it should complete on its own
metadata, err = client.WaitForOrchestrationCompletion(ctx, id)
if err != nil {
log.Fatalf("Failed to wait for orchestration to complete: %v", err)
}
if metadata.FailureDetails != nil {
log.Println("orchestration failed:", metadata.FailureDetails.ErrorMessage)
} else {
log.Println("orchestration completed:", metadata.SerializedOutput)
}
}
// Init creates and initializes an in-memory client and worker pair with default configuration.
func Init(ctx context.Context, r *task.TaskRegistry) (backend.TaskHubClient, backend.TaskHubWorker, error) {
logger := backend.DefaultLogger()
// Create an executor
executor := task.NewTaskExecutor(r)
// Create a new backend
// Use the in-memory sqlite provider by specifying ""
be := sqlite.NewSqliteBackend(sqlite.NewSqliteOptions(""), logger)
orchestrationWorker := backend.NewOrchestrationWorker(be, executor, logger)
activityWorker := backend.NewActivityTaskWorker(be, executor, logger)
taskHubWorker := backend.NewTaskHubWorker(be, orchestrationWorker, activityWorker, logger)
// Start the worker
err := taskHubWorker.Start(ctx)
if err != nil {
return nil, nil, err
}
// Get the client to the backend
taskHubClient := backend.NewTaskHubClient(be)
return taskHubClient, taskHubWorker, nil
}
// ExternalEventOrchestrator is an orchestrator function that blocks for 30 seconds or
// until a "Name" event is sent to it.
func ExternalEventOrchestrator(ctx *task.OrchestrationContext) (any, error) {
var nameInput string
if err := ctx.WaitForSingleEvent("Name", 30*time.Second).Await(&nameInput); err != nil {
// Timeout expired
return nil, err
}
return fmt.Sprintf("Hello, %s!", nameInput), nil
}