Skip to content

Commit

Permalink
feat: add delete task job and list tasks job in scheduler job impleme…
Browse files Browse the repository at this point in the history
…ntation.

Signed-off-by: Asklv <[email protected]>
  • Loading branch information
IRONICBo committed Aug 4, 2024
1 parent df2ae9c commit dcbb0f7
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
38 changes: 37 additions & 1 deletion internal/job/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,52 @@

package job

import "d7y.io/dragonfly/v2/scheduler/resource"

type PreheatRequest struct {
URL string `json:"url" validate:"required,url"`
Tag string `json:"tag" validate:"omitempty"`
Digest string `json:"digest" validate:"omitempty"`
FilteredQueryParams string `json:"filteredQueryParams" validate:"omitempty"`
FilteredQueryParams string `json:"filtered_query_params" validate:"omitempty"`
Headers map[string]string `json:"headers" validate:"omitempty"`
Application string `json:"application" validate:"omitempty"`
Priority int32 `json:"priority" validate:"omitempty"`
PieceLength uint32 `json:"pieceLength" validate:"omitempty"`
}

type PreheatResponse struct {
TaskID string `json:"taskID"`
}

// ListTasksRequest defines the request parameters for listing tasks.
type ListTasksRequest struct {
TaskID string `json:"task_id" validate:"required"`
Page int `json:"page" validate:"required"`
PerPage int `json:"per_page" validate:"required"`
}

// ListTasksResponse defines the response parameters for listing tasks.
type ListTasksResponse struct {
Peers []*resource.Peer `json:"peers"`
Page int `json:"page"`
Total int `json:"total"`
}

// DeleteTaskRequest defines the request parameters for deleting task.
type DeleteTaskRequest struct {
TaskID string `json:"task_id" validate:"required"`
}

// TaskInfo includes information about a task along with peer details and a description.
type TaskInfo struct {
Task *resource.Task `json:"task"`
Peer *resource.Peer `json:"peer"`
Desc string `json:"desc"`
}

// DeleteTaskResponse represents the response after attempting to delete tasks,
// categorizing them into successfully and unsuccessfully deleted.
type DeleteTaskResponse struct {
SuccessTasks []*TaskInfo `json:"success_tasks"`
FailureTasks []*TaskInfo `json:"failure_tasks"`
}
66 changes: 66 additions & 0 deletions manager/handlers/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ var (
"user_id": 4,
"bio": "bio"
}`
mockListTasksJobReqBody = `
{
"type": "list_tasks",
"user_id": 4,
"bio": "bio"
}`
mockDeleteTaskJobReqBody = `
{
"type": "delete_task",
"user_id": 4,
"bio": "bio"
}`
mockOtherJobReqBody = `
{
"type": "others",
Expand All @@ -50,6 +62,16 @@ var (
Type: "preheat",
BIO: "bio",
}
mockListTasksCreateJobRequest = types.CreateListTasksJobRequest{
UserID: 4,
Type: "list_tasks",
BIO: "bio",
}
mockDeleteTaskCreateJobRequest = types.CreateDeleteTaskJobRequest{
UserID: 4,
Type: "delete_task",
BIO: "bio",
}
mockUpdateJobRequest = types.UpdateJobRequest{
UserID: 4,
BIO: "bio",
Expand All @@ -61,6 +83,20 @@ var (
BIO: "bio",
TaskID: "2",
}
mockListTasksJobModel = &models.Job{
BaseModel: mockBaseModel,
UserID: 4,
Type: "list_tasks",
BIO: "bio",
TaskID: "2",
}
mockDeleteTaskJobModel = &models.Job{
BaseModel: mockBaseModel,
UserID: 4,
Type: "delete_task",
BIO: "bio",
TaskID: "2",
}
)

func mockJobRouter(h *Handlers) *gin.Engine {
Expand Down Expand Up @@ -115,6 +151,36 @@ func TestHandlers_CreateJob(t *testing.T) {
assert.Equal(mockPreheatJobModel, &job)
},
},
{
name: "success",
req: httptest.NewRequest(http.MethodPost, "/oapi/v1/jobs", strings.NewReader(mockListTasksJobReqBody)),
mock: func(ms *mocks.MockServiceMockRecorder) {
ms.CreateListTasksJob(gomock.Any(), gomock.Eq(mockListTasksCreateJobRequest)).Return(mockListTasksJobModel, nil).Times(1)
},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusOK, w.Code)
job := models.Job{}
err := json.Unmarshal(w.Body.Bytes(), &job)
assert.NoError(err)
assert.Equal(mockListTasksJobModel, &job)
},
},
{
name: "success",
req: httptest.NewRequest(http.MethodPost, "/oapi/v1/jobs", strings.NewReader(mockDeleteTaskJobReqBody)),
mock: func(ms *mocks.MockServiceMockRecorder) {
ms.CreateDeleteTaskJob(gomock.Any(), gomock.Eq(mockDeleteTaskCreateJobRequest)).Return(mockDeleteTaskJobModel, nil).Times(1)
},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusOK, w.Code)
job := models.Job{}
err := json.Unmarshal(w.Body.Bytes(), &job)
assert.NoError(err)
assert.Equal(mockDeleteTaskJobModel, &job)
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion manager/job/manager_tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (m *managerTasks) createGroupJob(ctx context.Context, name string, args []m

logger.Infof("create manager tasks group %s in queues %v, tasks: %#v", group.GroupUUID, queues, tasks)
if _, err := m.job.Server.SendGroupWithContext(ctx, group, 0); err != nil {
logger.Errorf("create manager tasks group %s failed", group.GroupUUID, err)
logger.Errorf("create manager tasks group %s failed", group.GroupUUID, err)
return nil, err
}

Expand Down

0 comments on commit dcbb0f7

Please sign in to comment.