temporal go客户端,Client.CheckHealth()示例

huangapple go评论86阅读模式
英文:

temporal go client, Client.CheckHealth() example

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对temporalGo SDK都是新手。Client接口定义了一个CheckHealth方法。我在samples中找不到它的简单示例用法。

我成功执行了该方法,但它没有返回任何内容:

package main

import (
	"context"
	"log"

	"go.temporal.io/sdk/client"

	"github.com/temporalio/samples-go/helloworld"
)

func main() {
	log.Println("Starting")

	// 客户端是一个重量级对象,每个进程只应创建一次。
	c, err := client.Dial(client.Options{})
	if err != nil {
		log.Fatalln("无法创建客户端", err)
	}
	log.Println("客户端已创建", c)

	// 检查客户端健康状态
	checkHealthRequest := client.CheckHealthRequest{}

	checkHealthResponse, err := c.CheckHealth(context.Background(), &checkHealthRequest)
	if err != nil {
		log.Fatalln("无法检查健康状态", err)
	}
	log.Println("checkHealthResponse", checkHealthResponse)

	defer c.Close()

	workflowOptions := client.StartWorkflowOptions{
		ID:        "hello_world_workflowID",
		TaskQueue: "hello-world",
	}

	we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, helloworld.Workflow, "Temporal")
	if err != nil {
		log.Fatalln("无法执行工作流", err)
	}

	log.Println("已启动工作流", "WorkflowID", we.GetID(), "RunID", we.GetRunID())

	// 同步等待工作流完成。
	var result string
	err = we.Get(context.Background(), &result)
	if err != nil {
		log.Fatalln("无法获取工作流结果", err)
	}
	log.Println("工作流结果:", result)
}

返回结果:

gitpod /workspace/samples-go (main) $ go run helloworld/starter/main_matt.go
2022/08/01 03:15:38 Starting
2022/08/01 03:15:38 INFO  未为Temporal客户端配置日志记录器。已创建默认日志记录器。
2022/08/01 03:15:38 客户端已创建 &{0xc0001a8950 0xc0002a7180 default 0xc0002aa3c0 0xc0001a4c18 {} 15492@temporalio-samplesgo-3fes2zok5o7@ 0xc0001fe460 [] [] 0xc0001a8958 0xc00019fc50 0xc0002b6860 {{0 0} 0 0 0 0}}
2022/08/01 03:15:38 checkHealthResponse &{}
2022/08/01 03:15:38 已启动工作流 WorkflowID hello_world_workflowID RunID 80727872-4df5-4fab-b96a-484276748e36
2022/08/01 03:15:38 工作流结果: Hello Temporal!
英文:

I am new to temporal, using the Go SDK. The Client interface defines a CheckHealth method. Where can I find a simple example of its usage? I couldn't find anything inside the samples.

I got the method to execute, but it returns nothing:

package main
import (
"context"
"log"
"go.temporal.io/sdk/client"
"github.com/temporalio/samples-go/helloworld"
)
func main() {
log.Println("Starting")
// The client is a heavyweight object that should be created once per process.
c, err := client.Dial(client.Options{})
if err != nil {
log.Fatalln("Unable to create client", err)
}
log.Println("Client created", c)
// Check client health
checkHealthRequest := client.CheckHealthRequest{
}
checkHealthResponse, err := c.CheckHealth(context.Background(), &checkHealthRequest) 
if err != nil {
log.Fatalln("Unable to check health", err)
}
log.Println("checkHealthResponse", checkHealthResponse)
defer c.Close()
workflowOptions := client.StartWorkflowOptions{
ID:        "hello_world_workflowID",
TaskQueue: "hello-world",
}
we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, helloworld.Workflow, "Temporal")
if err != nil {
log.Fatalln("Unable to execute workflow", err)
}
log.Println("Started workflow", "WorkflowID", we.GetID(), "RunID", we.GetRunID())
// Synchronously wait for the workflow completion.
var result string
err = we.Get(context.Background(), &result)
if err != nil {
log.Fatalln("Unable get workflow result", err)
}
log.Println("Workflow result:", result)
}

Returns:

gitpod /workspace/samples-go (main) $ go run helloworld/starter/main_matt.go
2022/08/01 03:15:38 Starting
2022/08/01 03:15:38 INFO  No logger configured for temporal client. Created default one.
2022/08/01 03:15:38 Client created &{0xc0001a8950 0xc0002a7180 default 0xc0002aa3c0 0xc0001a4c18 {} 15492@temporalio-samplesgo-3fes2zok5o7@ 0xc0001fe460 [] [] 0xc0001a8958 0xc00019fc50 0xc0002b6860 {{0 0} 0 0 0 0}}
2022/08/01 03:15:38 checkHealthResponse &{}
2022/08/01 03:15:38 Started workflow WorkflowID hello_world_workflowID RunID 80727872-4df5-4fab-b96a-484276748e36
2022/08/01 03:15:38 Workflow result: Hello Temporal!

