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

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

temporal go client, Client.CheckHealth() example

问题

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

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

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

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "go.temporal.io/sdk/client"
  6. "github.com/temporalio/samples-go/helloworld"
  7. )
  8. func main() {
  9. log.Println("Starting")
  10. // 客户端是一个重量级对象,每个进程只应创建一次。
  11. c, err := client.Dial(client.Options{})
  12. if err != nil {
  13. log.Fatalln("无法创建客户端", err)
  14. }
  15. log.Println("客户端已创建", c)
  16. // 检查客户端健康状态
  17. checkHealthRequest := client.CheckHealthRequest{}
  18. checkHealthResponse, err := c.CheckHealth(context.Background(), &checkHealthRequest)
  19. if err != nil {
  20. log.Fatalln("无法检查健康状态", err)
  21. }
  22. log.Println("checkHealthResponse", checkHealthResponse)
  23. defer c.Close()
  24. workflowOptions := client.StartWorkflowOptions{
  25. ID: "hello_world_workflowID",
  26. TaskQueue: "hello-world",
  27. }
  28. we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, helloworld.Workflow, "Temporal")
  29. if err != nil {
  30. log.Fatalln("无法执行工作流", err)
  31. }
  32. log.Println("已启动工作流", "WorkflowID", we.GetID(), "RunID", we.GetRunID())
  33. // 同步等待工作流完成。
  34. var result string
  35. err = we.Get(context.Background(), &result)
  36. if err != nil {
  37. log.Fatalln("无法获取工作流结果", err)
  38. }
  39. log.Println("工作流结果:", result)
  40. }

返回结果:

  1. gitpod /workspace/samples-go (main) $ go run helloworld/starter/main_matt.go
  2. 2022/08/01 03:15:38 Starting
  3. 2022/08/01 03:15:38 INFO 未为Temporal客户端配置日志记录器。已创建默认日志记录器。
  4. 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}}
  5. 2022/08/01 03:15:38 checkHealthResponse &{}
  6. 2022/08/01 03:15:38 已启动工作流 WorkflowID hello_world_workflowID RunID 80727872-4df5-4fab-b96a-484276748e36
  7. 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:

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "go.temporal.io/sdk/client"
  6. "github.com/temporalio/samples-go/helloworld"
  7. )
  8. func main() {
  9. log.Println("Starting")
  10. // The client is a heavyweight object that should be created once per process.
  11. c, err := client.Dial(client.Options{})
  12. if err != nil {
  13. log.Fatalln("Unable to create client", err)
  14. }
  15. log.Println("Client created", c)
  16. // Check client health
  17. checkHealthRequest := client.CheckHealthRequest{
  18. }
  19. checkHealthResponse, err := c.CheckHealth(context.Background(), &checkHealthRequest)
  20. if err != nil {
  21. log.Fatalln("Unable to check health", err)
  22. }
  23. log.Println("checkHealthResponse", checkHealthResponse)
  24. defer c.Close()
  25. workflowOptions := client.StartWorkflowOptions{
  26. ID: "hello_world_workflowID",
  27. TaskQueue: "hello-world",
  28. }
  29. we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, helloworld.Workflow, "Temporal")
  30. if err != nil {
  31. log.Fatalln("Unable to execute workflow", err)
  32. }
  33. log.Println("Started workflow", "WorkflowID", we.GetID(), "RunID", we.GetRunID())
  34. // Synchronously wait for the workflow completion.
  35. var result string
  36. err = we.Get(context.Background(), &result)
  37. if err != nil {
  38. log.Fatalln("Unable get workflow result", err)
  39. }
  40. log.Println("Workflow result:", result)
  41. }

Returns:

  1. gitpod /workspace/samples-go (main) $ go run helloworld/starter/main_matt.go
  2. 2022/08/01 03:15:38 Starting
  3. 2022/08/01 03:15:38 INFO No logger configured for temporal client. Created default one.
  4. 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}}
  5. 2022/08/01 03:15:38 checkHealthResponse &{}
  6. 2022/08/01 03:15:38 Started workflow WorkflowID hello_world_workflowID RunID 80727872-4df5-4fab-b96a-484276748e36
  7. 2022/08/01 03:15:38 Workflow result: Hello Temporal!

答案1

