diff --git a/.gitignore b/.gitignore index f4d7df4..95b3d9b 100644 --- a/.gitignore +++ b/.gitignore @@ -180,4 +180,5 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. .idea/ -*/gen \ No newline at end of file +*pb2.py* +*pb.go \ No newline at end of file diff --git a/activities/activities.go b/activities/activities.go new file mode 100644 index 0000000..5d3bd02 --- /dev/null +++ b/activities/activities.go @@ -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 +} diff --git a/server/main.go b/client/main.go similarity index 88% rename from server/main.go rename to client/main.go index ed56d50..cb4a4d7 100644 --- a/server/main.go +++ b/client/main.go @@ -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) diff --git a/proto/common_test.proto b/proto/common_test.proto index 716e997..0678267 100644 --- a/proto/common_test.proto +++ b/proto/common_test.proto @@ -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 即可。 diff --git a/server/activity/activity.go b/server/activity/activity.go deleted file mode 100644 index a9f17a9..0000000 --- a/server/activity/activity.go +++ /dev/null @@ -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 代理 -} diff --git a/workers/go/main.go b/workers/go/main.go new file mode 100644 index 0000000..e1512b8 --- /dev/null +++ b/workers/go/main.go @@ -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) + } +} diff --git a/worker/activities.py b/workers/python/activities.py similarity index 97% rename from worker/activities.py rename to workers/python/activities.py index 0715899..3e55b71 100644 --- a/worker/activities.py +++ b/workers/python/activities.py @@ -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}") diff --git a/worker/api_tests.py b/workers/python/api_tests.py similarity index 100% rename from worker/api_tests.py rename to workers/python/api_tests.py diff --git a/worker/gen/__init__.py b/workers/python/gen/__init__.py similarity index 100% rename from worker/gen/__init__.py rename to workers/python/gen/__init__.py diff --git a/worker/main.py b/workers/python/main.py similarity index 92% rename from worker/main.py rename to workers/python/main.py index d3178a6..e0209f0 100644 --- a/worker/main.py +++ b/workers/python/main.py @@ -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() diff --git a/worker/requirements.txt b/workers/python/requirements.txt similarity index 100% rename from worker/requirements.txt rename to workers/python/requirements.txt diff --git a/worker/ui_tests.py b/workers/python/ui_tests.py similarity index 100% rename from worker/ui_tests.py rename to workers/python/ui_tests.py diff --git a/worker/utils.py b/workers/python/utils.py similarity index 100% rename from worker/utils.py rename to workers/python/utils.py diff --git a/server/workflow/workflow.go b/workflows/workflow.go similarity index 91% rename from server/workflow/workflow.go rename to workflows/workflow.go index 76c1a4e..20ddcdf 100644 --- a/server/workflow/workflow.go +++ b/workflows/workflow.go @@ -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