-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (61 loc) · 2 KB
/
main.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
package main
import (
"context"
"fmt"
"net/http"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"
"github.com/nguyenptk/temporal-go-sample/workflow"
)
func NewTemporalClient() (client.Client, error) {
temporalClient, err := client.Dial(client.Options{})
if err != nil {
fmt.Println("\nfailed to create temporal client")
return nil, err
}
return temporalClient, nil
}
func main() {
// Init Temporal
temporalClient, err := NewTemporalClient()
temporalWorker := worker.New(temporalClient, "all-tasks", worker.Options{})
temporalWorker.RegisterWorkflow(workflow.DoSomething)
temporalWorker.RegisterActivity(workflow.NewActivity1(temporalClient, nil).Activity1)
temporalWorker.RegisterActivity(workflow.NewActivity2(temporalClient, nil).Activity2)
temporalWorker.RegisterActivity(workflow.NewActivity3(temporalClient, nil).Activity3)
temporalWorker.RegisterActivity(workflow.NewActivity4(temporalClient, nil).Activity4)
temporalWorker.RegisterActivity(workflow.NewActivity5(temporalClient, nil).Activity5)
// Handle HTTP endpoints
http.HandleFunc("/do-something", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("/do-something endpoint is called, triggering the workflow...")
// Submit DoSomething Workflow to Temporal
_, err = temporalClient.ExecuteWorkflow(
context.TODO(),
client.StartWorkflowOptions{
ID: workflow.GetDoSomethingWorkflowName(workflow.X_REQUEST_ID),
TaskQueue: "all-tasks",
},
workflow.DoSomething,
workflow.DoSomethingInput{
PaymentID: workflow.X_REQUEST_ID,
},
)
if err != nil {
fmt.Println("Error submitting DoSomething Workflow to Temporal:", err)
}
})
// Start Temporal
go func() {
err = temporalWorker.Run(worker.InterruptCh())
if err != nil {
fmt.Println("Error running temporal worker:", err)
}
}()
// Start HTTP Go server
port := 6000
fmt.Printf("Server listening on :%d\n", port)
err = http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
fmt.Println("Error starting server:", err)
}
}