-
Notifications
You must be signed in to change notification settings - Fork 145
/
cancel.go
77 lines (63 loc) · 2.27 KB
/
cancel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package workflows
import (
"fmt"
"sync"
"github.com/labstack/echo/v4"
"github.com/google/uuid"
"github.com/hashicorp/go-multierror"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/gen"
"github.com/hatchet-dev/hatchet/internal/msgqueue"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/db"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/sqlchelpers"
"github.com/hatchet-dev/hatchet/internal/services/shared/tasktypes"
)
func (t *WorkflowService) WorkflowRunCancel(ctx echo.Context, request gen.WorkflowRunCancelRequestObject) (gen.WorkflowRunCancelResponseObject, error) {
tenant := ctx.Get("tenant").(*db.TenantModel)
runIds := request.Body.WorkflowRunIds
var wg sync.WaitGroup
var mu sync.Mutex
var cancelledWorkflowRunIds []uuid.UUID
var returnErr error
for _, runId := range runIds {
wg.Add(1)
go func(runId uuid.UUID) {
defer wg.Done()
// Lookup step runs for the workflow run
runIdStr := runId.String()
jobRun, err := t.config.EngineRepository.JobRun().ListJobRunsForWorkflowRun(ctx.Request().Context(), tenant.ID, runIdStr)
if err != nil {
returnErr = multierror.Append(err, fmt.Errorf("failed to list job runs for workflow run %s", runIdStr))
return
}
for _, jobRun := range jobRun {
// If the step run is not in a final state, send a task to the taskqueue to cancel it
var reason = "CANCELLED_BY_USER"
// send a task to the taskqueue
jobRunId := sqlchelpers.UUIDToStr(jobRun.ID)
err = t.config.MessageQueue.AddMessage(
ctx.Request().Context(),
msgqueue.JOB_PROCESSING_QUEUE,
tasktypes.JobRunCancelledToTask(tenant.ID, jobRunId, &reason),
)
if err != nil {
returnErr = multierror.Append(err, fmt.Errorf("failed to send cancel task for job run %s", jobRunId))
continue
}
}
// Add the canceled workflow run ID to the slice
mu.Lock()
cancelledWorkflowRunIds = append(cancelledWorkflowRunIds, runId)
mu.Unlock()
}(runId)
}
wg.Wait()
if returnErr != nil {
return nil, returnErr
}
// Create a new instance of gen.WorkflowRunCancel200JSONResponse and assign canceledWorkflowRunUUIDs to its WorkflowRunIds field
response := gen.WorkflowRunCancel200JSONResponse{
WorkflowRunIds: &cancelledWorkflowRunIds,
}
// Return the response
return response, nil
}