Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding delaystart a new workflow with 30 sec delay #85

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export PATH := $(GOPATH)/bin:$(PATH)
default: test

PROGS = helloworld \
delaystart \
branch \
childworkflow \
crossdomain \
Expand Down Expand Up @@ -52,6 +53,7 @@ TEST_DIRS=./cmd/samples/cron \
./cmd/samples/recipes/choice \
./cmd/samples/recipes/greetings \
./cmd/samples/recipes/helloworld \
./cmd/samples/recipes/delaystart \
./cmd/samples/recipes/cancelactivity \
./cmd/samples/recipes/pickfirst \
./cmd/samples/recipes/mutex \
Expand All @@ -75,6 +77,9 @@ cancelactivity:
helloworld:
go build -o bin/helloworld cmd/samples/recipes/helloworld/*.go

delaystart:
go build -o bin/delaystart cmd/samples/recipes/delaystart/*.go

branch:
go build -o bin/branch cmd/samples/recipes/branch/*.go

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

bins: helloworld \
delaystart \
branch \
crossdomain \
childworkflow \
Expand Down
8 changes: 8 additions & 0 deletions cmd/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ See instructions for running the Cadence Server: https://github.com/uber/cadence

## Steps to run samples
### Build Samples
* Build all the workflows at once
```
make
```
* Build a workflow individually
```
make <WORKFLOW NAME>

Example:
make helloworld
```

### Run HelloWorld Sample
* Start workers for helloworld workflow and activities
Expand Down
64 changes: 64 additions & 0 deletions cmd/samples/recipes/delaystart/delaystart_workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"context"
"time"

"go.uber.org/cadence/activity"
"go.uber.org/cadence/workflow"
"go.uber.org/zap"
)

/**
* This is the hello world workflow sample.
*/

// ApplicationName is the task list for this sample
const ApplicationName = "delaystartGroup"

const delayStartWorkflowName = "delayStartWorkflow"

// helloWorkflow workflow decider
func delayStartWorkflow(ctx workflow.Context, delayStart time.Duration) error {
ao := workflow.ActivityOptions{
ScheduleToStartTimeout: time.Minute,
StartToCloseTimeout: time.Minute,
HeartbeatTimeout: time.Second * 20,
}
ctx = workflow.WithActivityOptions(ctx, ao)

logger := workflow.GetLogger(ctx)
logger.Info("delaystart workflow started after waiting for " + delayStart.String())
var helloworldResult string
err := workflow.ExecuteActivity(ctx, delayStartActivity, delayStart).Get(ctx, &helloworldResult)
if err != nil {
logger.Error("Activity failed after waiting for "+delayStart.String(), zap.Error(err))
return err
}

// Adding a new activity to the workflow will result in a non-determinstic change for the workflow
// Please check https://cadenceworkflow.io/docs/go-client/workflow-versioning/ for more information
//
// Un-commenting the following code and the TestReplayWorkflowHistoryFromFile in replay_test.go
// will fail due to the non-determinstic change
//
// If you have a completed workflow execution without the following code and run the
// TestWorkflowShadowing in shadow_test.go or start the worker in shadow mode (using -m shadower)
// those two shadowing check will also fail due to the non-deterministic change
//
// err := workflow.ExecuteActivity(ctx, helloWorldActivity, name).Get(ctx, &helloworldResult)
// if err != nil {
// logger.Error("Activity failed.", zap.Error(err))
// return err
// }

logger.Info("Workflow completed.", zap.String("Result", helloworldResult))

return nil
}

func delayStartActivity(ctx context.Context, delayStart time.Duration) (string, error) {
logger := activity.GetLogger(ctx)
logger.Info("delayStartActivity started after " + delayStart.String())
return "Activity started after " + delayStart.String(), nil
}
31 changes: 31 additions & 0 deletions cmd/samples/recipes/delaystart/delaystart_workflow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"testing"
"time"

"github.com/stretchr/testify/require"
"go.uber.org/cadence/activity"
"go.uber.org/cadence/encoded"
"go.uber.org/cadence/testsuite"
)

func Test_Workflow(t *testing.T) {
testSuite := &testsuite.WorkflowTestSuite{}

env := testSuite.NewTestWorkflowEnvironment()
env.RegisterWorkflow(delayStartWorkflow)
env.RegisterActivity(delayStartActivity)

var activityMessage string
env.SetOnActivityCompletedListener(func(activityInfo *activity.Info, result encoded.Value, err error) {
result.Get(&activityMessage)
})

delayStart := 30 * time.Second
env.ExecuteWorkflow(delayStartWorkflow, delayStart)

require.True(t, env.IsWorkflowCompleted())
require.NoError(t, env.GetWorkflowError())
require.Equal(t, "Activity started after "+delayStart.String(), activityMessage)
}
87 changes: 87 additions & 0 deletions cmd/samples/recipes/delaystart/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"flag"
"time"

"github.com/pborman/uuid"
"go.uber.org/cadence/client"
"go.uber.org/cadence/worker"

