-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add typed search attribute sample and Update Go SDK to v1.26.0 (#331)
* Add typed search attribute sample * Update to Go SDK v1.26.0
- Loading branch information
1 parent
3cc0870
commit 5c63363
Showing
13 changed files
with
290 additions
and
57 deletions.
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
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
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"log" | ||
|
||
"github.com/pborman/uuid" | ||
"google.golang.org/protobuf/types/known/durationpb" | ||
|
||
"strconv" | ||
|
||
|
@@ -35,7 +36,7 @@ func main() { | |
Namespace: uuidvar + "_" + strconv.Itoa(i), | ||
Description: "Namespace Description " + strconv.Itoa(i), | ||
OwnerEmail: "[email protected]", | ||
WorkflowExecutionRetentionPeriod: &retention, | ||
WorkflowExecutionRetentionPeriod: durationpb.New(retention), | ||
} | ||
if err = c.Register(context.Background(), req); err != nil { | ||
log.Fatalln("Unable to register namespace", err) | ||
|
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,14 @@ | ||
### Typed Search Attributes Sample | ||
|
||
This sample shows how to use and test the Typed Search Attributes API. | ||
|
||
### 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 typed-searchattributes/worker/main.go | ||
``` | ||
3) Run the following command to start the example: | ||
``` | ||
go run typed-searchattributes/starter/main.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,109 @@ | ||
package typedsearchattributes | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"go.temporal.io/sdk/log" | ||
"go.temporal.io/sdk/temporal" | ||
"go.temporal.io/sdk/workflow" | ||
) | ||
|
||
/* | ||
* This sample shows how to use the typed search attributes API. | ||
*/ | ||
|
||
var ( | ||
// CustomIntKey is the key for a custom int search attribute. | ||
CustomIntKey = temporal.NewSearchAttributeKeyInt64("CustomIntField") | ||
// CustomKeyword is the key for a custom keyword search attribute. | ||
CustomKeyword = temporal.NewSearchAttributeKeyString("CustomKeywordField") | ||
// CustomBool is the key for a custom bool search attribute. | ||
CustomBool = temporal.NewSearchAttributeKeyBool("CustomBoolField") | ||
// CustomDouble is the key for a custom double search attribute. | ||
CustomDouble = temporal.NewSearchAttributeKeyFloat64("CustomDoubleField") | ||
// CustomStringField is the key for a custom string search attribute. | ||
CustomStringField = temporal.NewSearchAttributeKeyString("CustomStringField") | ||
// CustomDatetimeField is the key for a custom datetime search attribute. | ||
CustomDatetimeField = temporal.NewSearchAttributeKeyTime("CustomDatetimeField") | ||
) | ||
|
||
// SearchAttributesWorkflow workflow definition | ||
func SearchAttributesWorkflow(ctx workflow.Context) error { | ||
logger := workflow.GetLogger(ctx) | ||
logger.Info("SearchAttributes workflow started") | ||
|
||
// Get search attributes that were provided when workflow was started. | ||
searchAttributes := workflow.GetTypedSearchAttributes(ctx) | ||
currentIntValue, ok := searchAttributes.GetInt64(CustomIntKey) | ||
if !ok { | ||
return errors.New("CustomIntField is not set") | ||
} | ||
logger.Info("Current search attribute value.", "CustomIntField", currentIntValue) | ||
|
||
// Upsert search attributes. | ||
|
||
// This won't persist search attributes on server because commands are not sent to server, | ||
// but local cache will be updated. | ||
err := workflow.UpsertTypedSearchAttributes(ctx, | ||
CustomIntKey.ValueSet(2), | ||
CustomKeyword.ValueSet("Update1"), | ||
CustomBool.ValueSet(true), | ||
CustomDouble.ValueSet(3.14), | ||
CustomDatetimeField.ValueSet(workflow.Now(ctx).UTC()), | ||
CustomStringField.ValueSet("String field is for text. When query, it will be tokenized for partial match."), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Print current search attributes with modifications above. | ||
searchAttributes = workflow.GetTypedSearchAttributes(ctx) | ||
err = printSearchAttributes(searchAttributes, logger) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Update search attributes again. | ||
err = workflow.UpsertTypedSearchAttributes(ctx, | ||
CustomKeyword.ValueSet("Update2"), | ||
CustomIntKey.ValueUnset(), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Sleep to allow update to be visible in search. | ||
err = workflow.Sleep(ctx, 1*time.Second) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Print current search attributes. | ||
searchAttributes = workflow.GetTypedSearchAttributes(ctx) | ||
err = printSearchAttributes(searchAttributes, logger) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Info("Workflow completed.") | ||
return nil | ||
} | ||
|
||
func printSearchAttributes(searchAttributes temporal.SearchAttributes, logger log.Logger) error { | ||
//workflowcheck:ignore | ||
if searchAttributes.Size() == 0 { | ||
logger.Info("Current search attributes are empty.") | ||
return nil | ||
} | ||
|
||
var builder strings.Builder | ||
//workflowcheck:ignore Only iterates for logging reasons | ||
for k, v := range searchAttributes.GetUntypedValues() { | ||
builder.WriteString(fmt.Sprintf("%s=%v\n", k.GetName(), v)) | ||
} | ||
logger.Info(fmt.Sprintf("Current search attribute values:\n%s", builder.String())) | ||
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,38 @@ | ||
package typedsearchattributes | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.temporal.io/sdk/temporal" | ||
"go.temporal.io/sdk/testsuite" | ||
) | ||
|
||
func Test_Workflow(t *testing.T) { | ||
testSuite := &testsuite.WorkflowTestSuite{} | ||
env := testSuite.NewTestWorkflowEnvironment() | ||
|
||
// mock search attributes on start | ||
_ = env.SetTypedSearchAttributesOnStart(temporal.NewSearchAttributes(CustomIntKey.ValueSet(1))) | ||
|
||
// mock upsert operations | ||
env.OnUpsertTypedSearchAttributes( | ||
temporal.NewSearchAttributes( | ||
CustomIntKey.ValueSet(2), | ||
CustomKeyword.ValueSet("Update1"), | ||
CustomBool.ValueSet(true), | ||
CustomDouble.ValueSet(3.14), | ||
CustomDatetimeField.ValueSet(env.Now().UTC()), | ||
CustomStringField.ValueSet("String field is for text. When query, it will be tokenized for partial match."), | ||
)).Return(nil).Once() | ||
|
||
env.OnUpsertTypedSearchAttributes( | ||
temporal.NewSearchAttributes( | ||
CustomKeyword.ValueSet("Update2"), | ||
CustomIntKey.ValueUnset(), | ||
)).Return(nil).Once() | ||
|
||
env.ExecuteWorkflow(SearchAttributesWorkflow) | ||
require.True(t, env.IsWorkflowCompleted()) | ||
require.NoError(t, env.GetWorkflowError()) | ||
} |
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,35 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/pborman/uuid" | ||
"go.temporal.io/sdk/client" | ||
"go.temporal.io/sdk/temporal" | ||
|
||
typedsearchattributes "github.com/temporalio/samples-go/typed-searchattributes" | ||
) | ||
|
||
func main() { | ||
// The client is a heavyweight object that should be created once per process. | ||
c, err := client.Dial(client.Options{ | ||
HostPort: client.DefaultHostPort, | ||
}) | ||
if err != nil { | ||
log.Fatalln("Unable to create client", err) | ||
} | ||
defer c.Close() | ||
|
||
workflowOptions := client.StartWorkflowOptions{ | ||
ID: "typed-search_attributes_" + uuid.New(), | ||
TaskQueue: "typed-search-attributes", | ||
TypedSearchAttributes: temporal.NewSearchAttributes(typedsearchattributes.CustomIntKey.ValueSet(1)), | ||
} | ||
|
||
we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, typedsearchattributes.SearchAttributesWorkflow) | ||
if err != nil { | ||
log.Fatalln("Unable to execute workflow", err) | ||
} | ||
log.Println("Started workflow", "WorkflowID", we.GetID(), "RunID", we.GetRunID()) | ||
} |
Oops, something went wrong.