Skip to content

Commit

Permalink
remove docker client creation during class init so ersilia cli can co…
Browse files Browse the repository at this point in the history
…ntinue to work normally (#1193)
  • Loading branch information
DhanshreeA authored Jul 9, 2024
1 parent 253196c commit 702aae8
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions ersilia/utils/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .identifiers.long import LongIdentifier
from .terminal import run_command, run_command_check_output

from .. import logger
from ..default import DEFAULT_DOCKER_PLATFORM, DEFAULT_UDOCKER_USERNAME
from ..utils.system import SystemChecker

Expand Down Expand Up @@ -49,7 +50,6 @@ def is_udocker_installed():
class SimpleDocker(object):
def __init__(self, use_udocker=None):
self.identifier = LongIdentifier()
self.client = docker.from_env()
if use_udocker is None:
self._with_udocker = self._use_udocker()
else:
Expand Down Expand Up @@ -250,17 +250,17 @@ def exec(self, cmd, org, img, tag, name):
self.exec_container(name, cmd)
self.kill(name)

def get_container(self):
def _get_containers(self):
"""
This function will get the Docker container running Ersilia Models.
it wil return a message if a container is not running.
This function will get the Docker container running an ersilia model
"""

try:
containers = self.client.containers.list()
client = docker.from_env()
containers = client.containers.list()

if not containers:
return ["No running containers found"]
logger.debug("No containers found")
return containers

except docker.errors.APIError as e:
Expand All @@ -272,42 +272,41 @@ def get_container(self):

def container_memory(self):
"""
This function will get the total memory usage of the Docker container running Ersilia Models.
This function will get the total memory usage of the Docker container running an ersilia model.
"""

containers = self.get_container()
if isinstance(containers, list) and isinstance(containers[0], str):
return containers[0]

result = []
for container in containers:
stats = container.stats(stream=False)
mem_usage = stats["memory_stats"]["usage"] / (1024 * 1024)
if len(containers) > 0:
result = []
for container in containers:
stats = container.stats(stream=False)
mem_usage = stats["memory_stats"]["usage"] / (1024 * 1024)

return (
f"Total memory consumed by container '{container.name}': {mem_usage:.2f}MiB",
)
return (
f"Total memory consumed by container '{container.name}': {mem_usage:.2f}MiB",
)
return

def container_cpu(self):
"""
This function will get the CPU time of the Docker container running Ersilia Models.
"""

containers = self.get_container()
if isinstance(containers, list) and isinstance(containers[0], str):
return containers[0]

for container in containers:
stats = container.stats(stream=False)
cpu_stats = stats["cpu_stats"]
total_cpu_time = cpu_stats["cpu_usage"]["total_usage"] / 1e9
if len(containers) > 0:
for container in containers:
stats = container.stats(stream=False)
cpu_stats = stats["cpu_stats"]
total_cpu_time = cpu_stats["cpu_usage"]["total_usage"] / 1e9

minutes = total_cpu_time // 60
seconds = total_cpu_time % 60
minutes = total_cpu_time // 60
seconds = total_cpu_time % 60

return (
f"Total CPU time used by container '{container.name}': {int(minutes)} minutes {seconds:.2f} seconds",
)
return (
f"Total CPU time used by container '{container.name}': {int(minutes)} minutes {seconds:.2f} seconds",
)

def container_peak(self):
"""
Expand Down

0 comments on commit 702aae8

Please sign in to comment.