-
Notifications
You must be signed in to change notification settings - Fork 11
/
wait.go
150 lines (125 loc) · 3.54 KB
/
wait.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
package wait
import (
"fmt"
"io"
"time"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/util"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/tasks"
"github.com/spf13/cobra"
)
const (
FlagTimeout = "timeout"
DefaultTimeout = 600 // 600 seconds : 10 minutes
)
type WaitOptions struct {
*cmd.Dependencies
taskIDs []string
GetServerTasksCallback ServerTasksCallback
}
func NewWaitOps(dependencies *cmd.Dependencies, taskIDs []string) *WaitOptions {
return &WaitOptions{
Dependencies: dependencies,
GetServerTasksCallback: GetServerTasksCallback(dependencies.Client),
}
}
type ServerTasksCallback func([]string) ([]*tasks.Task, error)
func NewCmdWait(f factory.Factory) *cobra.Command {
var timeout int
cmd := &cobra.Command{
Use: "wait [TaskIDs]",
Short: "Wait for task(s) to finish",
Long: "Wait for a provided list of task(s) to finish",
Example: heredoc.Docf("$ %s task wait", constants.ExecutableName),
RunE: func(c *cobra.Command, args []string) error {
taskIDs := make([]string, len(args))
copy(taskIDs, args)
taskIDs = append(taskIDs, util.ReadValuesFromPipe()...)
dependencies := cmd.NewDependencies(f, c)
opts := NewWaitOps(dependencies, taskIDs)
return WaitRun(opts.Out, taskIDs, opts.GetServerTasksCallback, timeout)
},
}
flags := cmd.Flags()
flags.IntVar(&timeout, FlagTimeout, DefaultTimeout, "Duration to wait (in seconds) before stopping execution")
return cmd
}
func WaitRun(out io.Writer, taskIDs []string, getServerTasksCallback ServerTasksCallback, timeout int) error {
if len(taskIDs) == 0 {
return fmt.Errorf("no server task IDs provided, at least one is required")
}
tasks, err := getServerTasksCallback(taskIDs)
if err != nil {
return err
}
if len(tasks) == 0 {
return fmt.Errorf("no server tasks found")
}
pendingTaskIDs := make([]string, 0)
for _, t := range tasks {
if t.IsCompleted == nil || !*t.IsCompleted {
pendingTaskIDs = append(pendingTaskIDs, t.ID)
}
fmt.Fprintf(out, "%s: %s\n", t.Description, t.State)
}
if len(pendingTaskIDs) == 0 {
return nil
}
gotError := make(chan error, 1)
done := make(chan bool, 1)
go func() {
for len(pendingTaskIDs) != 0 {
time.Sleep(5 * time.Second)
tasks, err = getServerTasksCallback(pendingTaskIDs)
if err != nil {
gotError <- err
return
}
for _, t := range tasks {
if t.IsCompleted != nil && *t.IsCompleted {
fmt.Fprintf(out, "%s: %s\n", t.Description, t.State)
pendingTaskIDs = removeTaskID(pendingTaskIDs, t.ID)
}
}
}
done <- true
}()
select {
case <-done:
return nil
case err := <-gotError:
return err
case <-time.After(time.Duration(timeout) * time.Second):
return fmt.Errorf("timeout while waiting for pending tasks")
}
}
func GetServerTasksCallback(octopus *client.Client) ServerTasksCallback {
return func(taskIDs []string) ([]*tasks.Task, error) {
query := tasks.TasksQuery{
IDs: taskIDs,
}
resourceTasks, err := octopus.Tasks.Get(query)
if err != nil {
return nil, err
}
tasks, err := resourceTasks.GetAllPages(octopus.Sling())
if err != nil {
return nil, err
}
return tasks, nil
}
}
func removeTaskID(taskIDs []string, taskID string) []string {
for i, p := range taskIDs {
if p == taskID {
taskIDs[i] = taskIDs[len(taskIDs)-1]
taskIDs = taskIDs[:len(taskIDs)-1]
break
}
}
return taskIDs
}