Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def get_kernel_id_from_container(val: Union[str, DockerContainer]) -> Optional[KernelId]:
if isinstance(val, DockerContainer):
if 'Name' not in val._container:
await val.show()
name = val['Name']
elif isinstance(val, str):
name = val
name = name.lstrip('/')
if not name.startswith('kernel.'):
return None
try:
return KernelId(UUID(name.rsplit('.', 2)[-1]))
except (IndexError, ValueError):
return None
async def api_impl(container_id):
try:
container = DockerContainer(ctx.agent.docker, id=container_id)
ret = await container.stats(stream=False) # TODO: cache
except RuntimeError as e:
msg = str(e.args[0]).lower()
if 'event loop is closed' in msg or 'session is closed' in msg:
return None
raise
except (DockerError, aiohttp.ClientError):
short_cid = container._id[:7]
log.warning(f'cannot read stats: Docker stats API error for {short_cid}.')
return None
else:
# API returned successfully but actually the result may be empty!
if ret is None or not isinstance(ret, dict):
return None
if ret['preread'].startswith('0001-01-01'):
return None