Skip to content

constraint

dev_tool.services.docker.constraint

log = logging.getLogger(__name__) module-attribute

ContainerConstraint

A class that applies resource constraints to Docker containers using docker update.

The constructor for the ContainerConstraint class.

Parameters:

  • client (DockerClient) –

    The Docker client instance.

  • app_name (str) –

    The name of the app container.

  • db_name (str) –

    The name of the database container.

Source code in dev_tool/services/docker/constraint.py
def __init__(self, client: DockerClient, app_name: str, db_name: str) -> None:
    """
    The constructor for the ContainerConstraint class.

    :param client: The Docker client instance.
    :param app_name: The name of the app container.
    :param db_name: The name of the database container.
    """

    self._app_name = app_name
    self._client = client
    self._db_name = db_name

apply

A method that applies resource constraints to both containers.

Parameters:

  • profile (ConstraintProfileType) –

    A dictionary with 'app' and 'db' keys containing constraint parameters.

Returns:

  • bool

    True if all constraints were applied successfully.

Source code in dev_tool/services/docker/constraint.py
def apply(self, profile: ConstraintProfileType) -> bool:
    """
    A method that applies resource constraints to both containers.

    :param profile: A dictionary with 'app' and 'db' keys containing constraint parameters.
    :return: True if all constraints were applied successfully.
    """

    app_ok = self._update_container(self._app_name, **profile['app'])
    db_ok = self._update_container(self._db_name, **profile['db'])

    return app_ok and db_ok

create_custom_profile staticmethod

A method that creates a custom constraint profile.

Parameters:

  • app_memory (str) –

    The app container memory limit (e.g., '1g', '512m').

  • app_cpu (int) –

    The app container CPU percentage (e.g., 100 for 1 core).

  • db_memory (str) –

    The database container memory limit.

  • db_cpu (int) –

    The database container CPU percentage.

Returns:

Source code in dev_tool/services/docker/constraint.py
@staticmethod
def create_custom_profile(
    app_memory: str,
    app_cpu: int,
    db_memory: str,
    db_cpu: int
) -> ConstraintProfileType:
    """
    A method that creates a custom constraint profile.

    :param app_memory: The app container memory limit (e.g., '1g', '512m').
    :param app_cpu: The app container CPU percentage (e.g., 100 for 1 core).
    :param db_memory: The database container memory limit.
    :param db_cpu: The database container CPU percentage.
    :return: The custom constraint profile dictionary.
    """

    cpu_period = 100000

    return {
        'app': {
            'mem_limit': app_memory,
            'memswap_limit': app_memory,
            'cpu_quota': app_cpu * 1000,
            'cpu_period': cpu_period,
        },
        'db': {
            'mem_limit': db_memory,
            'memswap_limit': db_memory,
            'cpu_quota': db_cpu * 1000,
            'cpu_period': cpu_period,
        },
    }

get_profile staticmethod

A method that returns a predefined constraint profile.

Parameters:

Returns:

Source code in dev_tool/services/docker/constraint.py
@staticmethod
def get_profile(name: ConstraintProfile) -> ConstraintProfileType:
    """
    A method that returns a predefined constraint profile.

    :param name: The profile name.
    :return: The constraint profile dictionary.
    """

    return CONSTRAINT_PROFILES[name]