Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes 3861: reject cancellation of certain task types #719

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/config/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ const (
)

var RequeueableTasks = []string{DeleteTemplatesTask, DeleteRepositorySnapshotsTask, UpdateTemplateContentTask}

var CancellableTasks = []string{IntrospectTask, RepositorySnapshotTask}
9 changes: 7 additions & 2 deletions pkg/handler/task_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
ce "github.com/content-services/content-sources-backend/pkg/errors"
"github.com/content-services/content-sources-backend/pkg/rbac"
"github.com/content-services/content-sources-backend/pkg/tasks/client"
"github.com/content-services/content-sources-backend/pkg/tasks/queue"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -104,11 +105,15 @@ func (t *TaskInfoHandler) cancel(c echo.Context) error {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "error canceling task", err.Error())
}
if task.OrgId == config.RedHatOrg {
return ce.NewErrorResponse(http.StatusBadRequest, "Cannot cancel a Red Hat Task", err.Error())
return ce.NewErrorResponse(http.StatusBadRequest, "error canceling task", "Cannot cancel a Red Hat Task")
}
err = t.TaskClient.SendCancelNotification(c.Request().Context(), id)
if err != nil {
return ce.NewErrorResponse(http.StatusInternalServerError, "error canceling task", err.Error())
if err == queue.ErrNotCancellable {
return ce.NewErrorResponse(http.StatusBadRequest, "error canceling task", err.Error())
} else {
return ce.NewErrorResponse(http.StatusInternalServerError, "error canceling task", err.Error())
}
}
return c.NoContent(http.StatusNoContent)
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/tasks/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package client

import (
"context"
"slices"

"github.com/content-services/content-sources-backend/pkg/config"
"github.com/content-services/content-sources-backend/pkg/tasks"
"github.com/content-services/content-sources-backend/pkg/tasks/queue"
"github.com/google/uuid"
Expand Down Expand Up @@ -40,6 +42,13 @@ func (c *Client) SendCancelNotification(ctx context.Context, taskId string) erro
if err != nil {
return err
}
task, err := c.queue.Status(taskUUID)
if err != nil {
return err
}
if !slices.Contains(config.CancellableTasks, task.Typename) {
return queue.ErrNotCancellable
}
err = c.queue.SendCancelNotification(ctx, taskUUID)
if err != nil {
return err
Expand Down
27 changes: 24 additions & 3 deletions pkg/tasks/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/content-services/content-sources-backend/pkg/models"
"github.com/content-services/content-sources-backend/pkg/tasks/queue"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -35,10 +36,30 @@ func (s *ClientSuite) TestEnqueue() {

func (s *ClientSuite) TestSendCancelNotification() {
mockQueue := queue.NewMockQueue(s.T())
expectedUuid := uuid.New()
expectedUuid1 := uuid.New()
expectedUuid2 := uuid.New()
cancellableTask := &models.TaskInfo{
Typename: "snapshot",
}
uncancellableTask := &models.TaskInfo{
Typename: "test",
}

mockQueue.On("SendCancelNotification", context.Background(), expectedUuid).Return(nil)
// Test cancel succeeds for cancellable task type
mockQueue.On("Status", expectedUuid1).Return(cancellableTask, nil)
mockQueue.On("SendCancelNotification", context.Background(), expectedUuid1).Return(nil)
tc := NewTaskClient(mockQueue)
err := tc.SendCancelNotification(context.Background(), expectedUuid.String())
taskInfo, err := mockQueue.Status(expectedUuid1)
assert.NoError(s.T(), err)
assert.NotNil(s.T(), taskInfo)
err = tc.SendCancelNotification(context.Background(), expectedUuid1.String())
assert.NoError(s.T(), err)

// Test cancel errors for un-cancellable task type
mockQueue.On("Status", expectedUuid2).Return(uncancellableTask, nil)
taskInfo, err = mockQueue.Status(expectedUuid2)
assert.NoError(s.T(), err)
assert.NotNil(s.T(), taskInfo)
err = tc.SendCancelNotification(context.Background(), expectedUuid2.String())
assert.Error(s.T(), err)
}
1 change: 1 addition & 0 deletions pkg/tasks/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ var (
ErrContextCanceled = fmt.Errorf("dequeue context timed out or was canceled")
ErrRowsNotAffected = fmt.Errorf("no rows were affected")
ErrMaxRetriesExceeded = fmt.Errorf("task has exceeded the maximum amount of retries")
ErrNotCancellable = fmt.Errorf("task not cancellable")
)
Loading