Skip to content

Commit 1e40f76

Browse files
author
Gabriel Monroy
committed
Merge pull request #119 from opdemand/118-progress-threads
Fixed #118 -- fixed progress thread management in CLI.
2 parents 20b01d2 + 4033d94 commit 1e40f76

1 file changed

Lines changed: 32 additions & 13 deletions

File tree

client/deis.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from threading import Thread
5050
import glob
5151
import json
52+
import os
5253
import os.path
5354
import random
5455
import re
@@ -218,18 +219,19 @@ def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
218219

219220
def run(self):
220221
"""Write ASCII progress animation frames to stdout."""
221-
time.sleep(0.5)
222-
self._write_frame(self.frames.next(), erase=False)
223-
while not self.cancelled.is_set():
224-
time.sleep(0.4)
225-
self._write_frame(self.frames.next())
222+
if not os.environ.get('DEIS_HIDE_PROGRESS'):
223+
time.sleep(0.5)
224+
self._write_frame(self.frames.next(), erase=False)
225+
while not self.cancelled.is_set():
226+
time.sleep(0.4)
227+
self._write_frame(self.frames.next())
228+
# clear the animation
229+
sys.stdout.write('\b' * (len(self.frames.next()) + 2))
230+
sys.stdout.flush()
226231

227232
def cancel(self):
228233
"""Set the animation thread as cancelled."""
229234
self.cancelled.set()
230-
# clear the animation
231-
sys.stdout.write('\b' * (len(self.frames.next()) + 2))
232-
sys.stdout.flush()
233235

234236
def _write_frame(self, frame, erase=True):
235237
if erase:
@@ -241,9 +243,6 @@ def _write_frame(self, frame, erase=True):
241243
sys.stdout.flush()
242244

243245

244-
progress = TextProgress()
245-
246-
247246
def dictify(args):
248247
"""Converts a list of key=val strings into a python dict.
249248
@@ -607,13 +606,15 @@ def containers_scale(self, args):
607606
body.update({typ: int(count)})
608607
print('Scaling containers... but first, coffee!')
609608
try:
609+
progress = TextProgress()
610610
progress.start()
611611
before = time.time()
612612
response = self._dispatch('post',
613613
"/api/formations/{}/scale/containers".format(formation),
614614
json.dumps(body))
615615
finally:
616616
progress.cancel()
617+
progress.join()
617618
if response.status_code == requests.codes.ok: # @UndefinedVariable
618619
print('done in {}s\n'.format(int(time.time() - before)))
619620
self.containers_list({})
@@ -748,8 +749,14 @@ def formations_create(self, args):
748749
return
749750
sys.stdout.write('Creating formation... ')
750751
sys.stdout.flush()
751-
response = self._dispatch('post', '/api/formations',
752-
json.dumps(body))
752+
try:
753+
progress = TextProgress()
754+
progress.start()
755+
response = self._dispatch('post', '/api/formations',
756+
json.dumps(body))
757+
finally:
758+
progress.cancel()
759+
progress.join()
753760
if response.status_code == requests.codes.created: # @UndefinedVariable
754761
data = response.json()
755762
formation = data['id']
@@ -843,11 +850,13 @@ def formations_destroy(self, args):
843850
sys.stdout.write("Destroying {}... ".format(formation))
844851
sys.stdout.flush()
845852
try:
853+
progress = TextProgress()
846854
progress.start()
847855
before = time.time()
848856
response = self._dispatch('delete', "/api/formations/{}".format(formation))
849857
finally:
850858
progress.cancel()
859+
progress.join()
851860
if response.status_code in (requests.codes.no_content, # @UndefinedVariable
852861
requests.codes.not_found): # @UndefinedVariable
853862
print('done in {}s'.format(int(time.time() - before)))
@@ -899,12 +908,14 @@ def formations_converge(self, args):
899908
sys.stdout.write('Converging {}... '.format(formation))
900909
sys.stdout.flush()
901910
try:
911+
progress = TextProgress()
902912
progress.start()
903913
before = time.time()
904914
response = self._dispatch('post',
905915
"/api/formations/{}/converge".format(formation))
906916
finally:
907917
progress.cancel()
918+
progress.join()
908919
if response.status_code == requests.codes.ok: # @UndefinedVariable
909920
print('done in {}s'.format(int(time.time() - before)))
910921
databag = json.loads(response.content)
@@ -1069,12 +1080,14 @@ def layers_create(self, args):
10691080
sys.stdout.write("Creating {} layer... ".format(args['<id>']))
10701081
sys.stdout.flush()
10711082
try:
1083+
progress = TextProgress()
10721084
progress.start()
10731085
before = time.time()
10741086
response = self._dispatch('post', "/api/formations/{}/layers".format(formation),
10751087
json.dumps(body))
10761088
finally:
10771089
progress.cancel()
1090+
progress.join()
10781091
if response.status_code == requests.codes.created: # @UndefinedVariable
10791092
print('done in {}s'.format(int(time.time() - before)))
10801093
else:
@@ -1093,12 +1106,14 @@ def layers_destroy(self, args):
10931106
sys.stdout.write("Destroying {layer} layer... ".format(**locals()))
10941107
sys.stdout.flush()
10951108
try:
1109+
progress = TextProgress()
10961110
progress.start()
10971111
before = time.time()
10981112
response = self._dispatch(
10991113
'delete', "/api/formations/{formation}/layers/{layer}".format(**locals()))
11001114
finally:
11011115
progress.cancel()
1116+
progress.join()
11021117
if response.status_code == requests.codes.no_content: # @UndefinedVariable
11031118
print('done in {}s'.format(int(time.time() - before)))
11041119
else:
@@ -1142,6 +1157,7 @@ def layers_scale(self, args):
11421157
body.update({typ: int(count)})
11431158
print('Scaling layers... but first, coffee!')
11441159
try:
1160+
progress = TextProgress()
11451161
progress.start()
11461162
before = time.time()
11471163
# TODO: add threaded spinner to print dots
@@ -1150,6 +1166,7 @@ def layers_scale(self, args):
11501166
json.dumps(body))
11511167
finally:
11521168
progress.cancel()
1169+
progress.join()
11531170
if response.status_code == requests.codes.ok: # @UndefinedVariable
11541171
print('done in {}s\n'.format(int(time.time() - before)))
11551172
print('Use `git push deis master` to deploy to your formation')
@@ -1241,12 +1258,14 @@ def nodes_destroy(self, args):
12411258
sys.stdout.write("Destroying {}... ".format(node))
12421259
sys.stdout.flush()
12431260
try:
1261+
progress = TextProgress()
12441262
progress.start()
12451263
before = time.time()
12461264
response = self._dispatch(
12471265
'delete', "/api/formations/{formation}/nodes/{node}".format(**locals()))
12481266
finally:
12491267
progress.cancel()
1268+
progress.join()
12501269
if response.status_code == requests.codes.no_content: # @UndefinedVariable
12511270
print('done in {}s\n'.format(int(time.time() - before)))
12521271
else:

0 commit comments

Comments
 (0)