Skip to content

Commit df65135

Browse files
authored
Merge pull request #1016 from minrk/refine-test-env
Refine buffered output debugging
2 parents dc03809 + ab1e33b commit df65135

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

repo2docker/app.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
import shutil
1717
import tempfile
1818
import time
19-
20-
from .engine import BuildError, ContainerEngineException, ImageLoadError
2119
from urllib.parse import urlparse
20+
2221
import escapism
2322
from pythonjsonlogger import jsonlogger
2423

@@ -38,6 +37,7 @@
3837
RBuildPack,
3938
)
4039
from . import contentproviders
40+
from .engine import BuildError, ContainerEngineException, ImageLoadError
4141
from .utils import ByteSpecification, chdir
4242

4343

@@ -629,9 +629,12 @@ def wait_for_container(self, container):
629629
Displaying logs while it's running
630630
"""
631631

632+
last_timestamp = None
632633
try:
633-
for line in container.logs(stream=True):
634-
self.log.info(line.decode("utf-8"), extra=dict(phase="running"))
634+
for line in container.logs(stream=True, timestamps=True):
635+
line = line.decode("utf-8")
636+
last_timestamp, line = line.split(" ", maxsplit=1)
637+
self.log.info(line, extra=dict(phase="running"))
635638

636639
finally:
637640
container.reload()
@@ -646,9 +649,9 @@ def wait_for_container(self, container):
646649
"Container finished running.\n".upper(), extra=dict(phase="running")
647650
)
648651
# are there more logs? Let's send them back too
649-
late_logs = container.logs().decode("utf-8")
652+
late_logs = container.logs(since=last_timestamp).decode("utf-8")
650653
for line in late_logs.split("\n"):
651-
self.log.info(line + "\n", extra=dict(phase="running"))
654+
self.log.debug(line + "\n", extra=dict(phase="running"))
652655

653656
container.remove()
654657
if exit_code:

repo2docker/docker.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"""
44

55
import docker
6+
from iso8601 import parse_date
7+
68
from .engine import Container, ContainerEngine, ContainerEngineException, Image
79

810

@@ -13,8 +15,15 @@ def __init__(self, container):
1315
def reload(self):
1416
return self._c.reload()
1517

16-
def logs(self, *, stream=False):
17-
return self._c.logs(stream=stream)
18+
def logs(self, *, stream=False, timestamps=False, since=None):
19+
if since:
20+
# docker only accepts integer timestamps
21+
# this means we will usually replay logs from the last second
22+
# of the container
23+
# we should check if this ever returns anything new,
24+
# since we know it ~always returns something redundant
25+
since = int(parse_date(since).timestamp())
26+
return self._c.logs(stream=stream, timestamps=timestamps, since=since)
1827

1928
def kill(self, *, signal="KILL"):
2029
return self._c.kill(signal=signal)

repo2docker/engine.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,23 @@ def reload(self):
2121
"""
2222

2323
@abstractmethod
24-
def logs(self, *, stream=False):
24+
def logs(self, *, stream=False, timestamps=False, since=None):
2525
"""
2626
Get the container logs.
2727
2828
Parameters
2929
----------
3030
stream : bool
3131
If `True` return an iterator over the log lines, otherwise return all logs
32+
timestamps : bool
33+
If `True` log lines will be prefixed with iso8601 timestamps followed by space
34+
since : str
35+
A timestamp string
36+
Should be in the same format as the timestamp prefix given
37+
when `timestamps=True`
38+
39+
If given, start logs from this point,
40+
instead of from container start.
3241
3342
Returns
3443
-------

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def get_identifier(json):
5050
"docker",
5151
"entrypoints",
5252
"escapism",
53+
"iso8601",
5354
"jinja2",
5455
"python-json-logger",
5556
"requests",

0 commit comments

Comments
 (0)