/
workflow.go
94 lines (82 loc) · 2.82 KB
/
workflow.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
// @@@SNIPSTART subscription-workflow-go-main
package subscribeemails
import (
"errors"
"time"
"go.temporal.io/sdk/workflow"
)
// Workflow definition
func SubscriptionWorkflow(ctx workflow.Context, emailDetails EmailDetails) error {
duration := 12 * time.Second
logger := workflow.GetLogger(ctx)
logger.Info("Subscription created", "EmailAddress", emailDetails.EmailAddress)
// Query handler
err := workflow.SetQueryHandler(ctx, "GetDetails", func() (EmailDetails, error) {
return emailDetails, nil
})
if err != nil {
return err
}
// variable for Activity Options. Timeout can be set to a longer timespan (such as a month)
ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
StartToCloseTimeout: 2 * time.Minute,
WaitForCancellation: true,
})
// Handle any cleanup, including cancellations.
defer func() {
newCtx, cancel := workflow.NewDisconnectedContext(ctx)
defer cancel()
if errors.Is(ctx.Err(), workflow.ErrCanceled) {
data := EmailDetails{
EmailAddress: emailDetails.EmailAddress,
Message: "Your subscription has been canceled. Sorry to see you go!",
IsSubscribed: false,
SubscriptionCount: emailDetails.SubscriptionCount,
}
// send cancellation email
err := workflow.ExecuteActivity(newCtx, SendEmail, data).Get(newCtx, nil)
if err != nil {
logger.Error("Failed to send cancellation email", "Error", err)
} else {
// Cancellation received.
logger.Info("Sent cancellation email", "EmailAddress", emailDetails.EmailAddress)
}
}
}()
// handling for the first email ever
logger.Info("Sending welcome email", "EmailAddress", emailDetails.EmailAddress)
emailDetails.SubscriptionCount++
data := EmailDetails{
EmailAddress: emailDetails.EmailAddress,
Message: "Welcome! Looks like you've been signed up!",
IsSubscribed: true,
SubscriptionCount: emailDetails.SubscriptionCount,
}
// send welcome email, increment billing period
err = workflow.ExecuteActivity(ctx, SendEmail, data).Get(ctx, nil)
if err != nil {
return err
}
// start subscription period. execute until no longer subscribed
for emailDetails.IsSubscribed {
emailDetails.SubscriptionCount++
data := EmailDetails{
EmailAddress: emailDetails.EmailAddress,
Message: "This is yet another email in the Subscription Workflow.",
IsSubscribed: true,
SubscriptionCount: emailDetails.SubscriptionCount,
}
err = workflow.ExecuteActivity(ctx, SendEmail, data).Get(ctx, nil)
if err != nil {
return err
}
logger.Info("Sent content email", "EmailAddress", emailDetails.EmailAddress)
// Sleep the Workflow until the next subscription email needs to be sent.
// This can be set to sleep every month between emails.
if err = workflow.Sleep(ctx, duration); err != nil {
return err
}
}
return nil
}
// @@@SNIPEND