Files
homelab-frontend/internal/temporal/grpc_client.go
T

75 lines
2.1 KiB
Go
Raw Normal View History

// Package temporal provides gRPC client for Temporal server operations
package temporal
import (
"context"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"go.temporal.io/api/workflowservice/v1"
"go.temporal.io/api/operatorservice/v1"
)
// GRPCClient wraps Temporal gRPC clients
type GRPCClient struct {
conn *grpc.ClientConn
workflowServiceStub workflowservice.WorkflowServiceClient
operatorServiceStub operatorservice.OperatorServiceClient
}
// NewGRPCClient creates a new Temporal gRPC client
func NewGRPCClient(hostPort string) (*GRPCClient, error) {
if hostPort == "" {
hostPort = "localhost:7233"
}
// Create insecure connection (for development)
// In production, use credentials.NewTLS() for secure connection
conn, err := grpc.Dial(
hostPort,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(20*1024*1024), // 20MB max message size
),
)
if err != nil {
return nil, fmt.Errorf("failed to connect to Temporal server at %s: %w", hostPort, err)
}
return &GRPCClient{
conn: conn,
workflowServiceStub: workflowservice.NewWorkflowServiceClient(conn),
operatorServiceStub: operatorservice.NewOperatorServiceClient(conn),
}, nil
}
// Close closes the gRPC connection
func (c *GRPCClient) Close() error {
if c.conn != nil {
return c.conn.Close()
}
return nil
}
// HealthCheck checks if Temporal server is responsive
func (c *GRPCClient) HealthCheck(ctx context.Context) error {
// Use ListClusters as a health check since it's a simple operation
_, err := c.operatorServiceStub.ListClusters(ctx, &operatorservice.ListClustersRequest{})
if err != nil {
return fmt.Errorf("temporal server health check failed: %w", err)
}
return nil
}
// GetWorkflowServiceStub returns the WorkflowService client
func (c *GRPCClient) GetWorkflowServiceStub() workflowservice.WorkflowServiceClient {
return c.workflowServiceStub
}
// GetOperatorServiceStub returns the OperatorService client
func (c *GRPCClient) GetOperatorServiceStub() operatorservice.OperatorServiceClient {
return c.operatorServiceStub
}