"github.com/uber-common/cadence-samples/cmd/samples/common"
)

// This needs to be done as part of a bootstrap step when the process starts.
// The workers are supposed to be long running.
func startWorkers(h *common.SampleHelper) {
// Configure worker options.
workerOptions := worker.Options{
MetricsScope: h.WorkerMetricScope,
Logger: h.Logger,
FeatureFlags: client.FeatureFlags{
WorkflowExecutionAlreadyCompletedErrorEnabled: true,
},
}
h.StartWorkers(h.Config.DomainName, ApplicationName, workerOptions)
}

func startShadower(h *common.SampleHelper) {
workerOptions := worker.Options{
MetricsScope: h.WorkerMetricScope,
Logger: h.Logger,
EnableShadowWorker: true,
ShadowOptions: worker.ShadowOptions{
WorkflowTypes: []string{delayStartWorkflowName},
WorkflowStatus: []string{"Completed"},
ExitCondition: worker.ShadowExitCondition{
ShadowCount: 10,
},
},
}
h.StartWorkers(h.Config.DomainName, ApplicationName, workerOptions)
}

func startWorkflow(h *common.SampleHelper) {
delayStart := 30 * time.Second
workflowOptions := client.StartWorkflowOptions{
ID: "delaystart_" + uuid.New(),
TaskList: ApplicationName,
ExecutionStartToCloseTimeout: time.Minute,
DecisionTaskStartToCloseTimeout: time.Minute,
DelayStart: delayStart,
}
h.StartWorkflow(workflowOptions, delayStartWorkflowName, delayStart)
}

func registerWorkflowAndActivity(
h *common.SampleHelper,
) {
h.RegisterWorkflowWithAlias(delayStartWorkflow, delayStartWorkflowName)
h.RegisterActivity(delayStartActivity)
}

func main() {
var mode string
flag.StringVar(&mode, "m", "trigger", "Mode is worker, trigger or shadower.")
flag.Parse()

var h common.SampleHelper
h.SetupServiceConfig()

switch mode {
case "worker":
registerWorkflowAndActivity(&h)
startWorkers(&h)

// The workers are supposed to be long running process that should not exit.
// Use select{} to block indefinitely for samples, you can quit by CMD+C.
select {}
case "shadower":
registerWorkflowAndActivity(&h)
startShadower(&h)

select {}
case "trigger":
startWorkflow(&h)
}
}
51 changes: 31 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,76 @@ require (
github.com/opentracing/opentracing-go v1.2.0
github.com/pborman/uuid v1.2.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.8.1
github.com/uber-go/tally v3.4.3+incompatible
github.com/uber/cadence-idl v0.0.0-20220505064515-7bb6b0808383
github.com/uber/cadence-idl v0.0.0-20230905165949-03586319b849
github.com/uber/jaeger-client-go v2.30.0+incompatible
go.uber.org/cadence v0.19.0
go.uber.org/cadence v1.2.9
go.uber.org/yarpc v1.60.0
go.uber.org/zap v1.23.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/BurntSushi/toml v0.4.1 // indirect
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 // indirect
github.com/apache/thrift v0.0.0-20161221203622-b2a4d4ae21c7 // indirect
github.com/apache/thrift v0.16.0 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cristalhq/jwt/v3 v3.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gogo/googleapis v1.3.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gogo/status v1.1.0 // indirect
github.com/golang/mock v1.4.4 // indirect
github.com/golang/protobuf v1.3.3 // indirect
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
github.com/golang/mock v1.5.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/jessevdk/go-flags v1.4.0 // indirect
github.com/kisielk/errcheck v1.5.0 // indirect
github.com/m3db/prometheus_client_model v0.1.0 // indirect
github.com/m3db/prometheus_common v0.1.0 // indirect
github.com/m3db/prometheus_procfs v0.8.1 // indirect
github.com/marusama/semaphore/v2 v2.5.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.4.1 // indirect
github.com/prometheus/client_golang v1.11.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.9.1 // indirect
github.com/prometheus/procfs v0.0.9 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/stretchr/objx v0.4.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/twmb/murmur3 v1.1.6 // indirect
github.com/uber-go/mapdecode v1.0.0 // indirect
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
github.com/uber/tchannel-go v1.22.2 // indirect
go.uber.org/atomic v1.7.0 // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/uber/tchannel-go v1.32.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/dig v1.17.0 // indirect
go.uber.org/fx v1.13.1 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/net/metrics v1.3.0 // indirect
go.uber.org/thriftrw v1.29.2 // indirect
golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e // indirect
golang.org/x/lint v0.0.0-20200130185559-910be7a94367 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/text v0.3.3 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.1.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.0.0-20170927054726-6dc17368e09b // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce // indirect
google.golang.org/grpc v1.28.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
honnef.co/go/tools v0.0.1-2019.2.3 // indirect
honnef.co/go/tools v0.3.2 // indirect
)
Loading
Loading