-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.go
60 lines (51 loc) · 1.67 KB
/
example.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
// Package examples shows the requirements of a workflow before running QueueDrainWorkflow
package examples
import (
"errors"
"time"
"github.com/kevindweb/version-drain/pkg/drain"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/workflow"
)
func activityOptions() workflow.ActivityOptions {
return workflow.ActivityOptions{
StartToCloseTimeout: 5 * time.Second,
RetryPolicy: &temporal.RetryPolicy{},
}
}
type wfState struct {
Checkpoint int
}
// ExampleContinueWorkflow does work with a selector
// while waiting for a ContinueAsNewSignal
func ExampleContinueWorkflow(ctx workflow.Context, state wfState) error {
ctx = workflow.WithActivityOptions(ctx, activityOptions())
var cas error
continueAsNew := workflow.GetSignalChannel(ctx, drain.ContinueAsNewSignal)
selector := workflow.NewSelector(ctx).
AddReceive(continueAsNew, func(c workflow.ReceiveChannel, more bool) {
cas = errors.New("continue")
})
exitCondition := false
log := workflow.GetLogger(ctx)
for {
selector.Select(ctx)
if cas != nil {
// remember to drain other signals
return exitContinueAsNew(ctx, state)
}
// your businss logic here
if exitCondition {
return nil
}
state.Checkpoint += 1
log.Debug("no error yet")
}
}
// exitContinueAsNew represents the most critical part of the entire workflow
// as we must tell Temporal that we need to be continued on the newest task queue version
// which ensures our "old" worker does not pick up this task
func exitContinueAsNew(ctx workflow.Context, state wfState) error {
ctx = workflow.WithWorkflowVersioningIntent(ctx, temporal.VersioningIntentDefault)
return workflow.NewContinueAsNewError(ctx, ExampleContinueWorkflow, state)
}