Skip to content

Commit

Permalink
feat: cancel in progress (#325)
Browse files Browse the repository at this point in the history
* chore: bad project path

* fix: remove tickerId for replay

* feat: cancel from request

* feat: cancel button in UI

* chore: rm comment

* fix: build error

* chore: reason case

* chore: reason string

* fix: linting

---------

Co-authored-by: gabriel ruttner <gabe@hatchet.run>
  • Loading branch information
grutt and grutt authored Apr 2, 2024
1 parent d731a13 commit 5066547
Show file tree
Hide file tree
Showing 14 changed files with 2,757 additions and 1,861 deletions.
2 changes: 2 additions & 0 deletions api-contracts/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ paths:
$ref: "./paths/step-run/step-run.yaml#/stepRunScoped"
/api/v1/tenants/{tenant}/step-runs/{step-run}/rerun:
$ref: "./paths/step-run/step-run.yaml#/rerunStepRun"
/api/v1/tenants/{tenant}/step-runs/{step-run}/cancel:
$ref: "./paths/step-run/step-run.yaml#/cancelStepRun"
/api/v1/tenants/{tenant}/step-runs/{step-run}/schema:
$ref: "./paths/step-run/step-run.yaml#/getSchema"
/api/v1/tenants/{tenant}/worker:
Expand Down
48 changes: 48 additions & 0 deletions api-contracts/openapi/paths/step-run/step-run.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,54 @@ rerunStepRun:
summary: Rerun step run
tags:
- Step Run

cancelStepRun:
post:
x-resources: ["tenant", "step-run"]
description: Attempts to cancel a step run
operationId: step-run:update:cancel
parameters:
- description: The tenant id
in: path
name: tenant
required: true
schema:
type: string
format: uuid
minLength: 36
maxLength: 36
- description: The step run id
in: path
name: step-run
required: true
schema:
type: string
format: uuid
minLength: 36
maxLength: 36
responses:
"200":
content:
application/json:
schema:
$ref: "../../components/schemas/_index.yaml#/StepRun"
description: Successfully dispatched the cancellation
"400":
content:
application/json:
schema:
$ref: "../../components/schemas/_index.yaml#/APIErrors"
description: A malformed or bad request
"403":
content:
application/json:
schema:
$ref: "../../components/schemas/_index.yaml#/APIErrors"
description: Forbidden
summary: Attempts to cancel a step run
tags:
- Step Run

getSchema:
get:
x-resources: ["tenant", "step-run"]
Expand Down
74 changes: 74 additions & 0 deletions api/v1/server/handlers/step-runs/cancel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package stepruns

import (
"fmt"
"time"

"github.com/labstack/echo/v4"

"github.com/hatchet-dev/hatchet/api/v1/server/oas/apierrors"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/gen"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/transformers"
"github.com/hatchet-dev/hatchet/internal/msgqueue"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/db"
"github.com/hatchet-dev/hatchet/internal/services/shared/tasktypes"
)

func (t *StepRunService) StepRunUpdateCancel(ctx echo.Context, request gen.StepRunUpdateCancelRequestObject) (gen.StepRunUpdateCancelResponseObject, error) {
tenant := ctx.Get("tenant").(*db.TenantModel)
stepRun := ctx.Get("step-run").(*db.StepRunModel)

// check to see if the step run is in a running or pending state
status := stepRun.Status

if status == db.StepRunStatusFailed || status == db.StepRunStatusCancelled || status == db.StepRunStatusSucceeded {
return gen.StepRunUpdateCancel400JSONResponse(
apierrors.NewAPIErrors("step run is not in a running or pending state"),
), nil
}

engineStepRun, err := t.config.EngineRepository.StepRun().GetStepRunForEngine(tenant.ID, stepRun.ID)

if err != nil {
return nil, fmt.Errorf("could not get step run for engine: %w", err)
}

var reason = "CANCELLED_BY_USER"

// send a task to the taskqueue
err = t.config.MessageQueue.AddMessage(
ctx.Request().Context(),
msgqueue.JOB_PROCESSING_QUEUE,
tasktypes.StepRunCancelToTask(engineStepRun, reason),
)

if err != nil {
return nil, fmt.Errorf("could not add step queued task to task queue: %w", err)
}

// wait for a short period of time
for i := 0; i < 5; i++ {
newStepRun, err := t.config.APIRepository.StepRun().GetStepRunById(tenant.ID, stepRun.ID)

if err != nil {
return nil, fmt.Errorf("could not get step run: %w", err)
}

if newStepRun.Status != stepRun.Status {
stepRun = newStepRun
break
}

time.Sleep(100 * time.Millisecond)
}

res, err := transformers.ToStepRun(stepRun)

if err != nil {
return nil, fmt.Errorf("could not transform step run: %w", err)
}

return gen.StepRunUpdateCancel200JSONResponse(
*res,
), nil
}
337 changes: 217 additions & 120 deletions api/v1/server/oas/gen/openapi.gen.go

Large diffs are not rendered by default.

Loading

0 comments on commit 5066547

Please sign in to comment.