Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework container.execute to use multiprocessing as watchdog #50

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions steps/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import tarfile
import tempfile
import time
import multiprocessing as mp

try:
d = docker.Client(version="1.22")
Expand Down Expand Up @@ -148,26 +149,30 @@ def startWithCommand(self, **kwargs):

def execute(self, cmd, detach=False):
""" executes cmd in container and return its output """
self.logger.debug("container.execute(%s,%s)" % (cmd,detach))
inst = d.exec_create(container=self.container, cmd=cmd)

if detach:
d.exec_start(inst, detach)
return None

output = d.exec_start(inst, detach=detach)
retcode = d.exec_inspect(inst)['ExitCode']
ctx = mp.get_context('fork')
q = ctx.Queue()
p = ctx.Process(target=lambda q: q.put(d.exec_start(inst, detach=detach)), args=(q,))
p.start()

if None == p.join(60): # timeout in secs
p.terminate()
raise ExecException("container.execute: timeout reading from exec (command '{}')".format(cmd))

count = 0
output = q.get()
retcode = d.exec_inspect(inst)['ExitCode']

while retcode is None:
count += 1
retcode = d.exec_inspect(inst)['ExitCode']
time.sleep(1)
if count > 15:
raise ExecException("Command %s timed out, output: %s" % (cmd, output))
if retcode is None:
raise ExecException("Command %s timed out, output: %s" % (cmd, output))

if retcode != 0:
raise ExecException("Command %s failed to execute, return code: %s" % (cmd, retcode), output)
raise ExecException("Command %s failed, return code: %s" % (cmd, retcode), output)

return output

Expand Down
Loading