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 1 commit
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
9 changes: 9 additions & 0 deletions cmd/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ 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 delaystart
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd avoid adding an extra example here. make helloworld seems sufficient to show how to build an individual workflow

make hellowrold
vishwa-uber marked this conversation as resolved.
Show resolved Hide resolved
```

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

import (
"context"
"github.com/stretchr/testify/require"
"go.uber.org/cadence/activity"
"go.uber.org/cadence/testsuite"
"go.uber.org/cadence/worker"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"testing"
)

func sampleActivity(ctx context.Context) error {
logger := activity.GetLogger(ctx)
logger.Info("test logging")
return nil
}

func Test_Activity_Noop_Logger(t *testing.T) {
testSuite := &testsuite.WorkflowTestSuite{}
env := testSuite.NewTestActivityEnvironment()
env.RegisterActivity(sampleActivity)
val, err := env.ExecuteActivity(sampleActivity)
require.Nil(t, err)
require.True(t, !val.HasValue())
}

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

logger, err := zap.NewProduction()
require.Nil(t, err)

var outputLogs []string
logger = logger.WithOptions(zap.Hooks(
func(entry zapcore.Entry) error {
outputLogs = append(outputLogs, entry.Message)
return nil
},
))

env.SetWorkerOptions(worker.Options{
Logger: logger,
})
env.RegisterActivity(sampleActivity)

val, err := env.ExecuteActivity(sampleActivity)

require.Nil(t, err)
require.True(t, !val.HasValue())
require.True(t, len(outputLogs)==1)
require.True(t, outputLogs[0] == "test logging")
}
159 changes: 159 additions & 0 deletions cmd/samples/recipes/delaystart/delaystart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used. lets delete

{
"eventId":1,
"timestamp":1558126752505445000,
"eventType":"WorkflowExecutionStarted",
"version":-24,
"taskId":33554432,
"workflowExecutionStartedEventAttributes":{
"workflowType":{
"name":"github.com/uber-common/cadence-samples/cmd/samples/recipes/delaystart.delayStartWorkflow"
},
"taskList":{
"name":"helloWorldGroup"
},
"input":"IkNhZGVuY2UiCg==",
"executionStartToCloseTimeoutSeconds":3600,
"taskStartToCloseTimeoutSeconds":10,
"identity":"66434@longer-C02V60N3HTDG@",
"attempt":0,
"firstDecisionTaskBackoffSeconds":0
}
},
{
"eventId":2,
"timestamp":1558126752505631000,
"eventType":"DecisionTaskScheduled",
"version":-24,
"taskId":33554433,
"decisionTaskScheduledEventAttributes":{
"taskList":{
"name":"helloWorldGroup"
},
"startToCloseTimeoutSeconds":10,
"attempt":0
}
},
{
"eventId":3,
"timestamp":1558126757360699000,
"eventType":"DecisionTaskStarted",
"version":-24,
"taskId":33554438,
"decisionTaskStartedEventAttributes":{
"scheduledEventId":2,
"identity":"66471@longer-C02V60N3HTDG@helloWorldGroup",
"requestId":"665325ec-74eb-4337-bf82-fceeb60e2196"
}
},
{
"eventId":4,
"timestamp":1558126757385307000,
"eventType":"DecisionTaskCompleted",
"version":-24,
"taskId":33554441,
"decisionTaskCompletedEventAttributes":{
"scheduledEventId":2,
"startedEventId":3,
"identity":"66471@longer-C02V60N3HTDG@helloWorldGroup",
"binaryChecksum":"b2e32759177ccbb3e67ad7694aec233c"
}
},
{
"eventId":5,
"timestamp":1558126757385333000,
"eventType":"ActivityTaskScheduled",
"version":-24,
"taskId":33554442,
"activityTaskScheduledEventAttributes":{
"activityId":"0",
"activityType":{
"name":"github.com/uber-common/cadence-samples/cmd/samples/recipes/helloworld.helloWorldActivity"
},
"taskList":{
"name":"helloWorldGroup"
},
"input":"IkNhZGVuY2UiCg==",
"scheduleToCloseTimeoutSeconds":3600,
"scheduleToStartTimeoutSeconds":3600,
"startToCloseTimeoutSeconds":3600,
"heartbeatTimeoutSeconds":3600,
"decisionTaskCompletedEventId":4
}
},
{
"eventId":6,
"timestamp":1558126757393919000,
"eventType":"ActivityTaskStarted",
"version":-24,
"taskId":33554446,
"activityTaskStartedEventAttributes":{
"scheduledEventId":5,
"identity":"66471@longer-C02V60N3HTDG@helloWorldGroup",
"requestId":"45c4006a-ae7c-4392-baa6-c090857f884b",
"attempt":0
}
},
{
"eventId":7,
"timestamp":1558126757403468000,
"eventType":"ActivityTaskCompleted",
"version":-24,
"taskId":33554447,
"activityTaskCompletedEventAttributes":{
"result":"IkhlbGxvIENhZGVuY2UhIgo=",
"scheduledEventId":5,
"startedEventId":6,
"identity":"66471@longer-C02V60N3HTDG@helloWorldGroup"
}
},
{
"eventId":8,
"timestamp":1558126757403476000,
"eventType":"DecisionTaskScheduled",
"version":-24,
"taskId":33554450,
"decisionTaskScheduledEventAttributes":{
"taskList":{
"name":"longer-C02V60N3HTDG:33ab3ada-4636-4386-8575-81dd8dc02e9a"
},
"startToCloseTimeoutSeconds":10,
"attempt":0
}
},
{
"eventId":9,
"timestamp":1558126757410564000,
"eventType":"DecisionTaskStarted",
"version":-24,
"taskId":33554454,
"decisionTaskStartedEventAttributes":{
"scheduledEventId":8,
"identity":"66471@longer-C02V60N3HTDG@helloWorldGroup",
"requestId":"cb1fdadf-f46b-4840-9b97-863f4b3b6b11"
}
},
{
"eventId":10,
"timestamp":1558126757491491000,
"eventType":"DecisionTaskCompleted",
"version":-24,
"taskId":33554457,
"decisionTaskCompletedEventAttributes":{
"scheduledEventId":8,
"startedEventId":9,
"identity":"66471@longer-C02V60N3HTDG@helloWorldGroup",
"binaryChecksum":"b2e32759177ccbb3e67ad7694aec233c"
}
},
{
"eventId":11,
"timestamp":1558126757491513000,
"eventType":"WorkflowExecutionCompleted",
"version":-24,
"taskId":33554458,
"workflowExecutionCompletedEventAttributes":{
"decisionTaskCompletedEventId":10
}
}
]
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)
}
Loading
Loading