-
Notifications
You must be signed in to change notification settings - Fork 94
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
vishwa-uber
merged 4 commits into
cadence-workflow:master
from
vishwa-uber:vishwa/delay_start_example
Jan 28, 2025
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
[ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
cmd/samples/recipes/delaystart/delaystart_workflow_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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