go与python的最佳实践
This commit is contained in:
parent
d78dc1fb95
commit
e9336e1af4
3
.gitignore
vendored
3
.gitignore
vendored
@ -180,4 +180,5 @@ cython_debug/
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
.idea/
|
||||
|
||||
*/gen
|
||||
*pb2.py*
|
||||
*pb.go
|
19
activities/activities.go
Normal file
19
activities/activities.go
Normal file
@ -0,0 +1,19 @@
|
||||
package activities
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
// AddSuffixActivity appends a fixed suffix to the input data.
|
||||
func AddSuffixActivity(ctx context.Context, data string) (string, error) {
|
||||
suffixes := []string{
|
||||
"-one", "-two", "-three", "-four", "-five",
|
||||
"-six", "-seven", "-eight", "-nine", "-ten",
|
||||
}
|
||||
suffix := suffixes[rand.Intn(len(suffixes))]
|
||||
result := fmt.Sprintf("%s%s", data, suffix)
|
||||
fmt.Println("Go Activity: Modified data to:", result)
|
||||
return result, nil
|
||||
}
|
@ -2,8 +2,8 @@ package main
|
||||
|
||||
// Go 服务端入口,触发 Workflow
|
||||
import (
|
||||
"beacon/server/gen/pb"
|
||||
"beacon/server/workflow"
|
||||
"beacon/workflows"
|
||||
"beacon/workflows/gen/pb"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
@ -39,9 +39,9 @@ func main() {
|
||||
}
|
||||
|
||||
fmt.Printf("Starting TestRunWorkflow for run ID: %s\n", runID)
|
||||
we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, workflow.TestRunWorkflow, testInput)
|
||||
we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, workflows.TestRunWorkflow, testInput)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to execute workflow: %v", err)
|
||||
log.Fatalf("Unable to execute workflows: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Workflow started. Workflow ID: %s, Run ID: %s\n", we.GetID(), we.GetRunID())
|
||||
@ -50,7 +50,7 @@ func main() {
|
||||
var result pb.TestRunOutput
|
||||
err = we.Get(context.Background(), &result)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to get workflow result: %v", err)
|
||||
log.Fatalf("Unable to get workflows result: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Workflow finished. Overall Success: %t, Message: %s\n", result.OverallSuccess, result.CompletionMessage)
|
@ -2,7 +2,7 @@ syntax = "proto3";
|
||||
|
||||
package test_pb;
|
||||
|
||||
option go_package = "Beacon/server/gen/pb"; // 替换 your_module_path
|
||||
option go_package = "Beacon/client/gen/pb"; // 替换 your_module_path
|
||||
// Python 生成时,会在 proto_gen 目录下直接生成 common_test_pb2.py,
|
||||
// Python 代码中 import proto_gen.common_test_pb2 即可。
|
||||
|
||||
|
@ -1,23 +0,0 @@
|
||||
package activity
|
||||
|
||||
// 定义 Temporal Activity 接口
|
||||
import (
|
||||
"beacon/server/gen/pb" // 替换为你的模块路径
|
||||
"context"
|
||||
)
|
||||
|
||||
// 定义活动接口,Python Worker 将实现这些接口
|
||||
// Temporal Go SDK 会在编译时通过 go-temporal 插件自动生成这些接口的实现桩
|
||||
// 使得你可以直接调用这些接口,而实际执行在 Python Worker 中。
|
||||
|
||||
// RunApiTest 是执行接口测试的活动
|
||||
func RunApiTest(ctx context.Context, req *pb.ApiTestRequest) (*pb.ApiTestResult, error) {
|
||||
// 实际调用会被转发到 Python Worker
|
||||
return nil, nil // Go 侧不需要实现,由 Temporal SDK 代理
|
||||
}
|
||||
|
||||
// RunUiTest 是执行 UI 测试的活动
|
||||
func RunUiTest(ctx context.Context, req *pb.UiTestRequest) (*pb.UiTestResult, error) {
|
||||
// 实际调用会被转发到 Python Worker
|
||||
return nil, nil // Go 侧不需要实现,由 Temporal SDK 代理
|
||||
}
|
36
workers/go/main.go
Normal file
36
workers/go/main.go
Normal file
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"beacon/activities"
|
||||
"beacon/workflows"
|
||||
"go.temporal.io/sdk/client"
|
||||
"go.temporal.io/sdk/worker"
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create a Temporal client with the default options.
|
||||
c, err := client.Dial(client.Options{HostPort: "temporal.newai.day:17233"})
|
||||
if err != nil {
|
||||
log.Fatalln("Unable to create Temporal client", err)
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
taskQueue := "test-task-queue"
|
||||
|
||||
// Create a Worker that listens on the specified Task Queue.
|
||||
w := worker.New(c, taskQueue, worker.Options{})
|
||||
|
||||
// Register the Workflow and the real Go suffix Activity with the Worker.
|
||||
w.RegisterWorkflow(workflows.TestRunWorkflow)
|
||||
w.RegisterActivity(activities.AddSuffixActivity)
|
||||
//(for later) w.RegisterActivity(activities.AddSuffixActivity)
|
||||
|
||||
// Note: The Python and ts activities will be handled by the Python/ts worker and is not registered here.
|
||||
|
||||
// Start the Worker. This call blocks until the Worker is interrupted.
|
||||
err = w.Run(worker.InterruptCh())
|
||||
if err != nil {
|
||||
log.Fatalln("Unable to start Worker", err)
|
||||
}
|
||||
}
|
@ -15,7 +15,7 @@ from utils import upload_file_to_s3, scalar_map_to_dict
|
||||
|
||||
|
||||
class TestActivities:
|
||||
@activity.defn
|
||||
@activity.defn(name="run_api_test")
|
||||
async def run_api_test(self,req: pb.ApiTestRequest) -> pb.ApiTestResult:
|
||||
"""执行API测试的Temporal Activity实现"""
|
||||
activity.logger.info(f"Received API Test Request: {req.test_case_id}")
|
||||
@ -46,7 +46,7 @@ class TestActivities:
|
||||
return result
|
||||
|
||||
|
||||
@activity.defn
|
||||
@activity.defn(name="run_ui_test")
|
||||
async def run_ui_test(self,req: pb.UiTestRequest) -> pb.UiTestResult:
|
||||
"""执行UI测试的Temporal Activity实现"""
|
||||
activity.logger.info(f"Received UI Test Request: {req.test_case_id}")
|
@ -20,7 +20,7 @@ async def main():
|
||||
worker = Worker(
|
||||
client,
|
||||
task_queue="test-task-queue", # 保持与 Go Client 一致
|
||||
activities=[activities.run_ui_test, activities.run_api_test]
|
||||
activities=[activities.run_api_test,activities.run_ui_test]
|
||||
)
|
||||
print("Starting Python Temporal Worker...")
|
||||
await worker.run()
|
@ -1,9 +1,8 @@
|
||||
package workflow
|
||||
package workflows
|
||||
|
||||
// 定义 Temporal Workflow
|
||||
import (
|
||||
"beacon/server/activity"
|
||||
"beacon/server/gen/pb"
|
||||
"beacon/workflows/gen/pb"
|
||||
"fmt"
|
||||
"go.temporal.io/sdk/temporal"
|
||||
"go.temporal.io/sdk/workflow"
|
||||
@ -45,7 +44,7 @@ func TestRunWorkflow(ctx workflow.Context, input *pb.TestRunInput) (*pb.TestRunO
|
||||
ExpectedStatusCode: 200,
|
||||
}
|
||||
var apiRes pb.ApiTestResult
|
||||
err := workflow.ExecuteActivity(ctx, activity.RunApiTest, apiTestInput).Get(ctx, &apiRes)
|
||||
err := workflow.ExecuteActivity(ctx, "run_api_test", apiTestInput).Get(ctx, &apiRes)
|
||||
if err != nil {
|
||||
logger.Error("API test activity failed", "error", err)
|
||||
// 可以选择标记为失败,或者继续执行UI测试
|
||||
@ -66,7 +65,7 @@ func TestRunWorkflow(ctx workflow.Context, input *pb.TestRunInput) (*pb.TestRunO
|
||||
UserData: map[string]string{"user": "test", "pass": "password"},
|
||||
}
|
||||
var uiRes pb.UiTestResult
|
||||
err := workflow.ExecuteActivity(ctx, activity.RunUiTest, uiTestInput).Get(ctx, &uiRes)
|
||||
err := workflow.ExecuteActivity(ctx, "run_ui_test", uiTestInput).Get(ctx, &uiRes)
|
||||
if err != nil {
|
||||
logger.Error("UI test activity failed", "error", err)
|
||||
overallSuccess = false
|
Loading…
Reference in New Issue
Block a user