-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathactivities.go
101 lines (84 loc) · 2.96 KB
/
activities.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
97
98
99
100
101
package expense
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"go.temporal.io/sdk/activity"
)
func CreateExpenseActivity(ctx context.Context, expenseID string) error {
if len(expenseID) == 0 {
return errors.New("expense id is empty")
}
resp, err := http.Get(expenseServerHostPort + "/create?is_api_call=true&id=" + expenseID)
if err != nil {
return err
}
body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
return err
}
if string(body) == "SUCCEED" {
activity.GetLogger(ctx).Info("Expense created.", "ExpenseID", expenseID)
return nil
}
return errors.New(string(body))
}
// waitForDecisionActivity waits for the expense decision. This activity will complete asynchronously. When this method
// returns error activity.ErrResultPending, the Temporal Go SDK recognize this error, and won't mark this activity
// as failed or completed. The Temporal server will wait until Client.CompleteActivity() is called or timeout happened
// whichever happen first. In this sample case, the CompleteActivity() method is called by our dummy expense server when
// the expense is approved.
func WaitForDecisionActivity(ctx context.Context, expenseID string) (string, error) {
if len(expenseID) == 0 {
return "", errors.New("expense id is empty")
}
logger := activity.GetLogger(ctx)
// save current activity info so it can be completed asynchronously when expense is approved/rejected
activityInfo := activity.GetInfo(ctx)
formData := url.Values{}
formData.Add("task_token", string(activityInfo.TaskToken))
registerCallbackURL := expenseServerHostPort + "/registerCallback?id=" + expenseID
resp, err := http.PostForm(registerCallbackURL, formData)
if err != nil {
logger.Info("waitForDecisionActivity failed to register callback.", "Error", err)
return "", err
}
body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
return "", err
}
status := string(body)
if status == "SUCCEED" {
// register callback succeed
logger.Info("Successfully registered callback.", "ExpenseID", expenseID)
// ErrActivityResultPending is returned from activity's execution to indicate the activity is not completed when it returns.
// activity will be completed asynchronously when Client.CompleteActivity() is called.
return "", activity.ErrResultPending
}
logger.Warn("Register callback failed.", "ExpenseStatus", status)
return "", fmt.Errorf("register callback failed status:%s", status)
}
func PaymentActivity(ctx context.Context, expenseID string) error {
if len(expenseID) == 0 {
return errors.New("expense id is empty")
}
resp, err := http.Get(expenseServerHostPort + "/action?is_api_call=true&type=payment&id=" + expenseID)
if err != nil {
return err
}
body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
return err
}
if string(body) == "SUCCEED" {
activity.GetLogger(ctx).Info("paymentActivity succeed", "ExpenseID", expenseID)
return nil
}
return errors.New(string(body))
}