parallel.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import operator
  4. import sys
  5. from threading import Thread
  6. from docker.errors import APIError
  7. from six.moves import _thread as thread
  8. from six.moves.queue import Empty
  9. from six.moves.queue import Queue
  10. from compose.cli.signals import ShutdownException
  11. from compose.utils import get_output_stream
  12. def perform_operation(func, arg, callback, index):
  13. try:
  14. callback((index, func(arg)))
  15. except Exception as e:
  16. callback((index, e))
  17. def parallel_execute(objects, func, index_func, msg):
  18. """For a given list of objects, call the callable passing in the first
  19. object we give it.
  20. """
  21. objects = list(objects)
  22. stream = get_output_stream(sys.stderr)
  23. writer = ParallelStreamWriter(stream, msg)
  24. q = setup_queue(writer, objects, func, index_func)
  25. done = 0
  26. errors = {}
  27. while done < len(objects):
  28. try:
  29. msg_index, result = q.get(timeout=1)
  30. except Empty:
  31. continue
  32. # See https://github.com/docker/compose/issues/189
  33. except thread.error:
  34. raise ShutdownException()
  35. if isinstance(result, APIError):
  36. errors[msg_index] = "error", result.explanation
  37. writer.write(msg_index, 'error')
  38. elif isinstance(result, Exception):
  39. errors[msg_index] = "unexpected_exception", result
  40. else:
  41. writer.write(msg_index, 'done')
  42. done += 1
  43. if not errors:
  44. return
  45. stream.write("\n")
  46. for msg_index, (result, error) in errors.items():
  47. stream.write("ERROR: for {} {} \n".format(msg_index, error))
  48. if result == 'unexpected_exception':
  49. raise error
  50. def setup_queue(writer, objects, func, index_func):
  51. for obj in objects:
  52. writer.initialize(index_func(obj))
  53. q = Queue()
  54. # TODO: limit the number of threads #1828
  55. for obj in objects:
  56. t = Thread(
  57. target=perform_operation,
  58. args=(func, obj, q.put, index_func(obj)))
  59. t.daemon = True
  60. t.start()
  61. return q
  62. class ParallelStreamWriter(object):
  63. """Write out messages for operations happening in parallel.
  64. Each operation has it's own line, and ANSI code characters are used
  65. to jump to the correct line, and write over the line.
  66. """
  67. def __init__(self, stream, msg):
  68. self.stream = stream
  69. self.msg = msg
  70. self.lines = []
  71. def initialize(self, obj_index):
  72. self.lines.append(obj_index)
  73. self.stream.write("{} {} ... \r\n".format(self.msg, obj_index))
  74. self.stream.flush()
  75. def write(self, obj_index, status):
  76. position = self.lines.index(obj_index)
  77. diff = len(self.lines) - position
  78. # move up
  79. self.stream.write("%c[%dA" % (27, diff))
  80. # erase
  81. self.stream.write("%c[2K\r" % 27)
  82. self.stream.write("{} {} ... {}\r".format(self.msg, obj_index, status))
  83. # move back down
  84. self.stream.write("%c[%dB" % (27, diff))
  85. self.stream.flush()
  86. def parallel_operation(containers, operation, options, message):
  87. parallel_execute(
  88. containers,
  89. operator.methodcaller(operation, **options),
  90. operator.attrgetter('name'),
  91. message)
  92. def parallel_remove(containers, options):
  93. stopped_containers = [c for c in containers if not c.is_running]
  94. parallel_operation(stopped_containers, 'remove', options, 'Removing')
  95. def parallel_stop(containers, options):
  96. parallel_operation(containers, 'stop', options, 'Stopping')
  97. def parallel_start(containers, options):
  98. parallel_operation(containers, 'start', options, 'Starting')
  99. def parallel_pause(containers, options):
  100. parallel_operation(containers, 'pause', options, 'Pausing')
  101. def parallel_unpause(containers, options):
  102. parallel_operation(containers, 'unpause', options, 'Unpausing')
  103. def parallel_kill(containers, options):
  104. parallel_operation(containers, 'kill', options, 'Killing')
  105. def parallel_restart(containers, options):
  106. parallel_operation(containers, 'restart', options, 'Restarting')