From b894e477622cc8a8504bc6004824020bb91d1dd2 Mon Sep 17 00:00:00 2001 From: Zibbp Date: Sat, 23 Dec 2023 20:35:16 +0000 Subject: [PATCH] fix(workflows): start workflow by workflow name --- internal/transport/http/workflow.go | 6 ++---- internal/workflows/workflows.go | 30 ++++++++++++++++------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/internal/transport/http/workflow.go b/internal/transport/http/workflow.go index 1b3f6c3f..1fa0fb67 100644 --- a/internal/transport/http/workflow.go +++ b/internal/transport/http/workflow.go @@ -72,14 +72,12 @@ func (h *Handler) StartWorkflow(c echo.Context) error { return echo.NewHTTPError(http.StatusBadRequest, err.Error()) } - workflowId, err := workflows.StartWorkflow(c.Request().Context(), request.WorkflowName) + startWorkflowResponse, err := workflows.StartWorkflow(c.Request().Context(), request.WorkflowName) if err != nil { return err } - return c.JSON(200, map[string]string{ - "workflow_id": workflowId, - }) + return c.JSON(200, startWorkflowResponse) } func (h *Handler) RestartArchiveWorkflow(c echo.Context) error { diff --git a/internal/workflows/workflows.go b/internal/workflows/workflows.go index 86f5038d..618cc116 100644 --- a/internal/workflows/workflows.go +++ b/internal/workflows/workflows.go @@ -8,24 +8,28 @@ import ( "go.temporal.io/sdk/client" ) -func StartWorkflow(ctx context.Context, workflowName string) (string, error) { +type StartWorkflowResponse struct { + WorkflowId string `json:"workflow_id"` + RunId string `json:"run_id"` +} + +func StartWorkflow(ctx context.Context, workflowName string) (StartWorkflowResponse, error) { // TODO: develop a better way to do this - wfOptions := client.StartWorkflowOptions{ - ID: workflowName, + var startWorkflowResponse StartWorkflowResponse + + workflowOptions := client.StartWorkflowOptions{ TaskQueue: "archive", } - switch workflowName { - case "save_chapters_for_twitch_videos": - we, err := temporal.GetTemporalClient().Client.ExecuteWorkflow(ctx, wfOptions, SaveTwitchVideoChapters) - if err != nil { - log.Error().Err(err).Msg("failed to start workflow") - return "", err - } - - return we.GetID(), nil + we, err := temporal.GetTemporalClient().Client.ExecuteWorkflow(ctx, workflowOptions, workflowName) + if err != nil { + log.Error().Err(err).Msg("failed to start workflow") + return startWorkflowResponse, err } - return "", nil + startWorkflowResponse.WorkflowId = we.GetID() + startWorkflowResponse.RunId = we.GetRunID() + + return startWorkflowResponse, nil }