Skip to content

Commit fe92842

Browse files
authored
Adding delaystart a new workflow with 30 sec delay (#85)
* Adding new workflow "delaystart" with 30 sec delay
1 parent df6f7bd commit fe92842

File tree

7 files changed

+340
-20
lines changed

7 files changed

+340
-20
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export PATH := $(GOPATH)/bin:$(PATH)
77
default: test
88

99
PROGS = helloworld \
10+
delaystart \
1011
branch \
1112
childworkflow \
1213
crossdomain \
@@ -52,6 +53,7 @@ TEST_DIRS=./cmd/samples/cron \
5253
./cmd/samples/recipes/choice \
5354
./cmd/samples/recipes/greetings \
5455
./cmd/samples/recipes/helloworld \
56+
./cmd/samples/recipes/delaystart \
5557
./cmd/samples/recipes/cancelactivity \
5658
./cmd/samples/recipes/pickfirst \
5759
./cmd/samples/recipes/mutex \
@@ -75,6 +77,9 @@ cancelactivity:
7577
helloworld:
7678
go build -o bin/helloworld cmd/samples/recipes/helloworld/*.go
7779

80+
delaystart:
81+
go build -o bin/delaystart cmd/samples/recipes/delaystart/*.go
82+
7883
branch:
7984
go build -o bin/branch cmd/samples/recipes/branch/*.go
8085

@@ -168,6 +173,7 @@ sideeffect:
168173
go build -o bin/sideeffect cmd/samples/recipes/sideeffect/*.go
169174

170175
bins: helloworld \
176+
delaystart \
171177
branch \
172178
crossdomain \
173179
childworkflow \

cmd/samples/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ See instructions for running the Cadence Server: https://github.com/uber/cadence
1010

1111
## Steps to run samples
1212
### Build Samples
13+
* Build all the workflows at once
1314
```
1415
make
1516
```
17+
* Build a workflow individually
18+
```
19+
make <WORKFLOW NAME>
20+
21+
Example:
22+
make helloworld
23+
```
1624

1725
### Run HelloWorld Sample
1826
* Start workers for helloworld workflow and activities
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"go.uber.org/cadence/activity"
8+
"go.uber.org/cadence/workflow"
9+
"go.uber.org/zap"
10+
)
11+
12+
/**
13+
* This is the hello world workflow sample.
14+
*/
15+
16+
// ApplicationName is the task list for this sample
17+
const ApplicationName = "delaystartGroup"
18+
19+
const delayStartWorkflowName = "delayStartWorkflow"
20+
21+
// helloWorkflow workflow decider
22+
func delayStartWorkflow(ctx workflow.Context, delayStart time.Duration) error {
23+
ao := workflow.ActivityOptions{
24+
ScheduleToStartTimeout: time.Minute,
25+
StartToCloseTimeout: time.Minute,
26+
HeartbeatTimeout: time.Second * 20,
27+
}
28+
ctx = workflow.WithActivityOptions(ctx, ao)
29+
30+
logger := workflow.GetLogger(ctx)
31+
logger.Info("delaystart workflow started after waiting for " + delayStart.String())
32+
var helloworldResult string
33+
err := workflow.ExecuteActivity(ctx, delayStartActivity, delayStart).Get(ctx, &helloworldResult)
34+
if err != nil {
35+
logger.Error("Activity failed after waiting for "+delayStart.String(), zap.Error(err))
36+
return err
37+
}
38+
39+
// Adding a new activity to the workflow will result in a non-determinstic change for the workflow
40+
// Please check https://cadenceworkflow.io/docs/go-client/workflow-versioning/ for more information
41+
//
42+
// Un-commenting the following code and the TestReplayWorkflowHistoryFromFile in replay_test.go
43+
// will fail due to the non-determinstic change
44+
//
45+
// If you have a completed workflow execution without the following code and run the
46+
// TestWorkflowShadowing in shadow_test.go or start the worker in shadow mode (using -m shadower)
47+
// those two shadowing check will also fail due to the non-deterministic change
48+
//
49+
// err := workflow.ExecuteActivity(ctx, helloWorldActivity, name).Get(ctx, &helloworldResult)
50+
// if err != nil {
51+
// logger.Error("Activity failed.", zap.Error(err))
52+
// return err
53+
// }
54+
55+
logger.Info("Workflow completed.", zap.String("Result", helloworldResult))
56+
57+
return nil
58+
}
59+
60+
func delayStartActivity(ctx context.Context, delayStart time.Duration) (string, error) {
61+
logger := activity.GetLogger(ctx)
62+
logger.Info("delayStartActivity started after " + delayStart.String())
63+
return "Activity started after " + delayStart.String(), nil
64+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/require"
8+
"go.uber.org/cadence/activity"
9+
"go.uber.org/cadence/encoded"
10+
"go.uber.org/cadence/testsuite"
11+
)
12+
13+
func Test_Workflow(t *testing.T) {
14+
testSuite := &testsuite.WorkflowTestSuite{}
15+
16+
env := testSuite.NewTestWorkflowEnvironment()
17+
env.RegisterWorkflow(delayStartWorkflow)
18+
env.RegisterActivity(delayStartActivity)
19+
20+
var activityMessage string
21+
env.SetOnActivityCompletedListener(func(activityInfo *activity.Info, result encoded.Value, err error) {
22+
result.Get(&activityMessage)
23+
})
24+
25+
delayStart := 30 * time.Second
26+
env.ExecuteWorkflow(delayStartWorkflow, delayStart)
27+
28+
require.True(t, env.IsWorkflowCompleted())
29+
require.NoError(t, env.GetWorkflowError())
30+
require.Equal(t, "Activity started after "+delayStart.String(), activityMessage)
31+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"time"
6+
7+
"github.com/pborman/uuid"
8+
"go.uber.org/cadence/client"
9+
"go.uber.org/cadence/worker"
10+
11+
"github.com/uber-common/cadence-samples/cmd/samples/common"
12+
)
13+
14+
// This needs to be done as part of a bootstrap step when the process starts.
15+
// The workers are supposed to be long running.
16+
func startWorkers(h *common.SampleHelper) {
17+
// Configure worker options.
18+
workerOptions := worker.Options{
19+
MetricsScope: h.WorkerMetricScope,
20+
Logger: h.Logger,
21+
FeatureFlags: client.FeatureFlags{
22+
WorkflowExecutionAlreadyCompletedErrorEnabled: true,
23+
},
24+
}
25+
h.StartWorkers(h.Config.DomainName, ApplicationName, workerOptions)
26+
}
27+
28+
func startShadower(h *common.SampleHelper) {
29+
workerOptions := worker.Options{
30+
MetricsScope: h.WorkerMetricScope,
31+
Logger: h.Logger,
32+
EnableShadowWorker: true,
33+
ShadowOptions: worker.ShadowOptions{
34+
WorkflowTypes: []string{delayStartWorkflowName},
35+
WorkflowStatus: []string{"Completed"},
36+
ExitCondition: worker.ShadowExitCondition{
37+
ShadowCount: 10,
38+
},
39+
},
40+
}
41+
h.StartWorkers(h.Config.DomainName, ApplicationName, workerOptions)
42+
}
43+
44+
func startWorkflow(h *common.SampleHelper) {
45+
delayStart := 30 * time.Second
46+
workflowOptions := client.StartWorkflowOptions{
47+
ID: "delaystart_" + uuid.New(),
48+
TaskList: ApplicationName,
49+
ExecutionStartToCloseTimeout: time.Minute,
50+
DecisionTaskStartToCloseTimeout: time.Minute,
51+
DelayStart: delayStart,
52+
}
53+
h.StartWorkflow(workflowOptions, delayStartWorkflowName, delayStart)
54+
}
55+
56+
func registerWorkflowAndActivity(
57+
h *common.SampleHelper,
58+
) {
59+
h.RegisterWorkflowWithAlias(delayStartWorkflow, delayStartWorkflowName)
60+
h.RegisterActivity(delayStartActivity)
61+
}
62+
63+
func main() {
64+
var mode string
65+
flag.StringVar(&mode, "m", "trigger", "Mode is worker, trigger or shadower.")
66+
flag.Parse()
67+
68+
var h common.SampleHelper
69+
h.SetupServiceConfig()
70+
71+
switch mode {
72+
case "worker":
73+
registerWorkflowAndActivity(&h)
74+
startWorkers(&h)
75+
76+
// The workers are supposed to be long running process that should not exit.
77+
// Use select{} to block indefinitely for samples, you can quit by CMD+C.
78+
select {}
79+
case "shadower":
80+
registerWorkflowAndActivity(&h)
81+
startShadower(&h)
82+
83+
select {}
84+
case "trigger":
85+
startWorkflow(&h)
86+
}
87+
}

go.mod

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,65 +8,76 @@ require (
88
github.com/opentracing/opentracing-go v1.2.0
99
github.com/pborman/uuid v1.2.1
1010
github.com/pkg/errors v0.9.1
11-
github.com/stretchr/testify v1.8.0
11+
github.com/stretchr/testify v1.8.1
1212
github.com/uber-go/tally v3.4.3+incompatible
13-
github.com/uber/cadence-idl v0.0.0-20220505064515-7bb6b0808383
13+
github.com/uber/cadence-idl v0.0.0-20230905165949-03586319b849
1414
github.com/uber/jaeger-client-go v2.30.0+incompatible
15-
go.uber.org/cadence v0.19.0
15+
go.uber.org/cadence v1.2.9
1616
go.uber.org/yarpc v1.60.0
1717
go.uber.org/zap v1.23.0
1818
gopkg.in/yaml.v2 v2.4.0
1919
)
2020

2121
require (
22-
github.com/BurntSushi/toml v0.3.1 // indirect
22+
github.com/BurntSushi/toml v0.4.1 // indirect
2323
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 // indirect
24-
github.com/apache/thrift v0.0.0-20161221203622-b2a4d4ae21c7 // indirect
24+
github.com/apache/thrift v0.16.0 // indirect
2525
github.com/benbjohnson/clock v1.3.0 // indirect
2626
github.com/beorn7/perks v1.0.1 // indirect
2727
github.com/cespare/xxhash/v2 v2.1.1 // indirect
2828
github.com/cristalhq/jwt/v3 v3.1.0 // indirect
2929
github.com/davecgh/go-spew v1.1.1 // indirect
3030
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
3131
github.com/fatih/structtag v1.2.0 // indirect
32+
github.com/go-ole/go-ole v1.2.6 // indirect
3233
github.com/gogo/googleapis v1.3.2 // indirect
3334
github.com/gogo/protobuf v1.3.2 // indirect
3435
github.com/gogo/status v1.1.0 // indirect
35-
github.com/golang/mock v1.4.4 // indirect
36-
github.com/golang/protobuf v1.3.3 // indirect
36+
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
37+
github.com/golang/mock v1.5.0 // indirect
38+
github.com/golang/protobuf v1.5.3 // indirect
3739
github.com/jessevdk/go-flags v1.4.0 // indirect
3840
github.com/kisielk/errcheck v1.5.0 // indirect
3941
github.com/m3db/prometheus_client_model v0.1.0 // indirect
4042
github.com/m3db/prometheus_common v0.1.0 // indirect
4143
github.com/m3db/prometheus_procfs v0.8.1 // indirect
44+
github.com/marusama/semaphore/v2 v2.5.0 // indirect
4245
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
4346
github.com/pmezard/go-difflib v1.0.0 // indirect
44-
github.com/prometheus/client_golang v1.4.1 // indirect
47+
github.com/prometheus/client_golang v1.11.1 // indirect
4548
github.com/prometheus/client_model v0.2.0 // indirect
46-
github.com/prometheus/common v0.9.1 // indirect
47-
github.com/prometheus/procfs v0.0.9 // indirect
49+
github.com/prometheus/common v0.26.0 // indirect
50+
github.com/prometheus/procfs v0.6.0 // indirect
4851
github.com/robfig/cron v1.2.0 // indirect
49-
github.com/stretchr/objx v0.4.0 // indirect
52+
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
53+
github.com/stretchr/objx v0.5.0 // indirect
54+
github.com/tklauser/go-sysconf v0.3.11 // indirect
55+
github.com/tklauser/numcpus v0.6.0 // indirect
5056
github.com/twmb/murmur3 v1.1.6 // indirect
5157
github.com/uber-go/mapdecode v1.0.0 // indirect
52-
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
53-
github.com/uber/tchannel-go v1.22.2 // indirect
54-
go.uber.org/atomic v1.7.0 // indirect
58+
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
59+
github.com/uber/tchannel-go v1.32.1 // indirect
60+
github.com/yusufpapurcu/wmi v1.2.3 // indirect
61+
go.uber.org/atomic v1.9.0 // indirect
5562
go.uber.org/dig v1.17.0 // indirect
5663
go.uber.org/fx v1.13.1 // indirect
5764
go.uber.org/multierr v1.6.0 // indirect
5865
go.uber.org/net/metrics v1.3.0 // indirect
5966
go.uber.org/thriftrw v1.29.2 // indirect
67+
golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e // indirect
6068
golang.org/x/lint v0.0.0-20200130185559-910be7a94367 // indirect
61-
golang.org/x/mod v0.4.2 // indirect
62-
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
63-
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
64-
golang.org/x/text v0.3.3 // indirect
69+
golang.org/x/mod v0.8.0 // indirect
70+
golang.org/x/net v0.19.0 // indirect
71+
golang.org/x/oauth2 v0.1.0 // indirect
72+
golang.org/x/sys v0.15.0 // indirect
73+
golang.org/x/text v0.14.0 // indirect
6574
golang.org/x/time v0.0.0-20170927054726-6dc17368e09b // indirect
66-
golang.org/x/tools v0.1.5 // indirect
75+
golang.org/x/tools v0.6.0 // indirect
6776
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
77+
google.golang.org/appengine v1.6.7 // indirect
6878
google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce // indirect
6979
google.golang.org/grpc v1.28.0 // indirect
80+
google.golang.org/protobuf v1.31.0 // indirect
7081
gopkg.in/yaml.v3 v3.0.1 // indirect
71-
honnef.co/go/tools v0.0.1-2019.2.3 // indirect
82+
honnef.co/go/tools v0.3.2 // indirect
7283
)

0 commit comments

Comments
 (0)