forked from cadence-workflow/cadence-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow.go
More file actions
87 lines (75 loc) · 2.53 KB
/
workflow.go
File metadata and controls
87 lines (75 loc) · 2.53 KB
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
package main
import (
"time"
"github.com/pborman/uuid"
"go.uber.org/cadence"
"go.uber.org/cadence/workflow"
"go.uber.org/zap"
)
type (
fileInfo struct {
FileName string
HostID string
}
)
// ApplicationName is the task list for this sample
const ApplicationName = "FileProcessorGroup"
// HostID - Use a new uuid just for demo so we can run 2 host specific activity workers on same machine.
// In real world case, you would use a hostname or ip address as HostID.
var HostID = ApplicationName + "_" + uuid.New()
//sampleFileProcessingWorkflow workflow decider
func sampleFileProcessingWorkflow(ctx workflow.Context, fileID string) (err error) {
// step 1: download resource file
ao := workflow.ActivityOptions{
ScheduleToStartTimeout: time.Second * 5,
StartToCloseTimeout: time.Minute,
HeartbeatTimeout: time.Second * 2, // such a short timeout to make sample fail over very fast
RetryPolicy: &cadence.RetryPolicy{
InitialInterval: time.Second,
BackoffCoefficient: 2.0,
MaximumInterval: time.Minute,
ExpirationInterval: time.Minute * 10,
NonRetriableErrorReasons: []string{"bad-error"},
},
}
ctx = workflow.WithActivityOptions(ctx, ao)
// Retry the whole sequence from the first activity on any error
// to retry it on a different host. In a real application it might be reasonable to
// retry individual activities and the whole sequence discriminating between different types of errors.
// See the retryactivity sample for a more sophisticated retry implementation.
for i := 1; i < 5; i++ {
err = processFile(ctx, fileID)
if err == nil {
break
}
}
if err != nil {
workflow.GetLogger(ctx).Error("Workflow failed.", zap.String("Error", err.Error()))
} else {
workflow.GetLogger(ctx).Info("Workflow completed.")
}
return err
}
func processFile(ctx workflow.Context, fileID string) (err error) {
var fInfo *fileInfo
so := &workflow.SessionOptions{
CreationTimeout: time.Minute,
ExecutionTimeout: time.Minute,
}
sessionCtx, err := workflow.CreateSession(ctx, so)
if err != nil {
return err
}
defer workflow.CompleteSession(sessionCtx)
err = workflow.ExecuteActivity(sessionCtx, downloadFileActivityName, fileID).Get(sessionCtx, &fInfo)
if err != nil {
return err
}
var fInfoProcessed *fileInfo
err = workflow.ExecuteActivity(sessionCtx, processFileActivityName, *fInfo).Get(sessionCtx, &fInfoProcessed)
if err != nil {
return err
}
err = workflow.ExecuteActivity(sessionCtx, uploadFileActivityName, *fInfoProcessed).Get(sessionCtx, nil)
return err
}