Skip to content

Commit

Permalink
stop container when drop finished
Browse files Browse the repository at this point in the history
  • Loading branch information
calgray committed Aug 17, 2021
1 parent 613de94 commit 93e7f39
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions daliuge-engine/dlg/apps/dockerapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import os
import threading
import time
from overrides import overrides

from configobj import ConfigObj
import docker
Expand Down Expand Up @@ -351,7 +352,7 @@ def run(self):
c = DockerApp._get_client()

# Create container
container = c.containers.create(
self.container = c.containers.create(
self._image,
cmd,
volumes=binds,
Expand All @@ -360,12 +361,12 @@ def run(self):
working_dir=self.workdir,
auto_remove=self._removeContainer
)
self._containerId = cId = container.id
self._containerId = cId = self.container.id
logger.info("Created container %s for %r", cId, self)

# Start it
start = time.time()
container.start()
self.container.start()
logger.info("Started container %s", cId)

# Figure out the container's IP and save it
Expand All @@ -378,7 +379,7 @@ def run(self):
# Wait until it finishes
# In docker-py < 3 the .wait() method returns the exit code directly
# In docker-py >= 3 the .wait() method returns a dictionary with the API response
x = container.wait()
x = self.container.wait()
if isinstance(x, dict) and 'StatusCode' in x:
self._exitCode = x['StatusCode']
else:
Expand All @@ -396,15 +397,30 @@ def run(self):
stderr = container.logs(stream=False, stdout=False, stderr=True)
msg = "Container %s didn't finish successfully (exit code %d)" % (cId, self._exitCode)
logger.error(msg + ", output follows.\n==STDOUT==\n%s==STDERR==\n%s", stdout, stderr)

if not self._removeContainer:
container.remove()
raise Exception(msg)

if not self._removeContainer:
container.remove()
c.api.close()

@overrides
def setCompleted(self):
super(BarrierAppDROP, self).setCompleted()
self.container.stop()

@overrides
def setError(self):
super(BarrierAppDROP, self).setError()
self.container.stop()

@overrides
def cancel(self):
super(BarrierAppDROP, self).cancel()
self.container.stop()

@overrides
def skip(self):
super(BarrierAppDROP, self).skip()
self.container.stop()

@classmethod
def _get_client(cls):
return docker.from_env(version='auto', **cls._kwargs_from_env())
Expand Down

0 comments on commit 93e7f39

Please sign in to comment.