parallel.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 parallel_execute(objects, func, get_name, msg, get_deps=None):
  13. """Runs func on objects in parallel while ensuring that func is
  14. ran on object only after it is ran on all its dependencies.
  15. get_deps called on object must return a collection with its dependencies.
  16. get_name called on object must return its name.
  17. """
  18. objects = list(objects)
  19. stream = get_output_stream(sys.stderr)
  20. writer = ParallelStreamWriter(stream, msg)
  21. for obj in objects:
  22. writer.initialize(get_name(obj))
  23. q = setup_queue(objects, func, get_deps, get_name)
  24. done = 0
  25. errors = {}
  26. results = []
  27. error_to_reraise = None
  28. while done < len(objects):
  29. try:
  30. obj, result, exception = q.get(timeout=1)
  31. except Empty:
  32. continue
  33. # See https://github.com/docker/compose/issues/189
  34. except thread.error:
  35. raise ShutdownException()
  36. if exception is None:
  37. writer.write(get_name(obj), 'done')
  38. results.append(result)
  39. elif isinstance(exception, APIError):
  40. errors[get_name(obj)] = exception.explanation
  41. writer.write(get_name(obj), 'error')
  42. else:
  43. errors[get_name(obj)] = exception
  44. error_to_reraise = exception
  45. done += 1
  46. for obj_name, error in errors.items():
  47. stream.write("\nERROR: for {} {}\n".format(obj_name, error))
  48. if error_to_reraise:
  49. raise error_to_reraise
  50. return results
  51. def _no_deps(x):
  52. return []
  53. def setup_queue(objects, func, get_deps, get_name):
  54. if get_deps is None:
  55. get_deps = _no_deps
  56. results = Queue()
  57. started = set() # objects being processed
  58. finished = set() # objects which have been processed
  59. def do_op(obj):
  60. try:
  61. result = func(obj)
  62. results.put((obj, result, None))
  63. except Exception as e:
  64. results.put((obj, None, e))
  65. finished.add(obj)
  66. feed()
  67. def ready(obj):
  68. # Is object ready for performing operation
  69. return obj not in started and all(
  70. dep not in objects or dep in finished
  71. for dep in get_deps(obj)
  72. )
  73. def feed():
  74. for obj in filter(ready, objects):
  75. started.add(obj)
  76. t = Thread(target=do_op, args=(obj,))
  77. t.daemon = True
  78. t.start()
  79. feed()
  80. return results
  81. class ParallelStreamWriter(object):
  82. """Write out messages for operations happening in parallel.
  83. Each operation has it's own line, and ANSI code characters are used
  84. to jump to the correct line, and write over the line.
  85. """
  86. def __init__(self, stream, msg):
  87. self.stream = stream
  88. self.msg = msg
  89. self.lines = []
  90. def initialize(self, obj_index):
  91. if self.msg is None:
  92. return
  93. self.lines.append(obj_index)
  94. self.stream.write("{} {} ... \r\n".format(self.msg, obj_index))
  95. self.stream.flush()
  96. def write(self, obj_index, status):
  97. if self.msg is None:
  98. return
  99. position = self.lines.index(obj_index)
  100. diff = len(self.lines) - position
  101. # move up
  102. self.stream.write("%c[%dA" % (27, diff))
  103. # erase
  104. self.stream.write("%c[2K\r" % 27)
  105. self.stream.write("{} {} ... {}\r".format(self.msg, obj_index, status))
  106. # move back down
  107. self.stream.write("%c[%dB" % (27, diff))
  108. self.stream.flush()
  109. def parallel_operation(containers, operation, options, message):
  110. parallel_execute(
  111. containers,
  112. operator.methodcaller(operation, **options),
  113. operator.attrgetter('name'),
  114. message)
  115. def parallel_remove(containers, options):
  116. stopped_containers = [c for c in containers if not c.is_running]
  117. parallel_operation(stopped_containers, 'remove', options, 'Removing')
  118. def parallel_start(containers, options):
  119. parallel_operation(containers, 'start', options, 'Starting')
  120. def parallel_pause(containers, options):
  121. parallel_operation(containers, 'pause', options, 'Pausing')
  122. def parallel_unpause(containers, options):
  123. parallel_operation(containers, 'unpause', options, 'Unpausing')
  124. def parallel_kill(containers, options):
  125. parallel_operation(containers, 'kill', options, 'Killing')
  126. def parallel_restart(containers, options):
  127. parallel_operation(containers, 'restart', options, 'Restarting')