-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtasks_list.go
276 lines (244 loc) · 8.69 KB
/
tasks_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/olivere/elastic/v7/uritemplates"
)
// TasksListService retrieves the list of currently executing tasks
// on one ore more nodes in the cluster. It is part of the Task Management API
// documented at https://www.elastic.co/guide/en/elasticsearch/reference/7.0/tasks.html.
//
// It is supported as of Elasticsearch 2.3.0.
type TasksListService struct {
client *Client
pretty *bool // pretty format the returned JSON response
human *bool // return human readable values for statistics
errorTrace *bool // include the stack trace of returned errors
filterPath []string // list of filters used to reduce the response
headers http.Header // custom request-level HTTP headers
taskId []string
actions []string
detailed *bool
nodeId []string
parentTaskId string
waitForCompletion *bool
groupBy string
}
// NewTasksListService creates a new TasksListService.
func NewTasksListService(client *Client) *TasksListService {
return &TasksListService{
client: client,
}
}
// Pretty tells Elasticsearch whether to return a formatted JSON response.
func (s *TasksListService) Pretty(pretty bool) *TasksListService {
s.pretty = &pretty
return s
}
// Human specifies whether human readable values should be returned in
// the JSON response, e.g. "7.5mb".
func (s *TasksListService) Human(human bool) *TasksListService {
s.human = &human
return s
}
// ErrorTrace specifies whether to include the stack trace of returned errors.
func (s *TasksListService) ErrorTrace(errorTrace bool) *TasksListService {
s.errorTrace = &errorTrace
return s
}
// FilterPath specifies a list of filters used to reduce the response.
func (s *TasksListService) FilterPath(filterPath ...string) *TasksListService {
s.filterPath = filterPath
return s
}
// Header adds a header to the request.
func (s *TasksListService) Header(name string, value string) *TasksListService {
if s.headers == nil {
s.headers = http.Header{}
}
s.headers.Add(name, value)
return s
}
// Headers specifies the headers of the request.
func (s *TasksListService) Headers(headers http.Header) *TasksListService {
s.headers = headers
return s
}
// TaskId indicates to returns the task(s) with specified id(s).
// Notice that the caller is responsible for using the correct format,
// i.e. node_id:task_number, as specified in the REST API.
func (s *TasksListService) TaskId(taskId ...string) *TasksListService {
s.taskId = append(s.taskId, taskId...)
return s
}
// Actions is a list of actions that should be returned. Leave empty to return all.
func (s *TasksListService) Actions(actions ...string) *TasksListService {
s.actions = append(s.actions, actions...)
return s
}
// Detailed indicates whether to return detailed task information (default: false).
func (s *TasksListService) Detailed(detailed bool) *TasksListService {
s.detailed = &detailed
return s
}
// NodeId is a list of node IDs or names to limit the returned information;
// use `_local` to return information from the node you're connecting to,
// leave empty to get information from all nodes.
func (s *TasksListService) NodeId(nodeId ...string) *TasksListService {
s.nodeId = append(s.nodeId, nodeId...)
return s
}
// ParentTaskId returns tasks with specified parent task id.
// Notice that the caller is responsible for using the correct format,
// i.e. node_id:task_number, as specified in the REST API.
func (s *TasksListService) ParentTaskId(parentTaskId string) *TasksListService {
s.parentTaskId = parentTaskId
return s
}
// WaitForCompletion indicates whether to wait for the matching tasks
// to complete (default: false).
func (s *TasksListService) WaitForCompletion(waitForCompletion bool) *TasksListService {
s.waitForCompletion = &waitForCompletion
return s
}
// GroupBy groups tasks by nodes or parent/child relationships.
// As of now, it can either be "nodes" (default) or "parents" or "none".
func (s *TasksListService) GroupBy(groupBy string) *TasksListService {
s.groupBy = groupBy
return s
}
// buildURL builds the URL for the operation.
func (s *TasksListService) buildURL() (string, url.Values, error) {
// Build URL
var err error
var path string
if len(s.taskId) > 0 {
path, err = uritemplates.Expand("/_tasks/{task_id}", map[string]string{
"task_id": strings.Join(s.taskId, ","),
})
} else {
path = "/_tasks"
}
if err != nil {
return "", url.Values{}, err
}
// Add query string parameters
params := url.Values{}
if v := s.pretty; v != nil {
params.Set("pretty", fmt.Sprint(*v))
}
if v := s.human; v != nil {
params.Set("human", fmt.Sprint(*v))
}
if v := s.errorTrace; v != nil {
params.Set("error_trace", fmt.Sprint(*v))
}
if len(s.filterPath) > 0 {
params.Set("filter_path", strings.Join(s.filterPath, ","))
}
if len(s.actions) > 0 {
params.Set("actions", strings.Join(s.actions, ","))
}
if v := s.detailed; v != nil {
params.Set("detailed", fmt.Sprint(*v))
}
if len(s.nodeId) > 0 {
params.Set("nodes", strings.Join(s.nodeId, ","))
}
if s.parentTaskId != "" {
params.Set("parent_task_id", s.parentTaskId)
}
if v := s.waitForCompletion; v != nil {
params.Set("wait_for_completion", fmt.Sprint(*v))
}
if s.groupBy != "" {
params.Set("group_by", s.groupBy)
}
return path, params, nil
}
// Validate checks if the operation is valid.
func (s *TasksListService) Validate() error {
return nil
}
// Do executes the operation.
func (s *TasksListService) Do(ctx context.Context) (*TasksListResponse, error) {
// Check pre-conditions
if err := s.Validate(); err != nil {
return nil, err
}
// Get URL for request
path, params, err := s.buildURL()
if err != nil {
return nil, err
}
// Get HTTP response
res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
Method: "GET",
Path: path,
Params: params,
Headers: s.headers,
})
if err != nil {
return nil, err
}
// Return operation response
ret := new(TasksListResponse)
if err := s.client.decoder.Decode(res.Body, ret); err != nil {
return nil, err
}
ret.Header = res.Header
return ret, nil
}
// TasksListResponse is the response of TasksListService.Do.
type TasksListResponse struct {
Header http.Header `json:"-"`
TaskFailures []*TaskOperationFailure `json:"task_failures"`
NodeFailures []*FailedNodeException `json:"node_failures"`
// Nodes returns the tasks per node. The key is the node id.
Nodes map[string]*DiscoveryNode `json:"nodes"`
}
type TaskOperationFailure struct {
TaskId int64 `json:"task_id"` // this is a long in the Java source
NodeId string `json:"node_id"`
Status string `json:"status"`
Reason *ErrorDetails `json:"reason"`
}
type DiscoveryNode struct {
Name string `json:"name"`
TransportAddress string `json:"transport_address"`
Host string `json:"host"`
IP string `json:"ip"`
Roles []string `json:"roles"` // "master", "data", or "ingest"
Attributes map[string]interface{} `json:"attributes"`
// Tasks returns the tasks by its id (as a string).
Tasks map[string]*TaskInfo `json:"tasks"`
}
// TaskInfo represents information about a currently running task.
type TaskInfo struct {
Node string `json:"node"`
Id int64 `json:"id"` // the task id (yes, this is a long in the Java source)
Type string `json:"type"`
Action string `json:"action"`
Status interface{} `json:"status"` // has separate implementations of Task.Status in Java for reindexing, replication, and "RawTaskStatus"
Description interface{} `json:"description"` // same as Status
StartTime string `json:"start_time"`
StartTimeInMillis int64 `json:"start_time_in_millis"`
RunningTime string `json:"running_time"`
RunningTimeInNanos int64 `json:"running_time_in_nanos"`
Cancellable bool `json:"cancellable"`
Cancelled bool `json:"cancelled"`
ParentTaskId string `json:"parent_task_id"` // like "YxJnVYjwSBm_AUbzddTajQ:12356"
Headers map[string]string `json:"headers"`
}
// StartTaskResult is used in cases where a task gets started asynchronously and
// the operation simply returnes a TaskID to watch for via the Task Management API.
type StartTaskResult struct {
Header http.Header `json:"-"`
TaskId string `json:"task"`
}