Skip to content
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
15 changes: 15 additions & 0 deletions go-sandbox/activities/standalone/activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package standalone

import (
"context"
"fmt"
)

const (
TaskQueue = "standalone-activity-helloworld"
ActivityID = "standalone_activity_helloworld_ActivityID"
)

func Activity(ctx context.Context, name string) (string, error) {
return fmt.Sprintf("Hello %s!", name), nil
}
30 changes: 30 additions & 0 deletions go-sandbox/activities/standalone/activity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package standalone_test

import (
"testing"

"github.com/stretchr/testify/suite"
"go.temporal.io/sdk/testsuite"

"my-org/quickstart/activities/standalone"
)

type StandaloneActivityTestSuite struct {
suite.Suite
testsuite.WorkflowTestSuite
}

func TestStandaloneActivityTestSuite(t *testing.T) {
suite.Run(t, new(StandaloneActivityTestSuite))
}

func (s *StandaloneActivityTestSuite) Test_Activity() {
env := s.NewTestActivityEnvironment()
env.RegisterActivity(standalone.Activity)

val, err := env.ExecuteActivity(standalone.Activity, "Temporal")
s.NoError(err)
var result string
s.NoError(val.Get(&result))
s.Equal("Hello Temporal!", result)
}
106 changes: 106 additions & 0 deletions go-sandbox/activities/standalone/starter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package main

import (
"context"
"flag"
"log"
"time"

"go.temporal.io/sdk/client"

"my-org/quickstart/activities/standalone"
)

func main() {
command := flag.String("command", "start", "one of: start, handle, list, count")
runID := flag.String("run-id", "", "Run ID of the Activity Execution for the handle command")
flag.Parse()

c, err := client.Dial(client.Options{})
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()

switch *command {
case "start":
startActivity(c)
case "handle":
getActivityHandle(c, *runID)
case "list":
listActivities(c)
case "count":
countActivities(c)
default:
log.Fatalln("Unknown command", *command)
}
}

func startActivity(c client.Client) {
handle, err := c.ExecuteActivity(context.Background(), client.StartActivityOptions{
ID: standalone.ActivityID,
TaskQueue: standalone.TaskQueue,
StartToCloseTimeout: 10 * time.Second,
}, standalone.Activity, "Temporal")
if err != nil {
log.Fatalln("Unable to execute activity", err)
}

// @@@SNIPSTART go-standalone-activity-get-result
var result string
err = handle.Get(context.Background(), &result)
if err != nil {
log.Fatalln("Activity failed", err)
}
log.Println("Activity result:", result)
// @@@SNIPEND
}

func getActivityHandle(c client.Client, runID string) {
// @@@SNIPSTART go-standalone-activity-get-handle
handle := c.GetActivityHandle(client.GetActivityHandleOptions{
ActivityID: "standalone_activity_helloworld_ActivityID",
RunID: runID, // Leave empty to target the latest Activity Execution.
})

// Use the handle to get the result, describe, cancel, or terminate
var result string
err := handle.Get(context.Background(), &result)
if err != nil {
log.Fatalln("Unable to get activity result", err)
}
// @@@SNIPEND
log.Println("Activity result:", result)
}

func listActivities(c client.Client) {
// @@@SNIPSTART go-standalone-activity-list
resp, err := c.ListActivities(context.Background(), client.ListActivitiesOptions{
Query: "TaskQueue = 'standalone-activity-helloworld'",
})
if err != nil {
log.Fatalln("Unable to list activities", err)
}

for info, err := range resp.Results {
if err != nil {
log.Fatalln("Error iterating activities", err)
}
log.Printf("ActivityID: %s, Type: %s, Status: %v\n",
info.ActivityID, info.ActivityType, info.Status)
}
// @@@SNIPEND
}

func countActivities(c client.Client) {
// @@@SNIPSTART go-standalone-activity-count
resp, err := c.CountActivities(context.Background(), client.CountActivitiesOptions{
Query: "TaskQueue = 'standalone-activity-helloworld'",
})
if err != nil {
log.Fatalln("Unable to count activities", err)
}

log.Println("Total activities:", resp.Count)
// @@@SNIPEND
}
26 changes: 26 additions & 0 deletions go-sandbox/activities/standalone/worker/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"log"

"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"

"my-org/quickstart/activities/standalone"
)

func main() {
c, err := client.Dial(client.Options{})
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()

// Registering a Standalone Activity is the same as registering any other Activity.
w := worker.New(c, standalone.TaskQueue, worker.Options{})
w.RegisterActivity(standalone.Activity)

if err := w.Run(worker.InterruptCh()); err != nil {
log.Fatalln("Unable to start Worker", err)
}
}
Loading