tasks_cancel.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // TasksCancelService can cancel long-running tasks.
  13. // It is supported as of Elasticsearch 2.3.0.
  14. //
  15. // See http://www.elastic.co/guide/en/elasticsearch/reference/5.2/tasks-cancel.html
  16. // for details.
  17. type TasksCancelService struct {
  18. client *Client
  19. pretty bool
  20. taskId *int64
  21. actions []string
  22. nodeId []string
  23. parentNode string
  24. parentTask *int64
  25. }
  26. // NewTasksCancelService creates a new TasksCancelService.
  27. func NewTasksCancelService(client *Client) *TasksCancelService {
  28. return &TasksCancelService{
  29. client: client,
  30. actions: make([]string, 0),
  31. nodeId: make([]string, 0),
  32. }
  33. }
  34. // TaskId specifies the task to cancel. Set to -1 to cancel all tasks.
  35. func (s *TasksCancelService) TaskId(taskId int64) *TasksCancelService {
  36. s.taskId = &taskId
  37. return s
  38. }
  39. // Actions is a list of actions that should be cancelled. Leave empty to cancel all.
  40. func (s *TasksCancelService) Actions(actions []string) *TasksCancelService {
  41. s.actions = actions
  42. return s
  43. }
  44. // NodeId is a list of node IDs or names to limit the returned information;
  45. // use `_local` to return information from the node you're connecting to,
  46. // leave empty to get information from all nodes.
  47. func (s *TasksCancelService) NodeId(nodeId []string) *TasksCancelService {
  48. s.nodeId = nodeId
  49. return s
  50. }
  51. // ParentNode specifies to cancel tasks with specified parent node.
  52. func (s *TasksCancelService) ParentNode(parentNode string) *TasksCancelService {
  53. s.parentNode = parentNode
  54. return s
  55. }
  56. // ParentTask specifies to cancel tasks with specified parent task id.
  57. // Set to -1 to cancel all.
  58. func (s *TasksCancelService) ParentTask(parentTask int64) *TasksCancelService {
  59. s.parentTask = &parentTask
  60. return s
  61. }
  62. // Pretty indicates that the JSON response be indented and human readable.
  63. func (s *TasksCancelService) Pretty(pretty bool) *TasksCancelService {
  64. s.pretty = pretty
  65. return s
  66. }
  67. // buildURL builds the URL for the operation.
  68. func (s *TasksCancelService) buildURL() (string, url.Values, error) {
  69. // Build URL
  70. var err error
  71. var path string
  72. if s.taskId != nil {
  73. path, err = uritemplates.Expand("/_tasks/{task_id}/_cancel", map[string]string{
  74. "task_id": fmt.Sprintf("%d", *s.taskId),
  75. })
  76. } else {
  77. path = "/_tasks/_cancel"
  78. }
  79. if err != nil {
  80. return "", url.Values{}, err
  81. }
  82. // Add query string parameters
  83. params := url.Values{}
  84. if s.pretty {
  85. params.Set("pretty", "1")
  86. }
  87. if len(s.actions) > 0 {
  88. params.Set("actions", strings.Join(s.actions, ","))
  89. }
  90. if len(s.nodeId) > 0 {
  91. params.Set("node_id", strings.Join(s.nodeId, ","))
  92. }
  93. if s.parentNode != "" {
  94. params.Set("parent_node", s.parentNode)
  95. }
  96. if s.parentTask != nil {
  97. params.Set("parent_task", fmt.Sprintf("%v", *s.parentTask))
  98. }
  99. return path, params, nil
  100. }
  101. // Validate checks if the operation is valid.
  102. func (s *TasksCancelService) Validate() error {
  103. return nil
  104. }
  105. // Do executes the operation.
  106. func (s *TasksCancelService) Do(ctx context.Context) (*TasksListResponse, error) {
  107. // Check pre-conditions
  108. if err := s.Validate(); err != nil {
  109. return nil, err
  110. }
  111. // Get URL for request
  112. path, params, err := s.buildURL()
  113. if err != nil {
  114. return nil, err
  115. }
  116. // Get HTTP response
  117. res, err := s.client.PerformRequest(ctx, "POST", path, params, nil)
  118. if err != nil {
  119. return nil, err
  120. }
  121. // Return operation response
  122. ret := new(TasksListResponse)
  123. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  124. return nil, err
  125. }
  126. return ret, nil
  127. }