tasks_list.go 7.0 KB

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