Skip to content

process_metadata

drunc.processes.process_metadata

Process metadata management for remote processes.

Classes

ProcessMetadata(pid=None, hostname=None, user=None, started_at=None, tree_id=None, role=None, name=None) dataclass

Metadata about a remote process.

Stores process information that needs to persist across connections, including the remote process ID for signal delivery.

Methods:
compute_role_from_tree_id(tree_id, is_controller=False) staticmethod

Determines the role of a process based on its tree_id and executable type.

- empty tree_id                             -> "unknown"
- is_controller + tree_id == "0"            -> "root-controller"
- is_controller + tree_id starts with "0."  -> "segment-controller"
- is_controller + otherwise                 -> "infrastructure-applications"
- not controller + tree_id starts with "0." -> "application"
- not controller + otherwise                -> "infrastructure-applications"

Parameters:

Name Type Description Default
tree_id str

Dot-separated hierarchical identifier (e.g. "0", "0.1", "0.1.2.3").

required
is_controller bool

True if the process executable is a drunc-controller.

False

Returns:

Type Description
str

Role string: "root-controller", "segment-controller", "application", "infrastructure-applications", or "unknown"

Source code in drunc/processes/process_metadata.py
@staticmethod
def compute_role_from_tree_id(tree_id: str, is_controller: bool = False) -> str:
    """
    Determines the role of a process based on its tree_id and executable type.

        - empty tree_id                             -> "unknown"
        - is_controller + tree_id == "0"            -> "root-controller"
        - is_controller + tree_id starts with "0."  -> "segment-controller"
        - is_controller + otherwise                 -> "infrastructure-applications"
        - not controller + tree_id starts with "0." -> "application"
        - not controller + otherwise                -> "infrastructure-applications"

    Args:
        tree_id: Dot-separated hierarchical identifier (e.g. "0", "0.1", "0.1.2.3").
        is_controller: True if the process executable is a drunc-controller.

    Returns:
        Role string: "root-controller", "segment-controller", "application",
                     "infrastructure-applications", or "unknown"
    """
    if not tree_id:
        return "unknown"
    elif is_controller:
        if tree_id == "0":
            return "root-controller"
        elif tree_id.startswith("0."):
            return "segment-controller"
        else:
            return "infrastructure-applications"  # controller outside segment tree
    else:
        if tree_id.startswith("0."):
            return "application"
        else:
            return "infrastructure-applications"
from_dict(data) classmethod

Create ProcessMetadata from dictionary.

Parameters:

Name Type Description Default
data Dict[str, Any]

Dictionary containing metadata fields

required

Returns:

Type Description
ProcessMetadata

ProcessMetadata instance

Source code in drunc/processes/process_metadata.py
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "ProcessMetadata":
    """
    Create ProcessMetadata from dictionary.

    Args:
        data: Dictionary containing metadata fields

    Returns:
        ProcessMetadata instance
    """
    return cls(
        pid=data.get("pid"),
        hostname=data.get("hostname"),
        user=data.get("user"),
        started_at=data.get("started_at"),
        tree_id=data.get("tree_id"),
        role=data.get("role"),
        name=data.get("name"),
    )
from_json(json_str) classmethod

Deserialise metadata from JSON string.

Parameters:

Name Type Description Default
json_str str

JSON string containing metadata

required

Returns:

Type Description
ProcessMetadata

ProcessMetadata instance

Source code in drunc/processes/process_metadata.py
@classmethod
def from_json(cls, json_str: str) -> "ProcessMetadata":
    """
    Deserialise metadata from JSON string.

    Args:
        json_str: JSON string containing metadata

    Returns:
        ProcessMetadata instance
    """
    return cls.from_dict(json.loads(json_str))
to_dict()

Convert metadata to dictionary for JSON serialisation.

Source code in drunc/processes/process_metadata.py
def to_dict(self) -> Dict[str, Any]:
    """Convert metadata to dictionary for JSON serialisation."""
    return {
        "pid": self.pid,
        "hostname": self.hostname,
        "user": self.user,
        "started_at": self.started_at,
        "tree_id": self.tree_id,
        "role": self.role,
        "name": self.name,
    }
to_json()

Serialise metadata to JSON string.

Returns:

Type Description
str

JSON string representation

Source code in drunc/processes/process_metadata.py
def to_json(self) -> str:
    """
    Serialise metadata to JSON string.

    Returns:
        JSON string representation
    """
    return json.dumps(self.to_dict(), indent=2)