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

Redesign update API to task API #262

Merged
merged 4 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
73 changes: 71 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package meilisearch

import (
"context"
"net/http"
"strconv"
"time"

"github.com/valyala/fasthttp"
Expand All @@ -28,8 +30,8 @@ type ClientInterface interface {
GetRawIndex(uid string) (resp map[string]interface{}, err error)
GetAllIndexes() (resp []*Index, err error)
GetAllRawIndexes() (resp []map[string]interface{}, err error)
CreateIndex(config *IndexConfig) (resp *Index, err error)
DeleteIndex(uid string) (bool, error)
CreateIndex(config *IndexConfig) (resp *Task, err error)
DeleteIndex(uid string) (resp *Task, err error)
GetKeys() (resp *Keys, err error)
GetAllStats() (resp *Stats, err error)
CreateDump() (resp *Dump, err error)
Expand All @@ -38,6 +40,10 @@ type ClientInterface interface {
GetVersion() (resp *Version, err error)
Health() (*Health, error)
IsHealthy() bool
GetTask(taskID int64) (resp *Task, err error)
GetTasks() (resp *ResultTask, err error)
WaitForTask(ctx context.Context, interval time.Duration, taskID *Task) (*Task, error)
alallema marked this conversation as resolved.
Show resolved Hide resolved
DefaultWaitForTask(taskID *Task) (*Task, error)
Copy link
Contributor

Choose a reason for hiding this comment

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

What is defaultWaitForTask ?

I see that it is when you provide the default values. Is this the naming convention in GO ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is the WaitForTask with default values. I don't think this naming is a specific convention in go. I can rename it if you have a better naming 🤩

Copy link
Contributor

Choose a reason for hiding this comment

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

in waitForTask you can add a structure the following way

type waitParams struct{
   interval: int,
   taskId: int
}

provide this as a parameter and in case of null use the default value?

function waitForTask(taskId: int, params WaitParams) {
   var interval = 500 // default value
   if (params.interval != 0) {
     interval = params.interval
  }
  // etc ...
}

would something like that be possible?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but you had to specify the last param at nil like:

	client. waitForTask(0, nil)

There is no optional parameters in golang but I can use variadic args and do something like:

func (c *Client) WaitForTask(task *Task, options ...waitParams) (*Task, error) {
	if (options == nil) {
		ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*5)
		defer cancelFunc()
		options[0].Context = ctx
		options[0].Interval = time.Millisecond*50
	}
... do waitForTask ...
}

And I can use it like:

	client. waitForTask(0)
	// OR
	client. waitForTask(0, &waitParams{ctx, time.Millisecond*10})

I'm not sure which one is the most elegant because in the second option I get the waitParams as an array.
What do you think?

}

var _ ClientInterface = &Client{}
Expand Down Expand Up @@ -170,3 +176,66 @@ func (c *Client) GetDumpStatus(dumpUID string) (resp *Dump, err error) {
}
return resp, nil
}

func (c *Client) GetTask(taskID int64) (resp *Task, err error) {
resp = &Task{}
req := internalRequest{
endpoint: "/tasks/" + strconv.FormatInt(taskID, 10),
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetTask",
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}

func (c *Client) GetTasks() (resp *ResultTask, err error) {
resp = &ResultTask{}
req := internalRequest{
endpoint: "/tasks",
method: http.MethodGet,
withRequest: nil,
withResponse: &resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetTasks",
}
if err := c.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}

// // DefaultWaitForTask checks each 50ms the status of a task.
// // This is a default implementation of WaitForTask.
func (c *Client) DefaultWaitForTask(task *Task) (*Task, error) {
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*5)
defer cancelFunc()
return c.WaitForTask(ctx, time.Millisecond*50, task)
}

// WaitForTask waits for a task to be processed.
// The function will check by regular interval provided in parameter interval
// the TaskStatus. If it is not TaskStatusEnqueued or the ctx cancelled
// we return the TaskStatus.
func (c *Client) WaitForTask(
ctx context.Context,
interval time.Duration,
task *Task) (*Task, error) {
for {
if err := ctx.Err(); err != nil {
return nil, err
}
getTask, err := c.GetTask(task.UID)
if err != nil {
return nil, err
}
if getTask.Status != TaskStatusEnqueued && getTask.Status != TaskStatusProcessing {
return getTask, nil
}
time.Sleep(interval)
}
}
22 changes: 12 additions & 10 deletions client_index.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package meilisearch

import "net/http"
import (
"net/http"
)

func (c *Client) Index(uid string) *Index {
return newIndex(c, uid)
Expand All @@ -26,19 +28,19 @@ func (c *Client) GetRawIndex(uid string) (resp map[string]interface{}, err error
return resp, nil
}

func (c *Client) CreateIndex(config *IndexConfig) (resp *Index, err error) {
func (c *Client) CreateIndex(config *IndexConfig) (resp *Task, err error) {
request := &CreateIndexRequest{
UID: config.Uid,
PrimaryKey: config.PrimaryKey,
}
resp = newIndex(c, config.Uid)
resp = &Task{}
req := internalRequest{
endpoint: "/indexes",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: request,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusCreated},
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "CreateIndex",
}
if err := c.executeRequest(req); err != nil {
Expand Down Expand Up @@ -79,18 +81,18 @@ func (c *Client) GetAllRawIndexes() (resp []map[string]interface{}, err error) {
return resp, nil
}

func (c *Client) DeleteIndex(uid string) (ok bool, err error) {
func (c *Client) DeleteIndex(uid string) (resp *Task, err error) {
resp = &Task{}
req := internalRequest{
endpoint: "/indexes/" + uid,
method: http.MethodDelete,
withRequest: nil,
withResponse: nil,
acceptedStatusCodes: []int{http.StatusNoContent},
withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "DeleteIndex",
}
// err is not nil if status code is not 204 StatusNoContent
if err := c.executeRequest(req); err != nil {
return false, err
return nil, err
}
return true, nil
return resp, nil
}
Loading