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