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

Add cancel task api for v0.30.0 #395

Merged
merged 8 commits into from Dec 14, 2022
83 changes: 83 additions & 0 deletions client.go
Expand Up @@ -54,6 +54,9 @@ type ClientInterface interface {
IsHealthy() bool
GetTask(taskUID int64) (resp *Task, err error)
GetTasks(param *TasksQuery) (resp *TaskResult, err error)
CancelTasks(param *CancelTasksQuery) (resp *TaskInfo, err error)
DeleteTasks(param *DeleteTasksQuery) (resp *TaskInfo, err error)
SwapIndexes(param []SwapIndexesParams) (resp *TaskInfo, err error)
WaitForTask(taskUID int64, options ...WaitParams) (*Task, error)
GenerateTenantToken(APIKeyUID string, searchRules map[string]interface{}, options *TenantTokenOptions) (resp string, err error)
}
Expand Down Expand Up @@ -285,6 +288,86 @@ func (c *Client) GetTasks(param *TasksQuery) (resp *TaskResult, err error) {
return resp, nil
}

func (c *Client) CancelTasks(param *CancelTasksQuery) (resp *TaskInfo, err error) {
resp = &TaskInfo{}
req := internalRequest{
endpoint: "/tasks/cancel",
method: http.MethodPost,
withRequest: nil,
withResponse: &resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "CancelTasks",
}
if param != nil {
paramToSend := &TasksQuery{
UIDS: param.UIDS,
IndexUIDS: param.IndexUIDS,
Statuses: param.Statuses,
Types: param.Types,
BeforeEnqueuedAt: param.BeforeEnqueuedAt,
AfterEnqueuedAt: param.AfterEnqueuedAt,
BeforeStartedAt: param.BeforeStartedAt,
AfterStartedAt: param.AfterStartedAt,
}
encodeTasksQuery(paramToSend, &req)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this function encodeTasksQuery support multiple sets of params?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, I could put interface{} as a parameter instead of a typed struct, but I wouldn't have access to the fields easily.

}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}

func (c *Client) DeleteTasks(param *DeleteTasksQuery) (resp *TaskInfo, err error) {
resp = &TaskInfo{}
req := internalRequest{
endpoint: "/tasks",
method: http.MethodDelete,
withRequest: nil,
withResponse: &resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "DeleteTasks",
}
if param != nil {
paramToSend := &TasksQuery{
UIDS: param.UIDS,
IndexUIDS: param.IndexUIDS,
Statuses: param.Statuses,
Types: param.Types,
CanceledBy: param.CanceledBy,
BeforeEnqueuedAt: param.BeforeEnqueuedAt,
AfterEnqueuedAt: param.AfterEnqueuedAt,
BeforeStartedAt: param.BeforeStartedAt,
AfterStartedAt: param.AfterStartedAt,
BeforeFinishedAt: param.BeforeFinishedAt,
AfterFinishedAt: param.AfterFinishedAt,
}
encodeTasksQuery(paramToSend, &req)
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}

func (c *Client) SwapIndexes(param []SwapIndexesParams) (resp *TaskInfo, err error) {
resp = &TaskInfo{}
req := internalRequest{
endpoint: "/swap-indexes",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: param,
withResponse: &resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "SwapIndexes",
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}

// WaitForTask waits for a task to be processed
//
// The function will check by regular interval provided in parameter interval
Expand Down