得分: 1

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

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "go.temporal.io/sdk/client"
  6. "github.com/temporalio/samples-go/helloworld"
  7. )
  8. func main() {
  9. log.Println("Starting")
  10. // 创建一个客户端对象,这是一个重量级对象,每个进程只需要创建一次
  11. c, err := client.Dial(client.Options{})
  12. if err != nil {
  13. log.Fatalln("无法创建客户端", err)
  14. }
  15. log.Println("客户端已创建", c)
  16. // 检查客户端的健康状态
  17. // 文档中说:“如果检查失败,将返回一个错误”。如果 err == nil,则表示健康状态良好。
  18. if _, err := c.CheckHealth(context.Background(), &client.CheckHealthRequest{}); err != nil {
  19. /* 健康状态不好 */
  20. log.Println("健康状态不好")
  21. } else {
  22. /* 健康状态良好 */
  23. log.Println("健康状态良好")
  24. }
  25. defer c.Close()
  26. workflowOptions := client.StartWorkflowOptions{
  27. ID: "hello_world_workflowID",
  28. TaskQueue: "hello-world",
  29. }
  30. we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, helloworld.Workflow, "Temporal")
  31. if err != nil {
  32. log.Fatalln("无法执行工作流", err)
  33. }
  34. log.Println("已启动工作流", "WorkflowID", we.GetID(), "RunID", we.GetRunID())
  35. // 同步等待工作流完成
  36. var result string
  37. err = we.Get(context.Background(), &result)
  38. if err != nil {
  39. log.Fatalln("无法获取工作流结果", err)
  40. }
  41. log.Println("工作流结果:", result)
  42. }
  1. gitpod /workspace/samples-go (main) $ go run helloworld/starter/main_matt.go
  2. 2022/08/01 03:30:29 Starting
  3. 2022/08/01 03:30:29 INFO No logger configured for temporal client. Created default one.
  4. 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}}
  5. 2022/08/01 03:30:29 健康状态良好
  6. 2022/08/01 03:30:29 已启动工作流 WorkflowID hello_world_workflowID RunID 03cfd328-77c1-4d7a-9d5e-86d82e44c034
  7. 2022/08/01 03:30:29 工作流结果: Hello Temporal!
英文:

Answer based on comments from Cerise Limón above

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "go.temporal.io/sdk/client"
  6. "github.com/temporalio/samples-go/helloworld"
  7. )
  8. func main() {
  9. log.Println("Starting")
  10. // The client is a heavyweight object that should be created once per process.
  11. c, err := client.Dial(client.Options{})
  12. if err != nil {
  13. log.Fatalln("Unable to create client", err)
  14. }
  15. log.Println("Client created", c)
  16. // Check client health
  17. // The documentation says "If the check fails, an error is returned." The health is good if err == nil.
  18. if _, err := c.CheckHealth(context.Background(), &client.CheckHealthRequest{}); err != nil {
  19. /* health is bad */
  20. log.Println("Health is bad")
  21. } else {
  22. /* health is good */
  23. log.Println("Health is good")
  24. }
  25. defer c.Close()
  26. workflowOptions := client.StartWorkflowOptions{
  27. ID: "hello_world_workflowID",
  28. TaskQueue: "hello-world",
  29. }
  30. we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, helloworld.Workflow, "Temporal")
  31. if err != nil {
  32. log.Fatalln("Unable to execute workflow", err)
  33. }
  34. log.Println("Started workflow", "WorkflowID", we.GetID(), "RunID", we.GetRunID())
  35. // Synchronously wait for the workflow completion.
  36. var result string
  37. err = we.Get(context.Background(), &result)
  38. if err != nil {
  39. log.Fatalln("Unable get workflow result", err)
  40. }
  41. log.Println("Workflow result:", result)
  42. }
  1. gitpod /workspace/samples-go (main) $ go run helloworld/starter/main_matt.go
  2. 2022/08/01 03:30:29 Starting
  3. 2022/08/01 03:30:29 INFO No logger configured for temporal client. Created default one.
  4. 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}}
  5. 2022/08/01 03:30:29 Health is good
  6. 2022/08/01 03:30:29 Started workflow WorkflowID hello_world_workflowID RunID 03cfd328-77c1-4d7a-9d5e-86d82e44c034
  7. 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:

确定