-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow.go
63 lines (51 loc) · 1.83 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
package cancellation
import (
"errors"
"fmt"
"time"
"go.temporal.io/sdk/workflow"
)
// @@@SNIPSTART samples-go-cancellation-workflow-definition
// YourWorkflow is a Workflow Definition that shows how it can be canceled.
func YourWorkflow(ctx workflow.Context) (wfErr error) {
ao := workflow.ActivityOptions{
StartToCloseTimeout: 30 * time.Minute,
HeartbeatTimeout: 5 * time.Second,
WaitForCancellation: true,
}
ctx = workflow.WithActivityOptions(ctx, ao)
logger := workflow.GetLogger(ctx)
logger.Info("cancel workflow started")
var a *Activities // Used to call Activities by function pointer
defer func() {
if !errors.Is(ctx.Err(), workflow.ErrCanceled) {
return
}
// When the Workflow is canceled, it has to get a new disconnected context to execute any Activities
newCtx, _ := workflow.NewDisconnectedContext(ctx)
err := workflow.ExecuteActivity(newCtx, a.CleanupActivity).Get(ctx, nil)
if err != nil {
logger.Error("CleanupActivity failed", "Error", err)
}
wfErr = workflow.ErrCanceled
}()
completeCh := workflow.NewChannel(ctx)
workflow.Go(ctx, func(ctx workflow.Context) {
workflow.GetSignalChannel(ctx, "unblockSignal").Receive(ctx, nil)
var result string
err := workflow.ExecuteActivity(ctx, a.ActivityToBeCanceled).Get(ctx, &result)
logger.Info(fmt.Sprintf("ActivityToBeCanceled returns %v, %v", result, err))
err = workflow.ExecuteActivity(ctx, a.ActivityToBeSkipped).Get(ctx, nil)
logger.Error("Error from ActivityToBeSkipped", "Error", err)
completeCh.Send(ctx, true)
})
selector := workflow.NewSelector(ctx)
selector.AddReceive(completeCh, func(c workflow.ReceiveChannel, more bool) {
logger.Info("Workflow execution complete.")
})
selector.AddReceive(ctx.Done(), func(c workflow.ReceiveChannel, more bool) {
logger.Info("Workflow cancelled.")
})
selector.Select(ctx)
return nil
}