53 lines
1.7 KiB
Go
53 lines
1.7 KiB
Go
package workflows
|
|
|
|
// 定义 Temporal Workflow
|
|
import (
|
|
"beacon/server/activities"
|
|
"fmt"
|
|
"go.temporal.io/sdk/temporal"
|
|
"go.temporal.io/sdk/workflow"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// TestRunWorkflow 定义了整个测试执行的工作流
|
|
func TestRunWorkflow(ctx workflow.Context, data string) (string, error) {
|
|
logger := workflow.GetLogger(ctx)
|
|
retryPolicy := &temporal.RetryPolicy{
|
|
InitialInterval: time.Second, // First retry after 1 second
|
|
BackoffCoefficient: 2.0, // Double the wait time on each retry (1s → 2s → 4s → 8s, etc.)
|
|
MaximumInterval: 100 * time.Second, // Cap wait time at 100 seconds
|
|
MaximumAttempts: 50, // Retry up to 5 times before giving up
|
|
}
|
|
|
|
// Step 1: Add a prefix(Python)
|
|
pythonCtx := workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
|
|
TaskQueue: "python-task-queue",
|
|
StartToCloseTimeout: time.Minute,
|
|
RetryPolicy: retryPolicy,
|
|
})
|
|
|
|
var prefixed string
|
|
if err := workflow.ExecuteActivity(pythonCtx, "PythonAddRandomPrefixActivity", data).Get(pythonCtx, &prefixed); err != nil {
|
|
return "", fmt.Errorf("failed to add prefix: %w", err)
|
|
}
|
|
|
|
goCtx := workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
|
|
StartToCloseTimeout: time.Minute, //activity must complete within 1 minute
|
|
RetryPolicy: retryPolicy,
|
|
})
|
|
|
|
var suffixed string
|
|
err := workflow.ExecuteActivity(goCtx, activities.AddSuffixActivity, prefixed).Get(goCtx, &suffixed)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to add suffix: %w", err)
|
|
}
|
|
|
|
var processed string
|
|
// Demo Activity 3: Simulate uppercasing the data.
|
|
logger.Info("Demo Activity 3: Pretending to uppercase the data:", data)
|
|
processed = strings.ToUpper(suffixed)
|
|
|
|
return processed, nil
|
|
}
|