Debian Codegraph @

tasks_cancel.go

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