答案1

得分: 1

根据Cerise Limón的评论提供的答案:

package main

import (
	"context"
	"log"

	"go.temporal.io/sdk/client"

	"github.com/temporalio/samples-go/helloworld"
)

func main() {
	log.Println("Starting")

	// 创建一个客户端对象,这是一个重量级对象,每个进程只需要创建一次
	c, err := client.Dial(client.Options{})
	if err != nil {
		log.Fatalln("无法创建客户端", err)
	}
	log.Println("客户端已创建", c)

	// 检查客户端的健康状态
	// 文档中说:“如果检查失败,将返回一个错误”。如果 err == nil,则表示健康状态良好。
	if _, err := c.CheckHealth(context.Background(), &client.CheckHealthRequest{}); err != nil {
		/* 健康状态不好 */
		log.Println("健康状态不好")
	} else {
		/* 健康状态良好 */
		log.Println("健康状态良好")
	}

	defer c.Close()

	workflowOptions := client.StartWorkflowOptions{
		ID:        "hello_world_workflowID",
		TaskQueue: "hello-world",
	}

	we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, helloworld.Workflow, "Temporal")
	if err != nil {
		log.Fatalln("无法执行工作流", err)
	}

	log.Println("已启动工作流", "WorkflowID", we.GetID(), "RunID", we.GetRunID())

	// 同步等待工作流完成
	var result string
	err = we.Get(context.Background(), &result)
	if err != nil {
		log.Fatalln("无法获取工作流结果", err)
	}
	log.Println("工作流结果:", result)
}
gitpod /workspace/samples-go (main) $ go run helloworld/starter/main_matt.go
2022/08/01 03:30:29 Starting
2022/08/01 03:30:29 INFO  No logger configured for temporal client. Created default one.
2022/08/01 03:30:29 客户端已创建 &{0xc00012e950 0xc000231180 default 0xc0002323c0 0xc00012ac18 {} 18770@temporalio-samplesgo-3fes2zok5o7@ 0xc00017e460 [] [] 0xc00012e958 0xc000125c50 0xc000038748 {{0 0} 0 0 0 0}}
2022/08/01 03:30:29 健康状态良好
2022/08/01 03:30:29 已启动工作流 WorkflowID hello_world_workflowID RunID 03cfd328-77c1-4d7a-9d5e-86d82e44c034
2022/08/01 03:30:29 工作流结果: Hello Temporal!
英文:

Answer based on comments from Cerise Limón above

package main
import (
"context"
"log"
"go.temporal.io/sdk/client"
"github.com/temporalio/samples-go/helloworld"
)
func main() {
log.Println("Starting")
// The client is a heavyweight object that should be created once per process.
c, err := client.Dial(client.Options{})
if err != nil {
log.Fatalln("Unable to create client", err)
}
log.Println("Client created", c)
// Check client health
// The documentation says "If the check fails, an error is returned." The health is good if err == nil. 
if _, err := c.CheckHealth(context.Background(), &client.CheckHealthRequest{}); err != nil {
/* health is bad */
log.Println("Health is bad")
} else {
/* health is good */
log.Println("Health is good")
}
defer c.Close()
workflowOptions := client.StartWorkflowOptions{
ID:        "hello_world_workflowID",
TaskQueue: "hello-world",
}
we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, helloworld.Workflow, "Temporal")
if err != nil {
log.Fatalln("Unable to execute workflow", err)
}
log.Println("Started workflow", "WorkflowID", we.GetID(), "RunID", we.GetRunID())
// Synchronously wait for the workflow completion.
var result string
err = we.Get(context.Background(), &result)
if err != nil {
log.Fatalln("Unable get workflow result", err)
}
log.Println("Workflow result:", result)
}
gitpod /workspace/samples-go (main) $ go run helloworld/starter/main_matt.go
2022/08/01 03:30:29 Starting
2022/08/01 03:30:29 INFO  No logger configured for temporal client. Created default one.
2022/08/01 03:30:29 Client created &{0xc00012e950 0xc000231180 default 0xc0002323c0 0xc00012ac18 {} 18770@temporalio-samplesgo-3fes2zok5o7@ 0xc00017e460 [] [] 0xc00012e958 0xc000125c50 0xc000038748 {{0 0} 0 0 0 0}}
2022/08/01 03:30:29 Health is good
2022/08/01 03:30:29 Started workflow WorkflowID hello_world_workflowID RunID 03cfd328-77c1-4d7a-9d5e-86d82e44c034
2022/08/01 03:30:29 Workflow result: Hello Temporal!

huangapple
  • 本文由 发表于 2022年8月1日 10:51:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/73188412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定