Creates: - cmd/worker/main.go: Worker that registers workflows and activities - cmd/test-workflow/main.go: Test client to trigger workflows Adds go.temporal.io/sdk dependency to go.mod.
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package workflow
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"go.temporal.io/sdk/workflow"
|
|
)
|
|
|
|
// HelloWorldWorkflow is a simple hello world workflow
|
|
func HelloWorldWorkflow(ctx workflow.Context, name string) (string, error) {
|
|
opts := workflow.ActivityOptions{
|
|
StartToCloseTimeout: time.Minute,
|
|
}
|
|
ctx = workflow.WithActivityOptions(ctx, opts)
|
|
|
|
var result string
|
|
if err := workflow.ExecuteActivity(ctx, GreetActivity, name).Get(ctx, &result); err != nil {
|
|
return "", err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// GreetActivity greets someone
|
|
func GreetActivity(ctx context.Context, name string) (string, error) {
|
|
return fmt.Sprintf("Hello, %s!", name), nil
|
|
}
|
|
|
|
// ValidateOrderActivity validates an order
|
|
func ValidateOrderActivity(ctx context.Context, orderID string) (bool, error) {
|
|
// Simulate validation
|
|
if orderID == "" {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// ProcessPaymentActivity processes payment
|
|
func ProcessPaymentActivity(ctx context.Context, orderID string) (string, error) {
|
|
// Simulate payment processing
|
|
return fmt.Sprintf("payment-%s", orderID[:min(len(orderID), 3)]), nil
|
|
}
|
|
|
|
// NotifyCustomerActivity sends notification
|
|
func NotifyCustomerActivity(ctx context.Context, orderID string) (string, error) {
|
|
// Simulate notification
|
|
return fmt.Sprintf("notified for order %s", orderID), nil
|
|
}
|
|
|
|
func min(a, b int) int {
|
|
if a < b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|