Commit 160ac08b authored by Jérome Perrin's avatar Jérome Perrin

grid: log buildout output in realtime

use a thread to read subprocess process output and send it to logging, instead of

this uses the same approach from https://lab.nexedi.com/nexedi/nxdtest
parent cc0627f9
...@@ -39,7 +39,7 @@ import sys ...@@ -39,7 +39,7 @@ import sys
import logging import logging
import psutil import psutil
import time import time
import threading
import six import six
from slapos.grid.exception import BuildoutFailedError, WrongPermissionError from slapos.grid.exception import BuildoutFailedError, WrongPermissionError
...@@ -108,7 +108,7 @@ class SlapPopen(subprocess.Popen): ...@@ -108,7 +108,7 @@ class SlapPopen(subprocess.Popen):
else: else:
kwargs.setdefault('stdout', subprocess.PIPE) kwargs.setdefault('stdout', subprocess.PIPE)
kwargs.setdefault('stderr', subprocess.STDOUT) kwargs.setdefault('stderr', subprocess.STDOUT)
kwargs.update(stdin=subprocess.PIPE) kwargs.update(stdin=subprocess.PIPE, bufsize=1)
if sys.platform == 'cygwin' and kwargs.get('env') == {}: if sys.platform == 'cygwin' and kwargs.get('env') == {}:
kwargs['env'] = None kwargs['env'] = None
...@@ -125,14 +125,29 @@ class SlapPopen(subprocess.Popen): ...@@ -125,14 +125,29 @@ class SlapPopen(subprocess.Popen):
self.stdin.close() self.stdin.close()
self.stdin = None self.stdin = None
output_lines = [] def copy_output_to_log(output_file, logger, output_buffer):
for line in self.stdout: while True:
if type(line) is not str: data = os.read(output_file.fileno(), 4096)
line = line.decode(errors='replace') if not data:
output_lines.append(line) return
logger.info(line.rstrip('\n')) if type(data) is not str:
data = data.decode(errors='replace')
output_buffer.append(data)
logger.info(data.rstrip('\n'))
output_buffer = []
logging_thread = threading.Thread(
target=copy_output_to_log,
args=(
self.stdout,
logger,
output_buffer,
),
)
logging_thread.start()
logging_thread.join()
self.wait() self.wait()
self.output = ''.join(output_lines) self.output = ''.join(output_buffer)
def md5digest(url): def md5digest(url):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment