Skip to content

Commit

Permalink
close function and example in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
vikstrous committed Feb 11, 2024
1 parent 9f74a15 commit d2a564e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,87 @@
# tstemporal
Type-safe Temporal Go SDK wrapper

## Example Usage

Below is a simple example demonstrating how to define a workflow and an activity, register them, and execute the workflow using `tstemporal`.

```go
package main

import (
"context"
"fmt"
"github.com/vikstrous/tstemporal"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"
"go.temporal.io/sdk/workflow"
)

func main() {
// Create a new client connected to the Temporal server.
c, err := tstemporal.Dial(client.Options{})
if err != nil {
panic(err)
}
defer c.Close()

// Define a new namespace and task queue.
nsDefault := tstemporal.NewNamespace(client.DefaultNamespace)
queueMain := tstemporal.NewQueue(nsDefault, "main")

// Define a workflow with no parameters and no return.
workflowTypeHello := tstemporal.NewWorkflow0(queueMain, "HelloWorkflow")

// Define an activity with no parameters and no return.
activityTypeHello := tstemporal.NewActivity0(queueMain, "HelloActivity")

// Register the workflow and activity in a new worker.
wrk, err := tstemporal.NewWorker(queueMain, []tstemporal.Registerable{
workflowTypeHello.WithImplementation(helloWorkflow),
activityTypeHello.WithImplementation(helloActivity),
})
if err != nil {
panic(err)
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
err = wrk.Run(ctx, c, worker.Options{})
if err != nil {
panic(err)
}
}()

// Execute the workflow and wait for it to complete.
err = workflowTypeHello.Run(ctx, c, client.StartWorkflowOptions{})
if err != nil {
panic(err)
}

fmt.Println("Workflow completed.")
}

// helloWorkflow is a workflow function that calls the HelloActivity.
func helloWorkflow(ctx workflow.Context) error {
ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
StartToCloseTimeout: time.Second * 10,
})
err := workflow.ExecuteActivity(ctx, "HelloActivity").Get(ctx, nil)
return err
}

// helloActivity is an activity function that prints "Hello, Temporal!".
func helloActivity(ctx context.Context) error {
fmt.Println("Hello, Temporal!")
return nil
}

```

This example sets up a workflow and an activity that simply prints a greeting. It demonstrates the basic setup and execution flow using `tstemporal`. To see a more complex example, look in the example directory.

## Guarantees

List of guarantees provided by this wrapper:

Expand Down
4 changes: 4 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ type Client struct {
Client client.Client
}

func (c *Client) Close() {
c.Client.Close()
}

func NewLazyClient(opts client.Options) (*Client, error) {
namespace := client.DefaultNamespace
if opts.Namespace != "" {
Expand Down
1 change: 1 addition & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func main() {
if err != nil {
panic(err)
}
defer c.Close()
wrk, err := tstemporal.NewWorker(queueMain, []tstemporal.Registerable{
activityTypeFormatName.WithImplementation(activityFormatName),
activityTypeGreet.WithImplementation(activityGreet),
Expand Down

0 comments on commit d2a564e

Please sign in to comment.