-
Notifications
You must be signed in to change notification settings - Fork 204
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
Add sleep-for-days sample #384
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
931c0f5
sleep-for-days sample
THardy98 492f5a5
add test, fixed bugs
THardy98 7a05457
Merge branch 'main' into pithy-sleep-for-days
THardy98 ef9485f
Merge branch 'main' into pithy-sleep-for-days
THardy98 830edd4
Merge branch 'main' into pithy-sleep-for-days
THardy98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
### Sleep for days | ||
|
||
This sample demonstrates how to create a Temporal workflow that runs forever, sending an email every 30 days. | ||
|
||
### Steps to run this sample: | ||
1) Run a [Temporal service](https://github.com/temporalio/samples-go/tree/main/#how-to-use). | ||
2) Run the following command to start the worker | ||
``` | ||
go run worker/main.go | ||
``` | ||
3) Run the following command to start the example | ||
``` | ||
go run starter/main.go | ||
``` | ||
|
||
This sample will run indefinitely until you send a `complete` signal to the workflow. See how to send a signal via Temporal CLI [here](https://docs.temporal.io/cli/workflow#signal). |
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,37 @@ | ||
package sleepfordays | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"go.temporal.io/sdk/activity" | ||
"go.temporal.io/sdk/workflow" | ||
) | ||
|
||
func SleepForDaysWorkflow(ctx workflow.Context) (string, error) { | ||
ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{ | ||
StartToCloseTimeout: 10 * time.Second, | ||
}) | ||
|
||
isComplete := false | ||
sigChan := workflow.GetSignalChannel(ctx, "complete") | ||
|
||
for !isComplete { | ||
workflow.ExecuteActivity(ctx, SendEmailActivity, "Sleeping for 30 days") | ||
selector := workflow.NewSelector(ctx) | ||
selector.AddFuture(workflow.NewTimer(ctx, time.Hour*24*30), func(f workflow.Future) {}) | ||
selector.AddReceive(sigChan, func(c workflow.ReceiveChannel, more bool) { | ||
isComplete = true | ||
}) | ||
selector.Select(ctx) | ||
} | ||
|
||
return "done", nil | ||
} | ||
|
||
// A stub Activity for sending an email. | ||
func SendEmailActivity(ctx context.Context, msg string) error { | ||
activity.GetLogger(ctx).Info(fmt.Sprintf(`Sending email: "%v"\n`, msg)) | ||
return nil | ||
} |
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,39 @@ | ||
package sleepfordays | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
|
||
"go.temporal.io/sdk/testsuite" | ||
) | ||
|
||
func TestSleepForDaysWorkflow(t *testing.T) { | ||
testSuite := &testsuite.WorkflowTestSuite{} | ||
env := testSuite.NewTestWorkflowEnvironment() | ||
|
||
numActivityCalls := 0 | ||
env.RegisterActivity(SendEmailActivity) | ||
env.OnActivity(SendEmailActivity, mock.Anything, mock.Anything).Run( | ||
func(args mock.Arguments) { numActivityCalls++ }, | ||
).Return(nil) | ||
|
||
startTime := env.Now() | ||
|
||
// Time-skip 90 days. | ||
env.RegisterDelayedCallback(func() { | ||
// Check that the activity has been called 3 times. | ||
require.Equal(t, 3, numActivityCalls) | ||
// Send the signal to complete the workflow. | ||
env.SignalWorkflow("complete", nil) | ||
// Expect no more activity calls to have been made - workflow is complete. | ||
require.Equal(t, 3, numActivityCalls) | ||
// Expect more than 90 days to have passed. | ||
require.Equal(t, env.Now().Sub(startTime), time.Hour*24*90) | ||
}, time.Hour*24*90) | ||
|
||
// Execute workflow. | ||
env.ExecuteWorkflow(SleepForDaysWorkflow) | ||
} |
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,36 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
sleepfordays "github.com/temporalio/samples-go/sleep-for-days" | ||
"go.temporal.io/sdk/client" | ||
) | ||
|
||
func main() { | ||
c, err := client.Dial(client.Options{}) | ||
if err != nil { | ||
log.Fatalln("Unable to create client", err) | ||
} | ||
defer c.Close() | ||
|
||
workflowOptions := client.StartWorkflowOptions{ | ||
TaskQueue: "sleep-for-days", | ||
} | ||
|
||
we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, sleepfordays.SleepForDaysWorkflow) | ||
if err != nil { | ||
log.Fatalln("Unable to execute workflow", err) | ||
} | ||
|
||
log.Println("Started sleep-for-days workflow", "WorkflowID", we.GetID(), "RunID", we.GetRunID()) | ||
|
||
// Synchronously wait for the workflow completion (will run indefinitely until it receives a signal) | ||
var result string | ||
err = we.Get(context.Background(), &result) | ||
if err != nil { | ||
log.Fatalln("Unable get workflow result", err) | ||
} | ||
log.Println("Workflow result:", result) | ||
} |
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,29 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"go.temporal.io/sdk/client" | ||
"go.temporal.io/sdk/worker" | ||
|
||
sleepfordays "github.com/temporalio/samples-go/sleep-for-days" | ||
) | ||
|
||
func main() { | ||
// The client and worker are heavyweight objects that should be created once per process. | ||
c, err := client.Dial(client.Options{}) | ||
if err != nil { | ||
log.Fatalln("Unable to create client", err) | ||
} | ||
defer c.Close() | ||
|
||
w := worker.New(c, "sleep-for-days", worker.Options{}) | ||
|
||
w.RegisterWorkflow(sleepfordays.SleepForDaysWorkflow) | ||
w.RegisterActivity(sleepfordays.SendEmailActivity) | ||
|
||
err = w.Run(worker.InterruptCh()) | ||
if err != nil { | ||
log.Fatalln("Unable to start worker", err) | ||
} | ||
} |
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.
Might be simpler to just check the signal channel every loop, also this sample is a bit different then the python one temporalio/samples-python@2f3f2ac. In the python one your signal takes no input, but the Go one does. I think we should match python here.
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.
Removed the signal input (and workflow input).