@@ -11,7 +11,15 @@ import (
11
11
)
12
12
13
13
func (d * Driver ) getClient () * hcloud.Client {
14
- return hcloud .NewClient (hcloud .WithToken (d .AccessToken ), hcloud .WithApplication ("docker-machine-driver" , d .version ))
14
+ opts := []hcloud.ClientOption {
15
+ hcloud .WithToken (d .AccessToken ),
16
+ hcloud .WithApplication ("docker-machine-driver" , d .version ),
17
+ hcloud .WithPollBackoffFunc (hcloud .ConstantBackoff (time .Duration (d .WaitOnPolling ) * time .Second )),
18
+ }
19
+
20
+ opts = d .setupClientInstrumentation (opts )
21
+
22
+ return hcloud .NewClient (opts ... )
15
23
}
16
24
17
25
func (d * Driver ) getLocationNullable () (* hcloud.Location , error ) {
@@ -166,25 +174,24 @@ func (d *Driver) getServerHandleNullable() (*hcloud.Server, error) {
166
174
}
167
175
168
176
func (d * Driver ) waitForAction (a * hcloud.Action ) error {
169
- for {
170
- act , _ , err := d .getClient ().Action .GetByID (context .Background (), a .ID )
171
- if err != nil {
172
- return errors .Wrap (err , "could not get client by ID" )
173
- }
174
- if act == nil {
175
- return fmt .Errorf ("action not found: %v" , a .ID )
176
- }
177
-
178
- if act .Status == hcloud .ActionStatusSuccess {
179
- log .Debugf (" -> finished %s[%d]" , act .Command , act .ID )
180
- break
181
- } else if act .Status == hcloud .ActionStatusRunning {
182
- log .Debugf (" -> %s[%d]: %d %%" , act .Command , act .ID , act .Progress )
183
- } else if act .Status == hcloud .ActionStatusError {
184
- return act .Error ()
177
+ progress , done := d .getClient ().Action .WatchProgress (context .Background (), a )
178
+
179
+ running := true
180
+ var ret error
181
+
182
+ for running {
183
+ select {
184
+ case <- done :
185
+ ret = <- done
186
+ running = false
187
+ case <- progress :
188
+ log .Debugf (" -> %s[%d]: %d %%" , a .Command , a .ID , <- progress )
185
189
}
190
+ }
186
191
187
- time .Sleep (time .Duration (d .WaitOnPolling ) * time .Second )
192
+ if ret == nil {
193
+ log .Debugf (" -> finished %s[%d]" , a .Command , a .ID )
188
194
}
189
- return nil
195
+
196
+ return ret
190
197
}
0 commit comments