Skip to main content

base

A compute child, as an object that owns its process.

A Compute runs two children, the pod and the patient-data API, as separate processes. A ChildProcess is one of them: the ChildSpec saying what to run, the process started from it, and how to start, stop, health-check and report on it. Nothing outside the children package touches a child's process handle.

A child only does what it is asked. Keeping it running -- noticing it die or hang, and restarting it -- is the Supervisor's job, through these same public methods.

Each subclass holds what differs for its child: how it is spawned, how its health is checked, and what it remembers about the run it is on.

Classes​

ChildHealth​

class ChildHealth(healthy: bool, detail: str | None = None):

Whether a child passed its health check, and why not if it did not.

Arguments

  • healthy: Whether the child is running and answering as it should.
  • detail: Why it is not healthy, for logs and the host's status.

Variables​

  • detail : str | None
  • healthy : bool

ChildProcess​

class ChildProcess(spec: ChildSpecSource):

One child process of a Compute.

Not thread-safe, except check_health and the reads (is_alive, status, latest_health): Compute starts and stops it with its own lock held, and health-checks it without, as a probe can be slow.

Arguments

  • spec: What to run: a spec, or a function building a fresh one for each process.

Ancestors​

Variables​

  • static label : ClassVar[str] - What the child is called in logs, as in "the pod child".
  • static name : ClassVar[str] - What the child is called in the hub's compute heartbeat.

Static methods​


orphan_answer​

def orphan_answer(port: int) ‑> collections.abc.Mapping[str, typing.Any] | None:

What answers on port, if it is this kind of child from any build.

Must not raise, and must give up within a second or so: a start waits on this. See orphans.

Arguments

  • port: The port to ask on.

Returns The /health body it answered with, or None if nothing answers there as this kind of child.

port​

def port() ‑> int:

The port the child serves on.

Methods​


check_health​

def check_health(self) ‑> ChildHealth:

Check the child now, and keep the result for status().

A child that is not running is unhealthy without being asked. A result for a process that was stopped or replaced while it was being checked is returned but not kept.

Returns Whether the child is healthy, and why not if it is not.

clear_orphan​

def clear_orphan(self) ‑> int | None:

Clear the child's port of a child an earlier run left on it.

Done before every start, and safe to do at any other time: this child's own running process is never cleared, so a host may clear ahead of taking a lock it would otherwise hold throughout.

Returns The pid of the process cleared, or None if there was none.

Raises

  • ChildPortHeldError: If the port is held and could not be cleared.

is_alive​

def is_alive(self) ‑> bool:

Whether the child has been started and is still running.

kill​

def kill(self) ‑> None:

Kill the child at once, with none of the grace stop gives it.

For a child that has stopped answering: it will not shut down cleanly, and waiting for it would hold up everything else waiting on the compute's lock. Its process is kept, so it reads as exited until it is started again.

Reported as KILLED without a log line of its own: whoever decides a child is not worth waiting for says why in its own.

latest_health​

def latest_health(self) ‑> ChildHealth:

The child's health now, without probing it.

Its last check's result while it is running. A child that is not running is unhealthy whatever its last check said.

start​

def start(self) ‑> None:

Start the child if it is not running.

A child an earlier run left on the port is cleared first (see orphans). A process that cannot be started leaves the last one, if it had one, in place: a child restarted after a crash still reads as exited, with its exit code, until a start succeeds.

Raises

  • ChildPortHeldError: If the child's port is held and could not be cleared, once reported.
  • Exception: Whatever starting the process raised, once reported.

status​

def status(self) ‑> ChildStatus:

The child's state, from its process if it was ever started.

stop​

def stop(self) ‑> None:

Stop the child if it is running: ask, wait, then kill if it must.

Discards the process once stopped, so status() reports the child as never started rather than as crashed with its SIGTERM exit code -- a poll after a deliberate stop must read the same as one that never found a child, not as one that found a dead one.

ChildStatus​

class ChildStatus(    started: bool,    alive: bool,    exit_code: int | None,    pid: int | None = None,    instance_id: str | None = None,    health: ChildHealth | None = None,    restarts: int = 0,    restart_pending: bool = False,    started_at: float | None = None,):

Whether a child is running, and how it ended if not.

Arguments

  • started: Whether the child has been started since the last stop.
  • alive: Whether it is running now.
  • exit_code: How it ended, if it has.
  • pid: Its process id, if it was started.
  • instance_id: The identity it was handed, for the pod child only; see POD_INSTANCE_ID_ENV_VAR.
  • health: The result of its last health check, if one has run since it was started.
  • restarts: How many times it has been restarted automatically since it was last started deliberately. Filled in by the compute, which does the restarting.
  • restart_pending: Whether it is down and waiting to be restarted automatically. Filled in by the compute.
  • started_at: When its current process started, as a Unix time.

Variables​

  • alive : bool
  • exit_code : int | None
  • instance_id : str | None
  • pid : int | None
  • restart_pending : bool
  • restarts : int
  • started : bool
  • started_at : float | None