Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a86c22fba8 |
+35
-112
@@ -10,7 +10,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.temporal.io/api/common/v1"
|
"go.temporal.io/api/common/v1"
|
||||||
query "go.temporal.io/api/query/v1"
|
|
||||||
"go.temporal.io/api/taskqueue/v1"
|
"go.temporal.io/api/taskqueue/v1"
|
||||||
update "go.temporal.io/api/update/v1"
|
update "go.temporal.io/api/update/v1"
|
||||||
"go.temporal.io/api/workflowservice/v1"
|
"go.temporal.io/api/workflowservice/v1"
|
||||||
@@ -38,6 +37,8 @@ type ResponsePayload struct {
|
|||||||
type Handler struct {
|
type Handler struct {
|
||||||
hostPort string
|
hostPort string
|
||||||
grpcClient *GRPCClient
|
grpcClient *GRPCClient
|
||||||
|
ops *WorkflowGRPCImpl
|
||||||
|
searchOps *SearchAttributesGRPCImpl
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHandler creates a new Temporal HTTP handler
|
// NewHandler creates a new Temporal HTTP handler
|
||||||
@@ -51,10 +52,15 @@ func NewHandler(hostPort string) *Handler {
|
|||||||
log.Printf("WARNING: Failed to connect to Temporal at %s: %v", hostPort, err)
|
log.Printf("WARNING: Failed to connect to Temporal at %s: %v", hostPort, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Handler{
|
h := &Handler{
|
||||||
hostPort: hostPort,
|
hostPort: hostPort,
|
||||||
grpcClient: grpcClient,
|
grpcClient: grpcClient,
|
||||||
}
|
}
|
||||||
|
if grpcClient != nil {
|
||||||
|
h.ops = NewWorkflowGRPCImpl(grpcClient)
|
||||||
|
h.searchOps = NewSearchAttributesGRPCImpl(grpcClient)
|
||||||
|
}
|
||||||
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
// unavailable returns TEMPORAL_UNAVAILABLE when grpcClient is nil
|
// unavailable returns TEMPORAL_UNAVAILABLE when grpcClient is nil
|
||||||
@@ -206,7 +212,7 @@ func (h *Handler) handleMetrics(w http.ResponseWriter, r *http.Request) {
|
|||||||
// ── Workflow operations ──────────────────────────────────────────────────────
|
// ── Workflow operations ──────────────────────────────────────────────────────
|
||||||
|
|
||||||
func (h *Handler) startWorkflow(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
func (h *Handler) startWorkflow(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
||||||
if h.grpcClient == nil {
|
if h.ops == nil {
|
||||||
return h.unavailable()
|
return h.unavailable()
|
||||||
}
|
}
|
||||||
workflowID := getString(payload, "workflow_id")
|
workflowID := getString(payload, "workflow_id")
|
||||||
@@ -221,31 +227,16 @@ func (h *Handler) startWorkflow(ctx context.Context, namespace string, payload m
|
|||||||
if taskQueue == "" {
|
if taskQueue == "" {
|
||||||
return nil, "INVALID_REQUEST", "task_queue is required"
|
return nil, "INVALID_REQUEST", "task_queue is required"
|
||||||
}
|
}
|
||||||
|
input := getMap(payload, "input")
|
||||||
req := &workflowservice.StartWorkflowExecutionRequest{
|
result, err := h.ops.StartWorkflowExecution(ctx, namespace, workflowID, workflowType, taskQueue, input)
|
||||||
Namespace: namespace,
|
|
||||||
WorkflowId: workflowID,
|
|
||||||
WorkflowType: &common.WorkflowType{Name: workflowType},
|
|
||||||
TaskQueue: &taskqueue.TaskQueue{Name: taskQueue},
|
|
||||||
}
|
|
||||||
if input := getMap(payload, "input"); len(input) > 0 {
|
|
||||||
b, _ := json.Marshal(input)
|
|
||||||
req.Input = &common.Payloads{Payloads: []*common.Payload{{Data: b}}}
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := h.grpcClient.GetWorkflowServiceStub().StartWorkflowExecution(ctx, req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "TEMPORAL_UNAVAILABLE", fmt.Sprintf("failed to start workflow: %v", err)
|
return nil, "TEMPORAL_UNAVAILABLE", fmt.Sprintf("failed to start workflow: %v", err)
|
||||||
}
|
}
|
||||||
return map[string]interface{}{
|
return result, "", ""
|
||||||
"workflow_id": workflowID,
|
|
||||||
"run_id": resp.RunId,
|
|
||||||
"started_at": time.Now(),
|
|
||||||
}, "", ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) describeWorkflow(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
func (h *Handler) describeWorkflow(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
||||||
if h.grpcClient == nil {
|
if h.ops == nil {
|
||||||
return h.unavailable()
|
return h.unavailable()
|
||||||
}
|
}
|
||||||
workflowID := getString(payload, "workflow_id")
|
workflowID := getString(payload, "workflow_id")
|
||||||
@@ -253,25 +244,11 @@ func (h *Handler) describeWorkflow(ctx context.Context, namespace string, payloa
|
|||||||
return nil, "INVALID_REQUEST", "workflow_id is required"
|
return nil, "INVALID_REQUEST", "workflow_id is required"
|
||||||
}
|
}
|
||||||
runID := getString(payload, "run_id")
|
runID := getString(payload, "run_id")
|
||||||
|
result, err := h.ops.DescribeWorkflowExecution(ctx, namespace, workflowID, runID)
|
||||||
resp, err := h.grpcClient.GetWorkflowServiceStub().DescribeWorkflowExecution(ctx,
|
|
||||||
&workflowservice.DescribeWorkflowExecutionRequest{
|
|
||||||
Namespace: namespace,
|
|
||||||
Execution: &common.WorkflowExecution{WorkflowId: workflowID, RunId: runID},
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "WORKFLOW_NOT_FOUND", fmt.Sprintf("failed to describe workflow: %v", err)
|
return nil, "WORKFLOW_NOT_FOUND", fmt.Sprintf("failed to describe workflow: %v", err)
|
||||||
}
|
}
|
||||||
|
return result, "", ""
|
||||||
status := "UNKNOWN"
|
|
||||||
if resp.WorkflowExecutionInfo != nil {
|
|
||||||
status = resp.WorkflowExecutionInfo.Status.String()
|
|
||||||
}
|
|
||||||
return map[string]interface{}{
|
|
||||||
"workflow_id": workflowID,
|
|
||||||
"run_id": runID,
|
|
||||||
"status": status,
|
|
||||||
}, "", ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) listWorkflows(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
func (h *Handler) listWorkflows(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
||||||
@@ -317,7 +294,7 @@ func (h *Handler) listWorkflows(ctx context.Context, namespace string, payload m
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) getWorkflowHistory(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
func (h *Handler) getWorkflowHistory(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
||||||
if h.grpcClient == nil {
|
if h.ops == nil {
|
||||||
return h.unavailable()
|
return h.unavailable()
|
||||||
}
|
}
|
||||||
workflowID := getString(payload, "workflow_id")
|
workflowID := getString(payload, "workflow_id")
|
||||||
@@ -325,79 +302,48 @@ func (h *Handler) getWorkflowHistory(ctx context.Context, namespace string, payl
|
|||||||
return nil, "INVALID_REQUEST", "workflow_id is required"
|
return nil, "INVALID_REQUEST", "workflow_id is required"
|
||||||
}
|
}
|
||||||
runID := getString(payload, "run_id")
|
runID := getString(payload, "run_id")
|
||||||
|
result, err := h.ops.GetWorkflowExecutionHistory(ctx, namespace, workflowID, runID)
|
||||||
resp, err := h.grpcClient.GetWorkflowServiceStub().GetWorkflowExecutionHistory(ctx,
|
|
||||||
&workflowservice.GetWorkflowExecutionHistoryRequest{
|
|
||||||
Namespace: namespace,
|
|
||||||
Execution: &common.WorkflowExecution{WorkflowId: workflowID, RunId: runID},
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "TEMPORAL_UNAVAILABLE", fmt.Sprintf("failed to get history: %v", err)
|
return nil, "TEMPORAL_UNAVAILABLE", fmt.Sprintf("failed to get history: %v", err)
|
||||||
}
|
}
|
||||||
|
return result, "", ""
|
||||||
events := make([]interface{}, 0, len(resp.History.GetEvents()))
|
|
||||||
for _, e := range resp.History.GetEvents() {
|
|
||||||
events = append(events, map[string]interface{}{
|
|
||||||
"event_id": e.EventId,
|
|
||||||
"event_type": e.EventType.String(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return map[string]interface{}{
|
|
||||||
"events": events,
|
|
||||||
"next_page_token": string(resp.NextPageToken),
|
|
||||||
}, "", ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) terminateWorkflow(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
func (h *Handler) terminateWorkflow(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
||||||
if h.grpcClient == nil {
|
if h.ops == nil {
|
||||||
return h.unavailable()
|
return h.unavailable()
|
||||||
}
|
}
|
||||||
workflowID := getString(payload, "workflow_id")
|
workflowID := getString(payload, "workflow_id")
|
||||||
if workflowID == "" {
|
if workflowID == "" {
|
||||||
return nil, "INVALID_REQUEST", "workflow_id is required"
|
return nil, "INVALID_REQUEST", "workflow_id is required"
|
||||||
}
|
}
|
||||||
|
runID := getString(payload, "run_id")
|
||||||
reason := getString(payload, "reason")
|
reason := getString(payload, "reason")
|
||||||
|
result, err := h.ops.TerminateWorkflowExecution(ctx, namespace, workflowID, runID, reason)
|
||||||
_, err := h.grpcClient.GetWorkflowServiceStub().TerminateWorkflowExecution(ctx,
|
|
||||||
&workflowservice.TerminateWorkflowExecutionRequest{
|
|
||||||
Namespace: namespace,
|
|
||||||
WorkflowExecution: &common.WorkflowExecution{WorkflowId: workflowID},
|
|
||||||
Reason: reason,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "TEMPORAL_UNAVAILABLE", fmt.Sprintf("failed to terminate workflow: %v", err)
|
return nil, "TEMPORAL_UNAVAILABLE", fmt.Sprintf("failed to terminate workflow: %v", err)
|
||||||
}
|
}
|
||||||
return map[string]interface{}{
|
return result, "", ""
|
||||||
"workflow_id": workflowID,
|
|
||||||
"terminated_at": time.Now(),
|
|
||||||
}, "", ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) cancelWorkflow(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
func (h *Handler) cancelWorkflow(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
||||||
if h.grpcClient == nil {
|
if h.ops == nil {
|
||||||
return h.unavailable()
|
return h.unavailable()
|
||||||
}
|
}
|
||||||
workflowID := getString(payload, "workflow_id")
|
workflowID := getString(payload, "workflow_id")
|
||||||
if workflowID == "" {
|
if workflowID == "" {
|
||||||
return nil, "INVALID_REQUEST", "workflow_id is required"
|
return nil, "INVALID_REQUEST", "workflow_id is required"
|
||||||
}
|
}
|
||||||
|
runID := getString(payload, "run_id")
|
||||||
_, err := h.grpcClient.GetWorkflowServiceStub().RequestCancelWorkflowExecution(ctx,
|
result, err := h.ops.CancelWorkflowExecution(ctx, namespace, workflowID, runID)
|
||||||
&workflowservice.RequestCancelWorkflowExecutionRequest{
|
|
||||||
Namespace: namespace,
|
|
||||||
WorkflowExecution: &common.WorkflowExecution{WorkflowId: workflowID},
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "TEMPORAL_UNAVAILABLE", fmt.Sprintf("failed to cancel workflow: %v", err)
|
return nil, "TEMPORAL_UNAVAILABLE", fmt.Sprintf("failed to cancel workflow: %v", err)
|
||||||
}
|
}
|
||||||
return map[string]interface{}{
|
return result, "", ""
|
||||||
"workflow_id": workflowID,
|
|
||||||
"status": "canceling",
|
|
||||||
}, "", ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) signalWorkflow(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
func (h *Handler) signalWorkflow(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
||||||
if h.grpcClient == nil {
|
if h.ops == nil {
|
||||||
return h.unavailable()
|
return h.unavailable()
|
||||||
}
|
}
|
||||||
workflowID := getString(payload, "workflow_id")
|
workflowID := getString(payload, "workflow_id")
|
||||||
@@ -408,30 +354,17 @@ func (h *Handler) signalWorkflow(ctx context.Context, namespace string, payload
|
|||||||
if signalName == "" {
|
if signalName == "" {
|
||||||
return nil, "INVALID_REQUEST", "signal_name is required"
|
return nil, "INVALID_REQUEST", "signal_name is required"
|
||||||
}
|
}
|
||||||
|
runID := getString(payload, "run_id")
|
||||||
req := &workflowservice.SignalWorkflowExecutionRequest{
|
signalData := getMap(payload, "signal_data")
|
||||||
Namespace: namespace,
|
result, err := h.ops.SignalWorkflowExecution(ctx, namespace, workflowID, runID, signalName, signalData)
|
||||||
WorkflowExecution: &common.WorkflowExecution{WorkflowId: workflowID},
|
|
||||||
SignalName: signalName,
|
|
||||||
}
|
|
||||||
if data := getMap(payload, "signal_data"); len(data) > 0 {
|
|
||||||
b, _ := json.Marshal(data)
|
|
||||||
req.Input = &common.Payloads{Payloads: []*common.Payload{{Data: b}}}
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := h.grpcClient.GetWorkflowServiceStub().SignalWorkflowExecution(ctx, req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "TEMPORAL_UNAVAILABLE", fmt.Sprintf("failed to signal workflow: %v", err)
|
return nil, "TEMPORAL_UNAVAILABLE", fmt.Sprintf("failed to signal workflow: %v", err)
|
||||||
}
|
}
|
||||||
return map[string]interface{}{
|
return result, "", ""
|
||||||
"workflow_id": workflowID,
|
|
||||||
"signal_name": signalName,
|
|
||||||
"signaled_at": time.Now(),
|
|
||||||
}, "", ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) queryWorkflow(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
func (h *Handler) queryWorkflow(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
||||||
if h.grpcClient == nil {
|
if h.ops == nil {
|
||||||
return h.unavailable()
|
return h.unavailable()
|
||||||
}
|
}
|
||||||
workflowID := getString(payload, "workflow_id")
|
workflowID := getString(payload, "workflow_id")
|
||||||
@@ -442,22 +375,12 @@ func (h *Handler) queryWorkflow(ctx context.Context, namespace string, payload m
|
|||||||
if queryType == "" {
|
if queryType == "" {
|
||||||
return nil, "INVALID_REQUEST", "query_type is required"
|
return nil, "INVALID_REQUEST", "query_type is required"
|
||||||
}
|
}
|
||||||
|
runID := getString(payload, "run_id")
|
||||||
resp, err := h.grpcClient.GetWorkflowServiceStub().QueryWorkflow(ctx,
|
result, err := h.ops.QueryWorkflowExecution(ctx, namespace, workflowID, runID, queryType)
|
||||||
&workflowservice.QueryWorkflowRequest{
|
|
||||||
Namespace: namespace,
|
|
||||||
Execution: &common.WorkflowExecution{WorkflowId: workflowID},
|
|
||||||
Query: &query.WorkflowQuery{QueryType: queryType},
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "TEMPORAL_UNAVAILABLE", fmt.Sprintf("failed to query workflow: %v", err)
|
return nil, "TEMPORAL_UNAVAILABLE", fmt.Sprintf("failed to query workflow: %v", err)
|
||||||
}
|
}
|
||||||
|
return result, "", ""
|
||||||
var result interface{}
|
|
||||||
if resp.QueryResult != nil && len(resp.QueryResult.Payloads) > 0 {
|
|
||||||
json.Unmarshal(resp.QueryResult.Payloads[0].Data, &result)
|
|
||||||
}
|
|
||||||
return map[string]interface{}{"query_result": result}, "", ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) resetWorkflow(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
func (h *Handler) resetWorkflow(ctx context.Context, namespace string, payload map[string]interface{}) (interface{}, string, string) {
|
||||||
|
|||||||
@@ -1,95 +0,0 @@
|
|||||||
// Package temporal provides operation wrappers for Temporal operations
|
|
||||||
package temporal
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// OperationHandler handles Temporal operations
|
|
||||||
type OperationHandler struct {
|
|
||||||
grpc *GRPCClient
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewOperationHandler creates a new operation handler
|
|
||||||
func NewOperationHandler(grpcClient *GRPCClient) *OperationHandler {
|
|
||||||
return &OperationHandler{
|
|
||||||
grpc: grpcClient,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// StartWorkflowExecution starts a new workflow execution
|
|
||||||
func (oh *OperationHandler) StartWorkflowExecution(ctx context.Context, namespace, workflowID, workflowType, taskQueue string, input map[string]interface{}) (map[string]interface{}, error) {
|
|
||||||
// TODO: Implement gRPC call to Temporal
|
|
||||||
// This is a placeholder for actual implementation
|
|
||||||
if oh.grpc == nil {
|
|
||||||
return nil, fmt.Errorf("gRPC client not initialized")
|
|
||||||
}
|
|
||||||
|
|
||||||
return map[string]interface{}{
|
|
||||||
"workflow_id": workflowID,
|
|
||||||
"run_id": fmt.Sprintf("run_%d", time.Now().UnixNano()),
|
|
||||||
"start_time": time.Now(),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DescribeWorkflowExecution gets workflow details
|
|
||||||
func (oh *OperationHandler) DescribeWorkflowExecution(ctx context.Context, namespace, workflowID, runID string) (map[string]interface{}, error) {
|
|
||||||
// TODO: Implement gRPC call to Temporal
|
|
||||||
return map[string]interface{}{
|
|
||||||
"workflow_id": workflowID,
|
|
||||||
"run_id": runID,
|
|
||||||
"status": "RUNNING",
|
|
||||||
"start_time": time.Now(),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TerminateWorkflowExecution terminates a workflow
|
|
||||||
func (oh *OperationHandler) TerminateWorkflowExecution(ctx context.Context, namespace, workflowID, runID, reason string) (map[string]interface{}, error) {
|
|
||||||
// TODO: Implement gRPC call to Temporal
|
|
||||||
return map[string]interface{}{
|
|
||||||
"workflow_id": workflowID,
|
|
||||||
"run_id": runID,
|
|
||||||
"terminated_at": time.Now(),
|
|
||||||
"reason": reason,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CancelWorkflowExecution cancels a workflow
|
|
||||||
func (oh *OperationHandler) CancelWorkflowExecution(ctx context.Context, namespace, workflowID, runID string) (map[string]interface{}, error) {
|
|
||||||
// TODO: Implement gRPC call to Temporal
|
|
||||||
return map[string]interface{}{
|
|
||||||
"workflow_id": workflowID,
|
|
||||||
"status": "canceling",
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SignalWorkflowExecution sends a signal to a workflow
|
|
||||||
func (oh *OperationHandler) SignalWorkflowExecution(ctx context.Context, namespace, workflowID, runID, signalName string, input map[string]interface{}) (map[string]interface{}, error) {
|
|
||||||
// TODO: Implement gRPC call to Temporal
|
|
||||||
return map[string]interface{}{
|
|
||||||
"workflow_id": workflowID,
|
|
||||||
"signal_name": signalName,
|
|
||||||
"signaled_at": time.Now(),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// QueryWorkflowExecution queries a workflow
|
|
||||||
func (oh *OperationHandler) QueryWorkflowExecution(ctx context.Context, namespace, workflowID, runID, queryType string) (map[string]interface{}, error) {
|
|
||||||
// TODO: Implement gRPC call to Temporal
|
|
||||||
return map[string]interface{}{
|
|
||||||
"workflow_id": workflowID,
|
|
||||||
"query_type": queryType,
|
|
||||||
"query_result": map[string]interface{}{},
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListWorkflowExecutions lists workflows
|
|
||||||
func (oh *OperationHandler) ListWorkflowExecutions(ctx context.Context, namespace string, pageSize int32) (map[string]interface{}, error) {
|
|
||||||
// TODO: Implement gRPC call to Temporal
|
|
||||||
return map[string]interface{}{
|
|
||||||
"executions": []interface{}{},
|
|
||||||
"next_page_token": "",
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user